This repository has been archived by the owner on Aug 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateScreenshot.ts
81 lines (72 loc) · 2.54 KB
/
generateScreenshot.ts
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
/**
* Copyright (C) 2021 PythonCoderAS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {Browser, devices, Page} from "puppeteer";
import * as Buffer from "buffer";
import objectToWrapper from "@healthscreening/object-to-wrapper";
import { resolve } from "path";
import {screeningValueType} from "@healthscreening/screening-types";
/* eslint-disable @typescript-eslint/no-var-requires -- Really hates it if I don't do this */
const puppeteer = require("puppeteer");
/* eslint-enable @typescript-eslint/no-var-requires */
export let browser: Browser | null = null;
export async function startupBrowser(options?: object): Promise<void> {
if (browser === null) {
browser = await puppeteer.launch({
headless: true,
...options,
});
}
}
export async function closeBrowser(): Promise<void> {
if (browser !== null) {
await browser.close();
browser = null;
}
}
export interface GetScreenshotParams {
name: string;
date: string;
type: screeningValueType;
device?: string;
}
const baseURL = "file://" + resolve(require.resolve("@healthscreening/success-screening-clone"), "..", "page.html") + "?"
async function closePage(page: Page){
try {
await page.close();
} catch (e: any) {
if (e.message && e.message === "Protocol error: Connection closed. Most likely the page has been closed."){
// Ignore
} else {
throw e;
}
}
}
export default async function generateScreenshot(
options: GetScreenshotParams
): Promise<Buffer> {
const page = await browser!.newPage();
try {
await page.emulate(devices[options.device || "iPhone 11"]);
await page.goto(baseURL + new URLSearchParams(objectToWrapper(options)).toString());
await page.evaluate(() => {
window.scrollBy(0, -10000);
});
return (await page.screenshot()) as Buffer;
} finally {
await closePage(page)
}
}