Просмотр исходного кода

retry capture when execution context is destroyed and page url changed

Former-commit-id: 544bb12ffbb9ac4cbe0b2a8131c7caff43c1191a
Gildas 6 лет назад
Родитель
Сommit
a046c67a54
1 измененных файлов с 35 добавлено и 18 удалено
  1. 35 18
      cli/back-ends/puppeteer.js

+ 35 - 18
cli/back-ends/puppeteer.js

@@ -101,27 +101,44 @@ exports.getPageData = async options => {
 			timeout: 0,
 			timeout: 0,
 			waitUntil: options.browserWaitUntil || "networkidle0"
 			waitUntil: options.browserWaitUntil || "networkidle0"
 		});
 		});
-		return await page.evaluate(async options => {
-			singlefile.lib.helper.initDoc(document);
-			options.insertSingleFileComment = true;
-			options.insertFaviconLink = true;
-			const preInitializationPromises = [];
-			if (!options.saveRawPage) {
-				if (!options.removeFrames) {
-					preInitializationPromises.push(singlefile.lib.frameTree.content.frames.getAsync(options));
+		try {
+			return await page.evaluate(async options => {
+				singlefile.lib.helper.initDoc(document);
+				options.insertSingleFileComment = true;
+				options.insertFaviconLink = true;
+				const preInitializationPromises = [];
+				if (!options.saveRawPage) {
+					if (!options.removeFrames) {
+						preInitializationPromises.push(singlefile.lib.frameTree.content.frames.getAsync(options));
+					}
+					if (options.loadDeferredImages) {
+						preInitializationPromises.push(singlefile.lib.lazy.content.loader.process(options));
+					}
 				}
 				}
-				if (options.loadDeferredImages) {
-					preInitializationPromises.push(singlefile.lib.lazy.content.loader.process(options));
+				[options.frames] = await Promise.all(preInitializationPromises);
+				options.doc = document;
+				options.win = window;
+				const SingleFile = singlefile.lib.SingleFile.getClass();
+				const singleFile = new SingleFile(options);
+				await singleFile.run();
+				return await singleFile.getPageData();
+			}, options);
+		} catch (error) {
+			if (error.message.includes("Execution context was destroyed")) {
+				const pages = await browser.pages();
+				const page = pages[1] || pages[0];
+				const url = page.url();
+				if (url != options.url) {
+					await browser.close();
+					options.url = url;
+					return exports.getPageData(options);
+				} else {
+					throw error;
 				}
 				}
+			} else {
+				throw error;
 			}
 			}
-			[options.frames] = await Promise.all(preInitializationPromises);
-			options.doc = document;
-			options.win = window;
-			const SingleFile = singlefile.lib.SingleFile.getClass();
-			const singleFile = new SingleFile(options);
-			await singleFile.run();
-			return await singleFile.getPageData();
-		}, options);
+		}
 	} finally {
 	} finally {
 		if (browser && !options.browserDebug) {
 		if (browser && !options.browserDebug) {
 			await browser.close();
 			await browser.close();