Răsfoiți Sursa

implement includeInfobar option

Gildas 3 ani în urmă
părinte
comite
706cda5d92
2 a modificat fișierele cu 21 adăugiri și 1 ștergeri
  1. 6 1
      cli/back-ends/common/scripts.js
  2. 15 0
      cli/single-file-cli-api.js

+ 6 - 1
cli/back-ends/common/scripts.js

@@ -32,8 +32,9 @@ const SCRIPTS = [
 	"lib/single-file-hooks-frames.js"
 ];
 
+const basePath = "../../../";
+
 exports.get = async options => {
-	const basePath = "../../../";
 	let scripts = "let _singleFileDefine; if (typeof define !== 'undefined') { _singleFileDefine = define; define = null }";
 	scripts += await readScriptFiles(SCRIPTS, basePath);
 	scripts += await readScriptFiles(options && options.browserScripts ? options.browserScripts : [], "");
@@ -44,6 +45,10 @@ exports.get = async options => {
 	return scripts;
 };
 
+exports.getInfobarScript = () => {
+	return readScriptFile("lib/single-file-infobar.js", basePath);
+};
+
 async function readScriptFiles(paths, basePath = "../../../") {
 	return (await Promise.all(paths.map(path => readScriptFile(path, basePath)))).join("");
 }

+ 15 - 0
cli/single-file-cli-api.js

@@ -25,6 +25,7 @@
 
 const fs = require("fs");
 const path = require("path");
+const scripts = require("./back-ends/common/scripts.js");
 const VALID_URL_TEST = /^(https?|file):\/\//;
 
 const DEFAULT_OPTIONS = {
@@ -251,6 +252,9 @@ async function capturePage(options) {
 	try {
 		let filename;
 		const pageData = await backend.getPageData(options);
+		if (options.includeInfobar) {
+			await includeInfobarScript(pageData);
+		}
 		if (options.output) {
 			filename = getFilename(options.output, options);
 		} else if (options.dumpContent) {
@@ -313,3 +317,14 @@ function getFilename(filename, options, index = 1) {
 function escapeRegExp(string) {
 	return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
 }
+
+async function includeInfobarScript(pageData) {
+	let infobarContent = await scripts.getInfobarScript();
+	let lastInfobarContent;
+	while (lastInfobarContent != infobarContent) {
+		lastInfobarContent = infobarContent;
+		infobarContent = infobarContent.replace(/\/\*(.|\n)*?\*\//, "");
+	}
+	infobarContent = infobarContent.replace(/\t+/g, " ").replace(/\nthis\.[^(]*/gi, "\n").replace(/\n+/g, "");
+	pageData.content += "<script>document.currentScript.remove();" + infobarContent + "</script>";
+}