Skip to content
This repository has been archived by the owner on Nov 10, 2017. It is now read-only.

A fix and a couple of improvements #22

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
npm-debug.log
.idea
*.iml
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Content sanitization enabled:
{
"byline":"Nicolas Perriault —",
"content":"<p><strong>So finally you&#39;re <a href=\"https://nicolas.perriault.net/code/2013/testing-frontend-javascript-code-using-mocha-chai-and-sinon/\">testing",
"language": "en"
"length":2867,
"title":"Get your Frontend JavaScript Code Covered | Code",
"uri":"https://nicolas.perriault.net/code/2013/get-your-frontend-javascript-code-covered/",
Expand All @@ -77,6 +78,7 @@ Content sanitization disabled (default):
{
"byline":"Nicolas Perriault —",
"content":"<div id=\"readability-page-1\" class=\"page\"><section class=\"\">\n<p><strong>So finally you're…",
"language": "en"
"length":3851,
"title":"Get your Frontend JavaScript Code Covered | Code",
"uri":"https://nicolas.perriault.net/code/2013/get-your-frontend-javascript-code-covered/",
Expand Down
31 changes: 30 additions & 1 deletion phantom-scrape.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@ function outputJSON(object) {
*/
function runReadability(url, userAgent, pageContent) {
var location = document.location;
var getLanguage = function(document) {
var lang = document.documentElement.lang;
if (lang !== undefined) {
return lang;
}
var metas = document.getElementsByTagName('meta');
for (var i=0; i<metas.length; i++) {
if (metas[i].getAttribute("name") === "language") {
return metas[i].getAttribute("content");
}
}
return "";
};

var getDirection = function(document) {
var bodyTags = document.getElementsByTagName('body');
if (bodyTags.length === 0) {
return "";
}
if (window.getComputedStyle) {
return window.getComputedStyle(bodyTags[0], null).getPropertyValue('direction');
}
return "";
};

var uri = {
spec: location.href,
host: location.host,
Expand All @@ -37,6 +62,10 @@ function runReadability(url, userAgent, pageContent) {
if (result) {
result.userAgent = userAgent;
result.isProbablyReaderable = isProbablyReaderable;
result.language = getLanguage(document);
if (result.dir === undefined || result === '') {
result.dir = getDirection(document);
}
} else {
result = {
error: {
Expand Down Expand Up @@ -72,7 +101,7 @@ if (userAgent) {
page.settings.loadImages = false;

// ensure we don't waste time trying to load slow/missing resources
page.settings.resourceTimeout = 1000;
page.settings.resourceTimeout = 3000;

page.onConsoleMessage = function(msg) {
consoleLogs.push(msg);
Expand Down
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ app.get("/api", function(req, res) {
});

app.get("/api/get", function(req, res) {
var url = req.query.url,
var url = encodeURI(req.query.url),
sanitize = boolArg(req.query.sanitize),
userAgent = req.query.userAgent;
if (!url) {
Expand Down