-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·106 lines (88 loc) · 3.4 KB
/
index.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env node
const fs = require("fs");
const puppeteer = require('puppeteer');
const USER_AGENT = "node github.com/FabulousCupcake/gbf-wiki-opengraph"
const EMPTY_PAGE_URL="https://gbf.wiki/User:FabulousCupcake/og/";
// Suppress Fetch API Experimental Warning
// https://github.com/nodejs/node/issues/30810#issuecomment-1138834088
const suppressExperimentalWarning = () => {
const originalEmit = process.emit;
process.emit = function (name, data, ...args) {
if (
name === 'warning' &&
typeof data === 'object' &&
data.name === 'ExperimentalWarning'
)
return false
return originalEmit.apply(process, arguments)
}
}
// fetchFromParseApi uses parse API to execute TemplateSandbox feature
// We basically hotswap `Template:Weapon` with `User:FabulousCupcake/og/Template:weapon`
const fetchFromParseApi = async (pageName) => {
const sandboxPrefix = "User:FabulousCupcake/og";
const escapedPageName = encodeURIComponent(pageName);
const url = `https://gbf.wiki/api.php?action=parse&page=${escapedPageName}&disablelimitreport=1&disabletoc=1&redirects=1&prop=text&templatesandboxprefix=${sandboxPrefix}&format=json`
const data = await fetch(url, { headers: { "User-Agent": USER_AGENT }});
const json = await data.json();
const text = json.parse.text["*"];
return text;
}
// render takes Puppeteer page object and the name of the page to be rendered as image
// And somehow generates an image out of those
const render = async (page, pageName) => {
// Fetch content and put it in the page
const content = await fetchFromParseApi(pageName);
const command = `document.querySelector("#mw-content-text").innerHTML = \`${content}\``;
await page.evaluate(command);
// Wait and ensure it is rendered
await page.waitForSelector('.opengraph-image', { timeout: 5000 });
const element = await page.$('.opengraph-image');
// Wait until images are loaded / network is idle and screenshot
await page.waitForNetworkIdle();
await element.screenshot({
path: `dist/${pageName}.webp`,
omitBackground: true,
type: "webp",
quality: 95,
});
};
// main does a bit of args processing and initializations
const main = async () => {
// We expect stdin to contain the list of page names split by linebreak
const stdin = fs.readFileSync(0).toString();
const pages = stdin
.split("\n")
.filter(x => x)
.map(x => x.replaceAll("&", "&"))
.map(x => x.replaceAll(" ", "_"));
// Initialize Puppeteer
const browser = await puppeteer.launch();
const page = await browser.newPage();
page.setExtraHTTPHeaders({ "User-Agent": USER_AGENT });
await page.goto(EMPTY_PAGE_URL);
await page.addStyleTag({ path: 'style.css' });
// Process
for await (const [index, pageName] of pages.entries()) {
// Pretty-print progress/status
const count = (index+1).toString().padStart(pages.length.toString().length, "0");
const pageNamePadded = pageName.padEnd(48);
process.stdout.write(`[${count}/${pages.length}]: ${pageNamePadded}... `);
try {
const timeStart = performance.now();
await render(page, pageName);
const timeEnd = performance.now();
const duration = (timeEnd - timeStart).toFixed(3);
process.stdout.write(`${duration}ms `);
} catch (err) {
console.log("❌");
console.error(err);
throw(err);
}
console.log("✅");
}
// Exit
await browser.close();
}
suppressExperimentalWarning();
main();