-
Notifications
You must be signed in to change notification settings - Fork 6
/
screen.js
45 lines (37 loc) · 1.18 KB
/
screen.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
const Chrome = require("chrome-remote-interface");
const origin = "https://iws.nu";
module.exports = (req, res) => {
Chrome.New((err, tab) => {
if (err) {
console.log(err);
return Chrome.Close(tab);
}
Chrome((chromeInstance) => {
const { Page, DOMStorage } = chromeInstance;
const start = Date.now();
DOMStorage.domStorageItemAdded(({ storageId, key }) => {
if (storageId.securityOrigin !== origin || key !== "_screen") return;
Page.captureScreenshot()
.then(({ data }) => {
const img = Buffer.from(data, "base64");
res.writeHead(200, {
"Content-Type": "image/png",
"Content-Length": img.length,
});
res.end(img);
const end = Date.now();
console.log("screenshot: " + (end - start) + "ms");
return Promise.all([Chrome.Close(tab), chromeInstance.close()]);
})
.catch((err) => {
console.log(err);
});
});
DOMStorage.enable();
Page.enable();
chromeInstance.once("ready", () => {
Page.navigate({ url: origin + req.url });
});
});
});
};