Răsfoiți Sursa

added "browser-load-max-time" option

Gildas 7 ani în urmă
părinte
comite
d0024128aa
3 a modificat fișierele cu 29 adăugiri și 15 ștergeri
  1. 22 10
      cli/back-ends/webdriver-chromium.js
  2. 2 3
      cli/back-ends/webdriver-gecko.js
  3. 5 2
      cli/single-file

+ 22 - 10
cli/back-ends/webdriver-chromium.js

@@ -21,7 +21,7 @@
  *   Source.
  */
 
-/* global require, exports, process */
+/* global require, exports, process, setTimeout, clearTimeout */
 
 const fs = require("fs");
 
@@ -95,7 +95,7 @@ exports.getPageData = async options => {
 		}
 		builder.setChromeOptions(chromeOptions);
 		driver = await builder.forBrowser("chrome").build();
-		driver.manage().setTimeouts({ script: null, pageLoad: null, implicit: null });
+		driver.manage().setTimeouts({ script: options.browserLoadMaxTime, pageLoad: options.browserLoadMaxTime, implicit: options.browserLoadMaxTime });
 		if (options.browserWidth && options.browserHeight) {
 			const window = driver.manage().window();
 			if (window.setRect) {
@@ -107,14 +107,26 @@ exports.getPageData = async options => {
 		let scripts = SCRIPTS.map(scriptPath => fs.readFileSync(require.resolve(scriptPath)).toString()).join("\n");
 		scripts += "\nlazyLoader.getScriptContent = " + (function (path) { return (RESOLVED_CONTENTS)[path]; }).toString().replace("RESOLVED_CONTENTS", JSON.stringify(RESOLVED_CONTENTS)) + ";";
 		await driver.get(options.url);
-		if (!optionHeadless && (options.browserWaitUntil === undefined || options.browserWaitUntil == "networkidle0")) {
-			await driver.executeAsyncScript(scripts + "\naddEventListener(\"single-file-network-idle-0\", () => arguments[0](), true)");
-		} else if (!optionHeadless && options.browserWaitUntil == "networkidle2") {
-			await driver.executeAsyncScript(scripts + "\naddEventListener(\"single-file-network-idle-2\", () => arguments[0](), true)");
-		} else if (optionHeadless || options.browserWaitUntil == "load") {
-			await driver.executeAsyncScript(scripts + "\nif (document.readyState == \"loading\" || document.readyState == \"interactive\") { document.addEventListener(\"load\", () => arguments[0]()) } else { arguments[0](); }");
-		} else {
-			await driver.executeScript(scripts);
+		await driver.executeScript(scripts);
+		if (options.browserWaitUntil != "domcontentloaded") {
+			let scriptPromise;
+			if (!optionHeadless && (options.browserWaitUntil === undefined || options.browserWaitUntil == "networkidle0")) {
+				scriptPromise = driver.executeAsyncScript("addEventListener(\"single-file-network-idle-0\", () => arguments[0](), true)");
+			} else if (!optionHeadless && options.browserWaitUntil == "networkidle2") {
+				scriptPromise = driver.executeAsyncScript("addEventListener(\"single-file-network-idle-2\", () => arguments[0](), true)");
+			} else if (optionHeadless || options.browserWaitUntil == "load") {
+				scriptPromise = driver.executeAsyncScript("if (document.readyState == \"loading\" || document.readyState == \"interactive\") { document.addEventListener(\"load\", () => arguments[0]()) } else { arguments[0](); }");
+			}
+			let cancelTimeout;
+			const timeoutPromise = new Promise(resolve => {
+				const timeoutId = setTimeout(resolve, Math.max(0, options.browserLoadMaxTime - 5000));
+				cancelTimeout = () => {
+					clearTimeout(timeoutId);
+					resolve();
+				};
+			});
+			await Promise.race([scriptPromise, timeoutPromise]);
+			cancelTimeout();
 		}
 		const result = await driver.executeAsyncScript(getPageDataScript(), options);
 		if (result.error) {

+ 2 - 3
cli/back-ends/webdriver-gecko.js

@@ -51,7 +51,6 @@ const SCRIPTS = [
 	"../../lib/single-file/single-file-core.js",
 	"../../lib/single-file/single-file-browser.js"
 ];
-const NETWORK_IDLE_MAX_DELAY = 60000;
 
 exports.getPageData = async options => {
 	const RESOLVED_CONTENTS = {
@@ -89,7 +88,7 @@ exports.getPageData = async options => {
 		}
 		builder.setFirefoxOptions(firefoxOptions);
 		driver = await builder.forBrowser("firefox").build();
-		driver.manage().setTimeouts({ script: 2 * NETWORK_IDLE_MAX_DELAY, pageLoad: 2 * NETWORK_IDLE_MAX_DELAY, implicit: 2 * NETWORK_IDLE_MAX_DELAY });
+		driver.manage().setTimeouts({ script: options.browserLoadMaxTime, pageLoad: options.browserLoadMaxTime, implicit: options.browserLoadMaxTime });
 		if (options.browserWidth && options.browserHeight) {
 			const window = driver.manage().window();
 			if (window.setRect) {
@@ -113,7 +112,7 @@ exports.getPageData = async options => {
 			}
 			let cancelTimeout;
 			const timeoutPromise = new Promise(resolve => {
-				const timeoutId = setTimeout(resolve, NETWORK_IDLE_MAX_DELAY);
+				const timeoutId = setTimeout(resolve, Math.max(0, options.browserLoadMaxTime - 5000));
 				cancelTimeout = () => {
 					clearTimeout(timeoutId);
 					resolve();

+ 5 - 2
cli/single-file

@@ -37,6 +37,7 @@ const args = require("yargs")
 		"browser-executable-path": "",
 		"browser-width": 1280,
 		"browser-height": 720,
+		"browser-load-max-time": 60000,
 		"browser-wait-until": "networkidle0",
 		"compress-CSS": true,
 		"compress-HTML": true,
@@ -70,6 +71,8 @@ const args = require("yargs")
 	.number("browser-width")
 	.options("browser-height", { description: "Height of the browser viewport in pixels" })
 	.number("browser-height")
+	.options("browser-load-max-time", { description: "Maximum delay of time to wait for page loading in ms (webdriver-gecko, webdriver-chromium)" })
+	.number("browser-load-max-time")
 	.options("browser-wait-until", { description: "When to consider the page is loaded (puppeteer, webdriver-gecko, webdriver-chromium)" })
 	.choices("browser-wait-until", ["networkidle0", "networkidle2", "load", "domcontentloaded"])
 	.options("compress-CSS", { description: "Compress CSS stylesheets" })
@@ -82,11 +85,11 @@ const args = require("yargs")
 	.boolean("group-duplicate-images")
 	.options("load-deferred-images", { description: "Load deferred (a.k.a. lazy-loaded) images (puppeteer, webdriver-gecko, webdriver-chromium)" })
 	.boolean("load-deferred-images")
-	.options("load-deferred-images-max-idle-time", { description: "Maximum delay of time to wait for deferred images (puppeteer, webdriver-gecko, webdriver-chromium)" })
+	.options("load-deferred-images-max-idle-time", { description: "Maximum delay of time to wait for deferred images in ms (puppeteer, webdriver-gecko, webdriver-chromium)" })
 	.number("load-deferred-images-max-idle-time")
 	.options("max-resource-size-enabled", { description: "Enable removal of embedded resources exceeding a given size" })
 	.boolean("max-resource-size-enabled")
-	.options("max-resource-size", { description: "Maximum size of embedded resources (i.e. images, stylesheets, scripts and iframes)" })
+	.options("max-resource-size", { description: "Maximum size of embedded resources in MB (i.e. images, stylesheets, scripts and iframes)" })
 	.number("max-resource-size")
 	.options("remove-frames", { description: "Remove frames (puppeteer, webdriver-gecko, webdriver-chromium)" })
 	.boolean("remove-frames")