Browse Source

added "browser-debug" option

Gildas 7 years ago
parent
commit
8451ef64b7

+ 11 - 3
cli/back-ends/puppeteer.js

@@ -58,10 +58,15 @@ exports.getPageData = async options => {
 	};
 	const browserOptions = {};
 	if (options.browserHeadless !== undefined) {
-		browserOptions.headless = options.browserHeadless;
+		browserOptions.headless = options.browserHeadless && !options.browserDebug;
 	}
+	browserOptions.args = [];
 	if (options.browserDisableWebSecurity === undefined || options.browserDisableWebSecurity) {
-		browserOptions.args = ["--disable-web-security", "--no-pings"];
+		browserOptions.args.push("--disable-web-security");
+		browserOptions.args.push("--no-pings");
+	}
+	if (!options.browserHeadless && options.browserDebug) {
+		browserOptions.args.push("--auto-open-devtools-for-tabs");
 	}
 	if (options.browserExecutablePath) {
 		browserOptions.executablePath = options.browserExecutablePath || "chrome";
@@ -85,6 +90,9 @@ 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 page.evaluateOnNewDocument(scripts);
+		if (options.browserDebug) {
+			await page.waitFor(3000);
+		}
 		await page.goto(options.url, {
 			waitUntil: options.browserWaitUntil || "networkidle0"
 		});
@@ -108,7 +116,7 @@ exports.getPageData = async options => {
 			return await singleFile.getPageData();
 		}, options);
 	} finally {
-		if (browser) {
+		if (browser && !options.browserDebug) {
 			await browser.close();
 		}
 	}

+ 9 - 3
cli/back-ends/webdriver-chromium.js

@@ -21,7 +21,7 @@
  *   Source.
  */
 
-/* global require, exports, process, setTimeout, clearTimeout */
+/* global require, exports, process, setTimeout, clearTimeout, Buffer */
 
 const fs = require("fs");
 
@@ -61,7 +61,7 @@ exports.getPageData = async options => {
 	try {
 		const builder = new Builder();
 		const chromeOptions = new chrome.Options();
-		const optionHeadless = options.browserHeadless === undefined || options.browserHeadless;
+		const optionHeadless = (options.browserHeadless === undefined || options.browserHeadless) && !options.browserDebug;
 		if (optionHeadless) {
 			chromeOptions.headless();
 		}
@@ -76,6 +76,9 @@ exports.getPageData = async options => {
 			chromeOptions.addArguments("--no-pings");
 		}
 		if (!optionHeadless) {
+			if (options.browserDebug) {
+				chromeOptions.addArguments("--auto-open-devtools-for-tabs");
+			}
 			const extensions = [];
 			if (options.browserBypassCSP === undefined || options.browserBypassCSP) {
 				extensions.push(encode(require.resolve("./extensions/signed/bypass_csp-0.0.3-an+fx.xpi")));
@@ -106,6 +109,9 @@ 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)) + ";";
+		if (options.browserDebug) {
+			await driver.sleep(3000);
+		}
 		await driver.get(options.url);
 		await driver.executeScript(scripts);
 		if (options.browserWaitUntil != "domcontentloaded") {
@@ -135,7 +141,7 @@ exports.getPageData = async options => {
 			return result.pageData;
 		}
 	} finally {
-		if (driver) {
+		if (driver && !options.browserDebug) {
 			driver.quit();
 		}
 	}

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

@@ -26,7 +26,7 @@
 const fs = require("fs");
 
 const firefox = require("selenium-webdriver/firefox");
-const { Builder } = require("selenium-webdriver");
+const { Builder, By, Key } = require("selenium-webdriver");
 
 const SCRIPTS = [
 	"../../lib/hooks/hooks-frame.js",
@@ -61,7 +61,7 @@ exports.getPageData = async options => {
 	try {
 		const builder = new Builder().withCapabilities({ "pageLoadStrategy": "none" });
 		const firefoxOptions = new firefox.Options();
-		if (options.browserHeadless === undefined || options.browserHeadless) {
+		if ((options.browserHeadless === undefined || options.browserHeadless) && !options.browserDebug) {
 			firefoxOptions.headless();
 		}
 		if (options.browserExecutablePath) {
@@ -99,6 +99,10 @@ exports.getPageData = async options => {
 		}
 		let scripts = SCRIPTS.map(scriptPath => fs.readFileSync(require.resolve(scriptPath)).toString().replace(/\n(this)\.([^ ]+) = (this)\.([^ ]+) \|\|/g, "\nwindow.$2 = window.$4 ||")).join("\n");
 		scripts += "\nlazyLoader.getScriptContent = " + (function (path) { return (RESOLVED_CONTENTS)[path]; }).toString().replace("RESOLVED_CONTENTS", JSON.stringify(RESOLVED_CONTENTS)) + ";";
+		if (options.browserDebug) {
+			await driver.findElement(By.css("html")).sendKeys(Key.SHIFT + Key.F5);
+			await driver.sleep(3000);
+		}
 		await driver.get(options.url);
 		await driver.executeScript(scripts);
 		if (options.browserWaitUntil != "domcontentloaded") {
@@ -131,7 +135,7 @@ exports.getPageData = async options => {
 			return result.pageData;
 		}
 	} finally {
-		if (driver) {
+		if (driver && !options.browserDebug) {
 			driver.quit();
 		}
 	}

+ 3 - 0
cli/single-file

@@ -39,6 +39,7 @@ const args = require("yargs")
 		"browser-height": 720,
 		"browser-load-max-time": 60000,
 		"browser-wait-until": "networkidle0",
+		"browser-debug": false,
 		"compress-CSS": true,
 		"compress-HTML": true,
 		"filename-template": "",
@@ -75,6 +76,8 @@ const args = require("yargs")
 	.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("browser-debug", { description: "Enable debug mode (puppeteer, webdriver-gecko, webdriver-chromium)" })
+	.boolean("browser-debug")
 	.options("compress-CSS", { description: "Compress CSS stylesheets" })
 	.boolean("compress-CSS")
 	.options("compress-HTML", { description: "Compress HTML content" })

+ 7 - 3
maff2html/back-ends/webdriver-gecko.js

@@ -26,7 +26,7 @@
 const fs = require("fs");
 
 const firefox = require("selenium-webdriver/firefox");
-const { Builder } = require("selenium-webdriver");
+const { Builder, By, Key } = require("selenium-webdriver");
 
 const SCRIPTS = [
 	"../../lib/hooks/hooks-frame.js",
@@ -62,7 +62,7 @@ exports.getPageData = async options => {
 		const builder = new Builder().withCapabilities({ "pageLoadStrategy": "eager" });
 		const firefoxOptions = new firefox.Options();
 		options.enableMaff = true;
-		if (options.browserHeadless === undefined || options.browserHeadless) {
+		if ((options.browserHeadless === undefined || options.browserHeadless) && !options.browserDebug) {
 			firefoxOptions.headless();
 		}
 		if (options.browserExecutablePath) {
@@ -99,6 +99,10 @@ exports.getPageData = async options => {
 		}
 		let scripts = SCRIPTS.map(scriptPath => fs.readFileSync(require.resolve(scriptPath)).toString().replace(/\n(this)\.([^ ]+) = (this)\.([^ ]+) \|\|/g, "\nwindow.$2 = window.$4 ||")).join("\n");
 		scripts += "\nlazyLoader.getScriptContent = " + (function (path) { return (RESOLVED_CONTENTS)[path]; }).toString().replace("RESOLVED_CONTENTS", JSON.stringify(RESOLVED_CONTENTS)) + ";";
+		if (options.browserDebug) {
+			await driver.findElement(By.css("html")).sendKeys(Key.SHIFT + Key.F5);
+			await driver.sleep(3000);
+		}
 		await driver.get(options.url);
 		await driver.executeScript(scripts);
 		if (options.browserWaitUntil != "domcontentloaded") {
@@ -128,7 +132,7 @@ exports.getPageData = async options => {
 			return result.pageData;
 		}
 	} finally {
-		if (driver) {
+		if (driver && !options.browserDebug) {
 			driver.quit();
 		}
 	}

+ 3 - 0
maff2html/single-file

@@ -38,6 +38,7 @@ const args = require("yargs")
 		"browser-height": 720,
 		"browser-load-max-time": 60000,
 		"browser-wait-until": "load",
+		"browser-debug": false,
 		"compress-CSS": true,
 		"compress-HTML": true,
 		"filename-template": "",
@@ -71,6 +72,8 @@ const args = require("yargs")
 	.number("browser-load-max-time")
 	.options("browser-wait-until", { description: "When to consider the page is loaded" })
 	.choices("browser-wait-until", ["networkidle0", "networkidle2", "load", "domcontentloaded"])
+	.options("browser-debug", { description: "Enable debug mode" })
+	.boolean("browser-debug")
 	.options("compress-CSS", { description: "Compress CSS stylesheets" })
 	.boolean("compress-CSS")
 	.options("compress-HTML", { description: "Compress HTML content" })