Преглед изворни кода

added "--browser-scripts" option (cf. issue #196)

Gildas пре 6 година
родитељ
комит
db47036556

+ 1 - 1
cli/back-ends/jsdom.js

@@ -82,7 +82,7 @@ exports.getPageData = async options => {
 		options.insertFaviconLink = true;
 		const win = dom.window;
 		const doc = win.document;
-		let scripts = (await Promise.all(SCRIPTS.concat(options.browserScripts || []).map(scriptPath => fs.readFileSync(require.resolve(scriptPath)).toString()))).join("\n");
+		let scripts = (await Promise.all(SCRIPTS.concat(options.browserScripts).map(scriptPath => fs.readFileSync(require.resolve(scriptPath)).toString()))).join("\n");
 		scripts = "this.browser = { runtime: { getURL:() => " + JSON.stringify(require.resolve("../../lib/hooks/hooks-web.js")) + " } }" + scripts;
 		dom.window.eval(scripts);
 		if (dom.window.document.readyState == "loading") {

+ 1 - 1
cli/back-ends/puppeteer.js

@@ -82,7 +82,7 @@ exports.getPageData = async options => {
 		if (options.browserBypassCSP === undefined || options.browserBypassCSP) {
 			await page.setBypassCSP(true);
 		}
-		let scripts = SCRIPTS.map(scriptPath => fs.readFileSync(require.resolve(scriptPath)).toString()).join("\n");
+		let scripts = SCRIPTS.concat(options.browserScripts).map(scriptPath => fs.readFileSync(require.resolve(scriptPath)).toString()).join("\n");
 		scripts = "this.browser = { runtime: { getURL:() => " + JSON.stringify(require.resolve("../../lib/hooks/hooks-web.js")) + " } }" + scripts;
 		await page.evaluateOnNewDocument(scripts);
 		if (options.browserDebug) {

+ 1 - 1
cli/back-ends/webdriver-chromium.js

@@ -106,7 +106,7 @@ exports.getPageData = async options => {
 				window.setSize(options.browserWidth, options.browserHeight);
 			}
 		}
-		let scripts = SCRIPTS.map(scriptPath => fs.readFileSync(require.resolve(scriptPath)).toString()).join("\n");
+		let scripts = SCRIPTS.concat(options.browserScripts).map(scriptPath => fs.readFileSync(require.resolve(scriptPath)).toString()).join("\n");
 		scripts = "this.browser = { runtime: { getURL:() => " + JSON.stringify(require.resolve("../../lib/hooks/hooks-web.js")) + " } }" + scripts;
 		if (options.browserDebug) {
 			await driver.sleep(3000);

+ 1 - 1
cli/back-ends/webdriver-gecko.js

@@ -96,7 +96,7 @@ exports.getPageData = async options => {
 				window.setSize(options.browserWidth, options.browserHeight);
 			}
 		}
-		let scripts = SCRIPTS.map(scriptPath => fs.readFileSync(require.resolve(scriptPath)).toString().replace(/\n(this)\.([^ ]+) = (this)\.([^ ]+) \|\|/g, "\nwindow.$2 = window.$4 ||")).join("\n");
+		let scripts = SCRIPTS.concat(options.browserScripts).map(scriptPath => fs.readFileSync(require.resolve(scriptPath)).toString().replace(/\n(this)\.([^ ]+) = (this)\.([^ ]+) \|\|/g, "\nwindow.$2 = window.$4 ||")).join("\n");
 		scripts = "this.browser = { runtime: { getURL:() => " + JSON.stringify(require.resolve("../../lib/hooks/hooks-web.js")) + " } }" + scripts;
 		if (options.browserDebug) {
 			await driver.findElement(By.css("html")).sendKeys(Key.SHIFT + Key.F5);

+ 5 - 1
cli/single-file

@@ -42,6 +42,7 @@ const args = require("yargs")
 		"browser-wait-until": "networkidle0",
 		"browser-debug": false,
 		"browser-extensions": [],
+		"browser-scripts": [],
 		"compress-CSS": true,
 		"compress-HTML": true,
 		"filename-template": "",
@@ -80,8 +81,10 @@ const args = require("yargs")
 	.choices("browser-wait-until", ["networkidle0", "networkidle2", "load", "domcontentloaded"])
 	.options("browser-debug", { description: "Enable debug mode (puppeteer, webdriver-gecko, webdriver-chromium)" })
 	.boolean("browser-debug")
-	.options("browser-extensions", { description: "List of extension paths separated by a space and relative to cli folder (webdriver-gecko, webdriver-chromium)" })
+	.options("browser-extensions", { description: "List of extension paths separated by a space and relative to the 'cli' folder (webdriver-gecko, webdriver-chromium)" })
 	.array("browser-extensions")
+	.options("browser-scripts", { description: "List of script paths separated by a space and relative to the 'cli' folder" })
+	.array("browser-scripts")
 	.options("compress-CSS", { description: "Compress CSS stylesheets" })
 	.boolean("compress-CSS")
 	.options("compress-HTML", { description: "Compress HTML content" })
@@ -137,6 +140,7 @@ args.compressHTML = args.compressHtml;
 if (!/^(https?|file):\/\//.test(args.url)) {
 	args.url = fileUrl(args.url);
 }
+args.browserScripts = args.browserScripts.map(path => require.resolve(path));
 require(backEnds[args.backEnd]).getPageData(args).then(pageData => {
 	if (args.output) {
 		require("fs").writeFileSync(args.output, pageData.content);