Browse Source

merged processor.js into autosave.js

Gildas 7 năm trước cách đây
mục cha
commit
dbb709d373

+ 44 - 1
extension/core/bg/autosave.js

@@ -18,7 +18,7 @@
  *   along with SingleFile.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-/* global browser, singlefile */
+/* global browser, singlefile, SingleFileBrowser, URL, Blob */
 
 singlefile.autosave = (() => {
 
@@ -34,6 +34,9 @@ singlefile.autosave = (() => {
 		if (message.isAutoSaveEnabled) {
 			return isAutoSaveEnabled(sender.tab.id);
 		}
+		if (message.autoSaveContent) {
+			saveContent(message, sender.tab.id, sender.tab.incognito);
+		}
 	});
 
 	if (browser.runtime.onMessageExternal) {
@@ -59,6 +62,46 @@ singlefile.autosave = (() => {
 		refresh
 	};
 
+	async function saveContent(message, tabId, incognito) {
+		const options = await singlefile.config.getDefaultConfig();
+		options.content = message.content;
+		options.url = message.url;
+		options.framesData = message.framesData;
+		options.canvasData = message.canvasData;
+		options.fontsData = message.fontsData;
+		options.stylesheetContents = message.stylesheetContents;
+		options.imageData = message.imageData;
+		options.postersData = message.postersData;
+		options.usedFonts = message.usedFonts;
+		options.shadowRootContents = message.shadowRootContents;
+		options.insertSingleFileComment = true;
+		options.insertFaviconLink = true;
+		options.backgroundTab = true;
+		options.autoSave = true;
+		options.incognito = incognito;
+		options.tabId = tabId;
+		options.sessionId = 0;
+		let index = 0, maxIndex = 0;
+		options.onprogress = async event => {
+			if (event.type == event.RESOURCES_INITIALIZED) {
+				maxIndex = event.detail.max;
+				singlefile.ui.onProgress(tabId, index, maxIndex, { autoSave: true });
+			}
+			if (event.type == event.RESOURCE_LOADED) {
+				index++;
+				singlefile.ui.onProgress(tabId, index, maxIndex, { autoSave: true });
+			} else if (event.type == event.PAGE_ENDED) {
+				singlefile.ui.onEnd(tabId, { autoSave: true });
+			}
+		};
+		const processor = new (SingleFileBrowser.getClass())(options);
+		await processor.initialize();
+		await processor.run();
+		const page = await processor.getPageData();
+		page.url = URL.createObjectURL(new Blob([page.content], { type: "text/html" }));
+		return singlefile.download.downloadPage(page, options);
+	}
+
 	async function enableActiveTab(enabled) {
 		const tabs = await browser.tabs.query({ currentWindow: true, active: true });
 		const tab = tabs[0];

+ 0 - 72
extension/core/bg/processor.js

@@ -1,72 +0,0 @@
-/*
- * Copyright 2018 Gildas Lormeau
- * contact : gildas.lormeau <at> gmail.com
- * 
- * This file is part of SingleFile.
- *
- *   SingleFile is free software: you can redistribute it and/or modify
- *   it under the terms of the GNU Lesser General Public License as published by
- *   the Free Software Foundation, either version 3 of the License, or
- *   (at your option) any later version.
- *
- *   SingleFile is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU Lesser General Public License for more details.
- *
- *   You should have received a copy of the GNU Lesser General Public License
- *   along with SingleFile.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-/* global browser, SingleFileBrowser, singlefile, Blob, URL */
-
-singlefile.processor = (() => {
-
-	browser.runtime.onMessage.addListener((request, sender) => {
-		if (request.saveContent) {
-			saveContent(request, sender.tab.id, sender.tab.incognito);
-		}
-	});
-	return true;
-
-	async function saveContent(message, tabId, incognito) {
-		const options = await singlefile.config.getDefaultConfig();
-		options.content = message.content;
-		options.url = message.url;
-		options.framesData = message.framesData;
-		options.canvasData = message.canvasData;
-		options.fontsData = message.fontsData;
-		options.stylesheetContents = message.stylesheetContents;
-		options.imageData = message.imageData;
-		options.postersData = message.postersData;
-		options.usedFonts = message.usedFonts;
-		options.shadowRootContents = message.shadowRootContents;
-		options.insertSingleFileComment = true;
-		options.insertFaviconLink = true;
-		options.backgroundTab = true;
-		options.autoSave = true;
-		options.incognito = incognito;
-		options.tabId = tabId;
-		options.sessionId = 0;
-		let index = 0, maxIndex = 0;
-		options.onprogress = async event => {
-			if (event.type == event.RESOURCES_INITIALIZED) {
-				maxIndex = event.detail.max;
-				singlefile.ui.onProgress(tabId, index, maxIndex, { autoSave: true });
-			}
-			if (event.type == event.RESOURCE_LOADED) {
-				index++;
-				singlefile.ui.onProgress(tabId, index, maxIndex, { autoSave: true });
-			} else if (event.type == event.PAGE_ENDED) {
-				singlefile.ui.onEnd(tabId, { autoSave: true });
-			}
-		};
-		const processor = new (SingleFileBrowser.getClass())(options);
-		await processor.initialize();
-		await processor.run();
-		const page = await processor.getPageData();
-		page.url = URL.createObjectURL(new Blob([page.content], { type: "text/html" }));
-		return singlefile.download.downloadPage(page, options);
-	}
-
-})();

+ 2 - 2
extension/core/content/content-bootstrap.js

@@ -60,7 +60,7 @@ this.singlefile.autosave = this.singlefile.autosave || (async () => {
 					framesData = await frameTree.getAsync(options);
 				}
 				browser.runtime.sendMessage({
-					saveContent: true,
+					autoSaveContent: true,
 					content: docHelper.serialize(document, false),
 					canvasData: docData.canvasData,
 					fontsData: docData.fontsData,
@@ -99,7 +99,7 @@ this.singlefile.autosave = this.singlefile.autosave || (async () => {
 			const docData = docHelper.preProcessDoc(document, window, options);
 			const framesData = (typeof frameTree != "undefined") && !options.removeFrames && frameTree.getSync(options);
 			browser.runtime.sendMessage({
-				saveContent: true,
+				autoSaveContent: true,
 				content: docHelper.serialize(document),
 				canvasData: docData.canvasData,
 				fontsData: docData.fontsData,

+ 0 - 1
manifest.json

@@ -57,7 +57,6 @@
 			"extension/core/bg/tabs-data.js",
 			"extension/core/bg/download.js",
 			"extension/core/bg/runner.js",
-			"extension/core/bg/processor.js",
 			"extension/core/bg/autosave.js",
 			"extension/core/bg/config.js",
 			"extension/core/bg/core.js",