-
Notifications
You must be signed in to change notification settings - Fork 11
/
scan_page.js
60 lines (52 loc) · 2.17 KB
/
scan_page.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const puppeteer = require('puppeteer');
const { color, readCache, writeCache, writeHistory } = require('./utils');
const engine = process.argv[2];
const url = process.argv[3];
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// load cookies, if they exist
const cookieData = readCache(engine, 'cookies');
if (cookieData.cookies) {
for (let cookie of cookieData.cookies) {
await page.setCookie(cookie);
}
}
await page.goto(url);
const pageTitle = await page.title();
writeHistory(url, 'R', { title: pageTitle });
await page.addScriptTag({url: 'https://unpkg.com/turndown/dist/turndown.js'});
await page.addScriptTag({url: 'https://unpkg.com/turndown-plugin-gfm/dist/turndown-plugin-gfm.js'});
const content = await page.evaluate(() => {
// return document.getElementsByTagName('body')[0].innerText;
let turndownService = new TurndownService({
headingStyle: 'atx',
codeBlockStyle: 'fenced',
bulletListMarker: '-',
// linkStyle: 'referenced',
// linkReferenceStyle: 'collapsed'
});
let gfm = turndownPluginGfm.gfm;
turndownService.use(gfm);
turndownService.remove('script');
turndownService.remove('style');
/*turndownService.addRule('url', { // force url to be one-line to avoid breaking later regex
filter: ['a'],
replacement: function (content, node) {
let href = node.getAttribute('href');
let title = node.title ? ' "' + node.title.replace('\n', ' ') + '"' : '';
return '[' + content + '](' + href + ')';
}
});*/
let markdown = turndownService.turndown(document.getElementsByTagName('body')[0].innerHTML);
return markdown;
});
writeCache('_result_', 'preview', {
url: url,
content: content.split('\n')
});
content.split('\n').forEach((line, index) => {
console.log(`${color.bright}\x1b[38;5;237m${('' + index).padEnd(3)}${color.reset}\x1b[38;5;244m${line}${color.reset}`);
})
await browser.close();
})();