switch to server-side readability

This commit is contained in:
Nazar Kanaev 2021-03-29 14:25:49 +01:00
parent a83d43a5b1
commit 485587825c
4 changed files with 29 additions and 23 deletions

File diff suppressed because one or more lines are too long

View File

@ -102,9 +102,7 @@
return api('post', './logout')
},
crawl: function(url) {
return xfetch('./page?url=' + url).then(function(res) {
return res.text()
})
return api('post', './page?url=' + url).then(json)
}
}
})()

View File

@ -612,15 +612,9 @@ var vm = new Vue({
}
if (item.link) {
this.loading.readability = true
api.crawl(item.link).then(function(body) {
api.crawl(item.link).then(function(data) {
vm.itemSelectedReadability = data && data.content
vm.loading.readability = false
if (!body.length) return
var bodyClean = sanitize(body, item.link)
var doc = new DOMParser().parseFromString(bodyClean, 'text/html')
var parsed = new Readability(doc).parse()
if (parsed && parsed.content) {
vm.itemSelectedReadability = parsed.content
}
})
}
},

View File

@ -2,7 +2,6 @@ package server
import (
"encoding/json"
"io/ioutil"
"log"
"math"
"net/http"
@ -12,6 +11,7 @@ import (
"github.com/nkanaev/yarr/src/auth"
"github.com/nkanaev/yarr/src/opml"
"github.com/nkanaev/yarr/src/router"
"github.com/nkanaev/yarr/src/scraper"
"github.com/nkanaev/yarr/src/storage"
"github.com/nkanaev/yarr/src/worker"
)
@ -389,16 +389,32 @@ func (s *Server) handleOPMLExport(c *router.Context) {
}
func (s *Server) handlePageCrawl(c *router.Context) {
query := c.Req.URL.Query()
if url := query.Get("url"); len(url) > 0 {
res, err := http.Get(url)
if err == nil {
body, err := ioutil.ReadAll(res.Body)
if err == nil {
c.Out.Write(body)
}
}
if c.Req.Method != "POST" {
c.Out.WriteHeader(http.StatusBadRequest)
return
}
url := c.Req.URL.Query().Get("url")
if url == "" {
c.Out.WriteHeader(http.StatusBadRequest)
return
}
res, err := http.Get(url)
if err != nil {
log.Print(err)
c.Out.WriteHeader(http.StatusNoContent)
return
}
defer res.Body.Close()
content, err := scraper.ExtractContent(res.Body)
if err != nil {
log.Print(err)
c.Out.WriteHeader(http.StatusNoContent)
return
}
content = scraper.Sanitize(url, content)
c.JSON(http.StatusOK, map[string]string{
"content": content,
})
}
func (s *Server) handleLogout(c *router.Context) {