Browse Source

add option --browser-stylesheet

Gildas 5 years ago
parent
commit
7d977328e7
2 changed files with 9 additions and 1 deletions
  1. 6 1
      cli/args.js
  2. 3 0
      cli/back-ends/common/scripts.js

+ 6 - 1
cli/args.js

@@ -40,6 +40,7 @@ const args = require("yargs")
 		"browser-wait-until-fallback": true,
 		"browser-debug": false,
 		"browser-script": [],
+		"browser-stylesheet": [],
 		"browser-args": "",
 		"browser-start-minimized": false,
 		"browser-cookie": [],
@@ -100,8 +101,10 @@ const args = require("yargs")
 	.boolean("browser-wait-until-fallback")
 	.options("browser-debug", { description: "Enable debug mode (puppeteer, webdriver-gecko, webdriver-chromium)" })
 	.boolean("browser-debug")
-	.options("browser-script", { description: "Path of a script executed before the page is loaded (and all the frames)." })
+	.options("browser-script", { description: "Path of a script executed in the page (and all the frames) before it is loaded" })
 	.array("browser-script")
+	.options("browser-stylesheet", { description: "Path of a stylesheet file inserted into the page (and all the frames) after it is loaded" })
+	.array("browser-stylesheet")
 	.options("browser-args", { description: "Arguments provided as a JSON array and passed to the browser (puppeteer, webdriver-gecko, webdriver-chromium)" })
 	.string("browser-args")
 	.options("browser-start-minimized", { description: "Minimize the browser (puppeteer)" })
@@ -233,6 +236,8 @@ args.browserCookies = cookies.map(cookieValue => {
 });
 args.browserScripts = args.browserScript;
 delete args.browserScript;
+args.browserStylesheets = args.browserStylesheet;
+delete args.browserStylesheet;
 args.crawlRewriteRules = args.crawlRewriteRule;
 delete args.crawlRewriteRule;
 Object.keys(args).filter(optionName => optionName.includes("-"))

+ 3 - 0
cli/back-ends/common/scripts.js

@@ -73,6 +73,9 @@ exports.get = async options => {
 	scripts += "this.singlefile.lib.getFileContent = filename => (" + JSON.stringify(webScripts) + ")[filename];\n";
 	scripts += await readScriptFiles(SCRIPTS, basePath);
 	scripts += await readScriptFiles(options && options.browserScripts ? options.browserScripts : [], "");
+	if (options.browserStylesheets && options.browserStylesheets.length) {
+		scripts += "addEventListener(\"load\",()=>{const styleElement=document.createElement(\"style\");styleElement.textContent=" + JSON.stringify(await readScriptFiles(options.browserStylesheets, "")) + ";document.body.appendChild(styleElement);});";
+	}
 	return scripts;
 };