-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathpuppeteer.ts
More file actions
336 lines (314 loc) · 9.56 KB
/
puppeteer.ts
File metadata and controls
336 lines (314 loc) · 9.56 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* puppeteer.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import { readRegistryKey } from "./windows.ts";
import { safeExistsSync, which } from "./path.ts";
import { debug, error, info } from "../deno_ral/log.ts";
import { existsSync } from "../deno_ral/fs.ts";
import { UnreachableError } from "./lib/error.ts";
import { quartoDataDir } from "./appdirs.ts";
import { isMac, isWindows } from "../deno_ral/platform.ts";
import puppeteer from "puppeteer";
// deno-lint-ignore no-explicit-any
// let puppeteerImport: any = undefined;
// deno-lint-ignore prefer-const
// let puppeteerUrl = "puppeteer";
export async function getPuppeteer() {
return puppeteer;
// if (puppeteerImport !== undefined) {
// return puppeteerImport;
// }
// puppeteerImport = (await import(puppeteerUrl)).default;
// return puppeteerImport;
}
/**
* Extracts images and elements from an URL
*
* @param urlOrOptions webpage url or page options
* @param selector css selector
* @param filenames filenames to write screenshots to
* @returns html content of selected results
*/
export async function extractImagesFromElements(
urlOrOptions: string | PageOptions,
selector: string,
filenames: string[],
): Promise<string[]> {
return await withPuppeteerBrowserAndPage(
urlOrOptions,
// deno-lint-ignore no-explicit-any
async (_browser: any, page: any) => {
const elements = await page.$$(selector);
if (elements.length !== filenames.length) {
throw new Error(
`extractImagesFromElements was given ${filenames.length} filenames, but selector yielded ${elements.length} elements.`,
);
}
for (let i = 0; i < elements.length; ++i) {
await elements[i].screenshot({ path: filenames[i] });
}
// deno-lint-ignore no-explicit-any
const document = undefined as any;
const clientSideResult = await page.evaluate((selector: string) => {
// deno-lint-ignore no-explicit-any
return Array.from(document.querySelectorAll(selector)).map((n: any) =>
n.outerHTML
);
}, selector);
return clientSideResult;
},
);
}
export function extractHtmlFromElements(
url: string,
selector: string,
): Promise<string[]> {
// deno-lint-ignore no-explicit-any
const document = undefined as any;
return inPuppeteer(url, (selector: string) => {
// deno-lint-ignore no-explicit-any
return Array.from(document.querySelectorAll(selector)).map((n: any) =>
n.outerHTML
);
}, selector);
}
export interface PageOptions {
url: string;
viewport?: {
// https://github.com/puppeteer/puppeteer/blob/v0.12.0/docs/api.md#pagesetviewportviewport
width: number;
height: number;
deviceScaleFactor?: number;
isMobile?: boolean;
hasTouch?: boolean;
isLandscape?: boolean;
};
}
export async function withPuppeteerBrowserAndPage<T>(
url: string | PageOptions,
// deno-lint-ignore no-explicit-any
f: (b: any, p: any) => Promise<T>,
): Promise<T> {
const allowedErrorMessages = [
"Navigation failed because browser has disconnected!",
"Navigation timeout of 30000 ms exceeded",
"Evaluation failed: undefined",
];
let attempts = 0;
const maxAttempts = 5;
while (attempts++ < maxAttempts) {
try {
let finished = false;
let result: T;
// deno-lint-ignore no-explicit-any
await withHeadlessBrowser(async (browser: any) => {
const page = await browser.newPage();
if (typeof url === "string") {
await page.goto(url);
} else {
if (url.viewport) {
page.setViewport(url.viewport);
}
await page.goto(url.url);
}
result = await f(browser, page);
finished = true;
});
if (finished) {
return result!;
}
} catch (error) {
if (!(error instanceof Error)) throw error;
if (
(allowedErrorMessages.indexOf(error.message) !== -1) &&
(attempts < maxAttempts)
) {
error(
`\nEncountered a bad error message from puppeteer: "${error.message}"\n Retrying ${attempts}/${maxAttempts}`,
);
} else {
throw error;
}
}
}
throw new UnreachableError();
}
export async function inPuppeteer(
url: string,
// deno-lint-ignore no-explicit-any
f: any,
// deno-lint-ignore no-explicit-any
...params: any[]
// deno-lint-ignore no-explicit-any
): Promise<any> {
const allowedErrorMessages = [
"Navigation failed because browser has disconnected!",
"Navigation timeout of 30000 ms exceeded",
"Evaluation failed: undefined",
];
let attempts = 0;
const maxAttempts = 5;
while (attempts++ < maxAttempts) {
try {
// deno-lint-ignore no-explicit-any
return await withHeadlessBrowser(async (browser: any) => {
const page = await browser.newPage();
await page.goto(url);
const clientSideResult = await page.evaluate(f, ...params);
return clientSideResult;
});
} catch (error) {
if (!(error instanceof Error)) throw error;
if (
(allowedErrorMessages.indexOf(error.message) !== -1) &&
(attempts < maxAttempts)
) {
error(
`\nEncountered a bad error message from puppeteer: "${error.message}"\n Retrying ${attempts}/${maxAttempts}`,
);
} else {
throw error;
}
}
}
throw new UnreachableError();
}
export async function withHeadlessBrowser<T>(
// deno-lint-ignore no-explicit-any
fn: (browser: any) => Promise<T>,
) {
const browser = await fetchBrowser();
if (browser !== undefined) {
try {
const result = await fn(browser);
await browser.close();
return result;
} catch (e) {
// we can't try ... finally here because it plays badly with async
// and return values.
await browser.close();
throw e;
}
}
}
interface ChromeInfo {
path: string | undefined;
source: string | undefined;
}
export async function findChrome(): Promise<ChromeInfo> {
let path;
let source;
// First check env var and use this path if specified
const envPath = Deno.env.get("QUARTO_CHROMIUM");
if (envPath) {
debug("[CHROMIUM] Using path specified in QUARTO_CHROMIUM");
if (safeExistsSync(envPath)) {
debug(`[CHROMIUM] Found at ${envPath}, and will be used.`);
return { path: envPath, source: "QUARTO_CHROMIUM" };
} else {
debug(
`[CHROMIUM] Not found at ${envPath}. Check your environment variable valye. Searching now for another binary.`,
);
}
}
// Otherwise, try to find the path based on OS.
if (isMac) {
const programs = [
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
"/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
];
path = programs.find(safeExistsSync);
source = "MacOS known location";
} else if (isWindows) {
// Try the HKLM key
const programs = ["chrome.exe", "msedge.exe"];
for (let i = 0; i < programs.length; i++) {
path = await readRegistryKey(
"HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\" +
programs[i],
"(Default)",
);
if (path && safeExistsSync(path)) {
source = "Windows Registry";
break;
}
}
// Try the HKCR key
if (!path) {
const regKeys = ["ChromeHTML", "MSEdgeHTM"];
for (let i = 0; i < regKeys.length; i++) {
path = await readRegistryKey(
`HKCR\\${regKeys[i]}\\shell\\open\\command`,
"(Default)",
);
path = path?.match(/"(.*)"/);
path = path ? path[1] : undefined;
if (path && existsSync(path)) {
source = "Windows Registry";
break;
}
}
}
} else {
// in 1.28.2, this is (isLinux)
// in 1.32, there's other non-linux unixes
path = await which("google-chrome");
if (!path) {
path = await which("chromium-browser");
}
if (path && existsSync(path)) {
source = "PATH";
}
}
if (path) {
debug("[CHROMIUM] Found Chromium on OS known location");
debug(`[CHROMIUM] Path: ${path}`);
} else {
debug("[CHROMIUM] Chromium not found on OS known location");
}
return { path: path, source: source };
}
export async function getBrowserExecutablePath() {
// Cook up a new instance
const browserFetcher = await fetcher();
const availableRevisions = await browserFetcher.localRevisions();
let executablePath: string | undefined = undefined;
if (executablePath === undefined) {
executablePath = (await findChrome()).path;
}
if (executablePath === undefined && availableRevisions.length > 0) {
// get the latest available revision
availableRevisions.sort((a: string, b: string) => Number(b) - Number(a));
const revision = availableRevisions[0];
const revisionInfo = browserFetcher.revisionInfo(revision);
executablePath = revisionInfo.executablePath;
}
if (executablePath === undefined) {
error("Chrome not found");
info(
"\nNo Chrome or Chromium installation was detected.\n\nPlease run 'quarto install chromium' to install Chromium.\n",
);
throw new Error();
}
return executablePath;
}
async function fetchBrowser() {
const executablePath = await getBrowserExecutablePath();
const puppeteer = await getPuppeteer();
return await puppeteer.launch({
product: "chrome",
executablePath,
});
}
export async function fetcher() {
const options = {
path: chromiumInstallDir(),
};
const fetcher = (await getPuppeteer()).createBrowserFetcher(options);
return fetcher;
}
export function chromiumInstallDir(): string | undefined {
return quartoDataDir("chromium");
}