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

load optional content scripts on demand

Gildas пре 7 година
родитељ
комит
b6dcd89214
2 измењених фајлова са 57 додато и 12 уклоњено
  1. 35 12
      extension/core/bg/core.js
  2. 22 0
      lib/single-file/single-file-browser.js

+ 35 - 12
extension/core/bg/core.js

@@ -26,25 +26,38 @@ singlefile.core = (() => {
 
 	const contentScriptFiles = [
 		"/lib/browser-polyfill/custom-browser-polyfill.js",
-		"/extension/index.js",
 		"/lib/single-file/doc-helper.js",
-		"/extension/ui/content/content-ui.js",
 		"/lib/single-file/base64.js",
-		"/lib/single-file/uglifycss.js",
-		"/lib/single-file/css-what.js",
-		"/lib/single-file/parse-css.js",
-		"/lib/single-file/fonts-minifier.js",
-		"/lib/single-file/css-minifier.js",
-		"/lib/single-file/htmlmini.js",
 		"/lib/single-file/parse-srcset.js",
-		"/lib/single-file/lazy-loader.js",
-		"/lib/single-file/serializer.js",
+		"/lib/fetch/content/fetch.js",
 		"/lib/single-file/single-file-core.js",
 		"/lib/single-file/single-file-browser.js",
-		"/lib/fetch/content/fetch.js",
+		"/extension/index.js",
+		"/extension/ui/content/content-ui.js",
 		"/extension/core/content/content.js"
 	];
 
+	const optionalContentScriptFiles = {
+		compressHTML: [
+			"/lib/single-file/htmlmini.js",
+			"/lib/single-file/serializer.js"
+		],
+		compressCSS: [
+			"/lib/single-file/uglifycss.js"
+		],
+		removeAlternativeFonts: [
+			"/lib/single-file/fonts-minifier.js"
+		],
+		removeUnusedStyles: [
+			"/lib/single-file/css-what.js",
+			"/lib/single-file/parse-css.js",
+			"/lib/single-file/css-minifier.js"
+		],
+		lazyLoadImages: [
+			"/lib/single-file/lazy-loader.js",
+		]
+	};
+
 	browser.runtime.onMessage.addListener((request, sender) => {
 		if (request.getConfig) {
 			return singlefile.config.get();
@@ -133,7 +146,7 @@ singlefile.core = (() => {
 	}
 
 	async function processStart(tab, options) {
-		await executeScripts(tab.id, contentScriptFiles, { allFrames: false });
+		await executeScripts(tab.id, getContentScriptFiles(options), { allFrames: false });
 		if (options.frameId) {
 			await browser.tabs.sendMessage(tab.id, { processStartFrame: true, options }, { frameId: options.frameId });
 		} else {
@@ -148,4 +161,14 @@ singlefile.core = (() => {
 		}
 	}
 
+	function getContentScriptFiles(options) {
+		let files = contentScriptFiles;
+		Object.keys(optionalContentScriptFiles).forEach(option => {
+			if (options[option]) {
+				files = optionalContentScriptFiles[option].concat(files);
+			}
+		});
+		return files;
+	}
+
 })();

+ 22 - 0
lib/single-file/single-file-browser.js

@@ -29,6 +29,28 @@ this.SingleFile = this.SingleFile || (() => {
 	// --------	
 	let fetchResource;
 
+	if (this.serializer === undefined) {
+		this.serializer = {
+			process(doc) {
+				const docType = doc.doctype;
+				let docTypeString = "";
+				if (docType) {
+					docTypeString = "<!DOCTYPE " + docType.nodeName;
+					if (docType.publicId) {
+						docTypeString += " PUBLIC \"" + docType.publicId + "\"";
+						if (docType.systemId)
+							docTypeString += " \"" + docType.systemId + "\"";
+					} else if (docType.systemId)
+						docTypeString += " SYSTEM \"" + docType.systemId + "\"";
+					if (docType.internalSubset)
+						docTypeString += " [" + docType.internalSubset + "]";
+					docTypeString += "> ";
+				}
+				return docTypeString + doc.documentElement.outerHTML;
+			}
+		};
+	}
+
 	class Download {
 		static async getContent(resourceURL, options) {
 			let resourceContent;