-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
116 lines (97 loc) · 3.37 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
107
108
109
110
111
112
113
114
115
116
//@ts-check
/** @type {import('puppeteer-core').Puppeteer} */
const puppeteer = require('puppeteer-extra');
const {exec} = require('child_process');
const path = require('path');
const fs = require('fs');
const {checkForWorkarounds} = require('./workarounds');
const args = process.argv.slice(2);
// add stealth plugin and use defaults (all evasion techniques)
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
puppeteer.use(StealthPlugin())
async function getChromePath() {
let path;
try {
path = await linuxAppPath('google-chrome');
} catch (ball) {
path = await linuxAppPath('chromium');
}
return path;
}
/**
* @param {string} app
*/
function linuxAppPath(app) {
return new Promise((resolve, reject) => {
exec(`which ${app}`, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result.trim());
}
});
});
}
/**
* @param {import('puppeteer').Browser} browser
*/
async function openChromePopupPage(browser) {
const backgroundTarget = (await browser.targets()).find((t) => 'background_page' === t.type());
const backgroundPage = await backgroundTarget.page();
const popupURL = backgroundPage.url().replace('/_generated_background_page.html', '/edit.html');
const extensionPopup = await browser.newPage();
await extensionPopup.goto(popupURL);
return extensionPopup;
}
const chromeExtensionDebugDir = path.join(__dirname, '../stylus/');
// check if folder exist.
if (!fs.existsSync(chromeExtensionDebugDir)) {
// if not, error.
console.error(`chrome extension debug dir not found: ${chromeExtensionDebugDir}`);
process.exit(1);
}
const cssStyle = fs.readFileSync(path.resolve(args[0]), 'utf8');
(async () => {
const executablePath = await getChromePath();
const now = performance.now()
const browser = await puppeteer.launch({
executablePath,
headless: false,
// Optimized for speed.
// Really should have some more insight into chromiums flags.
// But this is a good start.
args: [
`--disable-extensions-except=${chromeExtensionDebugDir}`,
`--load-extension=${chromeExtensionDebugDir}`,
'--show-component-extension-options',
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-accelerated-2d-canvas',
'--no-first-run',
'--no-zygote',
'--single-process',
'--disable-gpu',
`--enable-features=enable-quic,enable-zero-copy`,
],
});
const stylusExt = await openChromePopupPage(browser);
stylusExt.evaluate(async (/** @type {string} */ style) => {
// @ts-ignore
editor.getEditors()[0].setValue(style);
const nameField = document.querySelector('#basic-info #name');
// @ts-ignore
nameField.value = 'preview-style';
// @ts-ignore
editor.updateName(true);
// save
// @ts-ignore
await editor.save();
}, cssStyle);
const page = await browser.newPage();
await page.goto(args[1], {waitUntil: 'domcontentloaded'});
await checkForWorkarounds(page);
await page.screenshot({path: 'output.png'});
console.log(`Took screenshot in ${performance.now() - now}ms`);
await browser.close();
})();