diff options
author | Florian Bruhin <me@the-compiler.org> | 2019-09-15 23:10:08 +0200 |
---|---|---|
committer | Florian Bruhin <me@the-compiler.org> | 2019-09-15 23:10:08 +0200 |
commit | f86afb77d04260bf177eb475ef36ad7fdea8ae57 (patch) | |
tree | 8b9cd8407f58739ca433aa752f7628387c8037fe | |
parent | b1a8feb6ba2346642aef78b4440550912cd1c63b (diff) | |
parent | c203a7e2d101cc87704b29827648f24d0143ebbc (diff) | |
download | qutebrowser-f86afb77d04260bf177eb475ef36ad7fdea8ae57.tar.gz qutebrowser-f86afb77d04260bf177eb475ef36ad7fdea8ae57.zip |
Merge remote-tracking branch 'origin/pr/5009'
-rwxr-xr-x | misc/userscripts/readability-js | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/misc/userscripts/readability-js b/misc/userscripts/readability-js new file mode 100755 index 000000000..7f0349181 --- /dev/null +++ b/misc/userscripts/readability-js @@ -0,0 +1,69 @@ +#!/usr/bin/env node +// +// # Description +// +// Summarize the current page in a new tab, by processing it with the standalone readability +// library used for Firefox Reader View. +// +// # Prerequisites +// +// - NODE_PATH might be required to point to your global node libraries (e.g. /usr/lib/node_modules) +// - Mozilla's readability library (npm install -g https://github.com/mozilla/readability.git) +// NOTE: You might have to *login* as root for a system-wide installation to work (e.g. sudo -s) +// - jsdom (npm install -g jsdom) +// - qutejs (npm install -g qutejs) +// +// # Usage +// +// :spawn --userscript readability-js +// +// One may wish to define an easy to type command alias in Qutebrowser's configuration file: +// c.aliases = {"readability" : "spawn --userscript readability-js", ...} + +const Readability = require('readability'); +const qute = require('qutejs'); +const JSDOM = require('jsdom').JSDOM; +const fs = require('fs'); +const path = require('path'); +const util = require('util'); + +const HEADER = ` +<!DOCTYPE html> +<html> +<head> + <meta name="viewport" content="width=device-width, initial-scale=1, text/html, charset=UTF-8" http-equiv="Content-Type"> + </meta> + <title>%s</title> + <style type="text/css"> + body { + margin: 30px auto; + max-width: 650px; + line-height: 1.4; + padding: 0 10px; + } + h1, h2, h3 { + line-height: 1.2; + } + </style> +</head>`; +const scriptsDir = path.join(process.env.QUTE_DATA_DIR, 'userscripts'); +const tmpFile = path.join(scriptsDir, '/readability.html'); + +if (!fs.existsSync(scriptsDir)){ + fs.mkdirSync(scriptsDir); +} + +JSDOM.fromFile(process.env.QUTE_HTML, { url: process.env.QUTE_URL }).then(dom => { + let reader = new Readability(dom.window.document); + let article = reader.parse(); + let content = util.format(HEADER, article.title) + article.content; + + fs.writeFile(tmpFile, content, (err) => { + if (err) { + qute.messageError([`"${err}"`]) + return 1; + } + // Success + qute.open(['-t', tmpFile]); + }) +}); |