Skip to content

Instantly share code, notes, and snippets.

@shogochiai
Last active April 12, 2018 14:44
Show Gist options
  • Save shogochiai/a3af4f1722c9005a6a24021705890ea9 to your computer and use it in GitHub Desktop.
Save shogochiai/a3af4f1722c9005a6a24021705890ea9 to your computer and use it in GitHub Desktop.
Fetch ethresear.ch to local. Because I wanna put it on newtab on chrome and I wanna render it faster.
/*
* Minimum requirement: NodeJS 8.0.0
* Put `*/15 * * * * /path/to/wget https://ethresear.ch -O /tmp/ethresear.ch >/tmp/stdout.log 2>/tmp/stderr.log && /path/to/node /path/to/ethresearch-localizer.js`
*/
const cheerio = require('cheerio')
const fs = require('fs')
const LOCAL_FILE = "/tmp/ethresear.ch"
const html = fs.readFileSync(LOCAL_FILE)
const $ = cheerio.load(html)
$('head').find('link').each(replaceIfAbsoluteLink)
$('head').find('script').each(replaceIfAbsoluteLink)
$('body').find('a').each(replaceIfAbsoluteLink)
function replaceIfAbsoluteLink(i,el) {
let srcObj = el.attribs.href ?
{ type: 'href', item: el.attribs.href }
:
{ type: 'src', item: el.attribs.src }
if (srcObj.item && srcObj.item.indexOf('/') === 0) {
let replacedSrc = 'https://ethresear.ch'+srcObj.item
$(el).attr(srcObj.type, replacedSrc)
}
}
let replacedHtml = $.html()
fs.writeFileSync(LOCAL_FILE, replacedHtml)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment