#!/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 // // - Setting NODE_PATH might be required to point qutebrowser to your global node libraries: // export NODE_PATH=$NODE_PATH:$(npm root -g) // - Mozilla's readability library (npm install -g @mozilla/readability) // - 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('@mozilla/readability'); const qute = require('qutejs'); const JSDOM = require('jsdom').JSDOM; const fs = require('fs'); const path = require('path'); const util = require('util'); const HEADER = ` %s `; const scriptsDir = path.join(process.env.QUTE_DATA_DIR, 'userscripts'); const tmpFile = path.join(scriptsDir, '/readability.html'); if (!fs.existsSync(scriptsDir)) { fs.mkdirSync(scriptsDir); } let getDOM, domOpts, target; // When hinting, use the selected hint instead of the current page if (process.env.QUTE_MODE === 'hints') { getDOM = JSDOM.fromURL; target = process.env.QUTE_URL; } else { getDOM = JSDOM.fromFile; domOpts = {url: process.env.QUTE_URL, contentType: "text/html; charset=utf-8"}; target = process.env.QUTE_HTML; } getDOM(target, domOpts).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', '-r', tmpFile]); }) });