-
Notifications
You must be signed in to change notification settings - Fork 4
/
get-image.js
40 lines (33 loc) · 1.18 KB
/
get-image.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
const puppeteer = require('puppeteer');
module.exports = async url => {
// disable sandbox in production
const browser = await puppeteer.launch({
slowMo: 100,
// headless: false,
args: process.env.NODE_ENV === 'production' ? ['--no-sandbox'] : []
});
const context = await browser.createIncognitoBrowserContext();
const page = await context.newPage();
// accept the "replace drawing" dialog if it shows up
page.on('dialog', dialog => dialog.accept());
await page.goto(url);
await page.click('[data-testid=image-export-button]');
const result = await page.mainFrame().evaluate(
() =>
new Promise((resolve, reject) => {
try {
delete window.chooseFileSystemEntries;
const reader = new FileReader();
reader.addEventListener('loadend', () => resolve(reader.result));
reader.addEventListener('error', () => reject(reader.error));
URL.createObjectURL = blob => reader.readAsText(blob);
const button = document.querySelector('[aria-label="Export to SVG"]');
button.click();
} catch (error) {
reject(error);
}
})
);
await browser.close();
return result;
};