Procházet zdrojové kódy

added "auto-save tab" feature (via the context menu)

Gildas před 7 roky
rodič
revize
74b0d2db87
3 změnil soubory, kde provedl 52 přidání a 23 odebrání
  1. 0 2
      extension/core/bg/bg.js
  2. 16 11
      extension/core/content/content.js
  3. 36 10
      extension/ui/bg/ui.js

+ 0 - 2
extension/core/bg/bg.js

@@ -75,8 +75,6 @@ singlefile.core = (() => {
 		async processTab(tab, processOptions = {}) {
 			const options = await singlefile.config.get();
 			Object.keys(processOptions).forEach(key => options[key] = processOptions[key]);
-			options.insertSingleFileComment = true;
-			options.insertFaviconLink = true;
 			return new Promise(async (resolve, reject) => {
 				const processPromise = processStart(tab, options);
 				const errorTimeout = setTimeout(reject, TIMEOUT_PROCESS_START_MESSAGE);

+ 16 - 11
extension/core/content/content.js

@@ -36,22 +36,27 @@ this.singlefile.top = this.singlefile.top || (() => {
 	return true;
 
 	async function savePage(message) {
-		if (message.processStart && !processing && !message.options.frameId) {
+		const options = message.options;
+		if (!processing) {
 			processing = true;
-			try {
-				const page = await processPage(message.options);
-				await downloadPage(page, message.options);
-				revokeDownloadURL(page);
-			} catch (error) {
-				console.error(error); // eslint-disable-line no-console
-				browser.runtime.sendMessage({ processError: true, error });
+			if (message.processStart && !options.frameId) {
+				try {
+					const page = await processPage(options);
+					await downloadPage(page, options);
+					revokeDownloadURL(page);
+				} catch (error) {
+					console.error(error); // eslint-disable-line no-console
+					browser.runtime.sendMessage({ processError: true, error });
+				}
 			}
 			processing = false;
 		}
 	}
 
 	async function processPage(options) {
-		if (options.shadowEnabled) {
+		options.insertSingleFileComment = true;
+		options.insertFaviconLink = true;
+		if (options.shadowEnabled && !options.autoSave) {
 			singlefile.ui.init();
 		}
 		options = await getOptions(options);
@@ -68,7 +73,7 @@ this.singlefile.top = this.singlefile.top || (() => {
 		const date = new Date();
 		page.filename = page.title + (options.appendSaveDate ? " (" + date.toISOString().split("T")[0] + " " + date.toLocaleTimeString() + ")" : "") + ".html";
 		page.url = URL.createObjectURL(new Blob([page.content], { type: "text/html" }));
-		if (options.shadowEnabled) {
+		if (options.shadowEnabled && !options.autoSave) {
 			singlefile.ui.end();
 		}
 		if (options.displayStats) {
@@ -119,7 +124,7 @@ this.singlefile.top = this.singlefile.top || (() => {
 				} catch (error) {
 					/* ignored */
 				}
-				if (options.shadowEnabled) {
+				if (options.shadowEnabled && !options.autoSave) {
 					singlefile.ui.onprogress(event);
 				}
 			} else if (event.type == event.PAGE_ENDED) {

+ 36 - 10
extension/ui/bg/ui.js

@@ -33,6 +33,7 @@ singlefile.ui = (() => {
 	const MENU_ID_SAVE_FRAME = "save-frame";
 	const MENU_ID_SAVE_TABS = "save-tabs";
 	const MENU_ID_SAVE_SELECTED_TABS = "save-selected-tabs";
+	const MENU_ID_AUTO_SAVE = "auto-save";
 
 	const tabs = {};
 
@@ -56,6 +57,12 @@ singlefile.ui = (() => {
 				const tabs = await browser.tabs.query({ currentWindow: true, highlighted: true });
 				tabs.forEach(tab => isAllowedURL(tab.url) && processTab(tab));
 			}
+			if (event.menuItemId == MENU_ID_AUTO_SAVE) {
+				if (!tabs[tab.id]) {
+					tabs[tab.id] = {};
+				}
+				tabs[tab.id].autoSave = event.checked;
+			}
 		});
 	}
 	browser.browserAction.onClicked.addListener(async tab => {
@@ -69,11 +76,18 @@ singlefile.ui = (() => {
 		}
 	});
 	browser.tabs.onActivated.addListener(async activeInfo => {
-		const tab = browser.tabs.get(activeInfo.tabId);
-		onTabActivated(tab.id, isAllowedURL(tab.url));
+		const tab = await browser.tabs.get(activeInfo.tabId);
+		onTabActivated(tab);
+	});
+	browser.tabs.onCreated.addListener(tab => onTabActivated(tab));
+	browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
+		if (tabs[tab.id] && tabs[tab.id].autoSave) {
+			if (changeInfo.status == "complete") {
+				processTab(tab, { autoSave: true });
+			}
+		}
+		onTabActivated(tab);
 	});
-	browser.tabs.onCreated.addListener(tab => onTabActivated(tab.id, isAllowedURL(tab.url)));
-	browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => onTabActivated(tab.id, isAllowedURL(tab.url)));
 	browser.tabs.onRemoved.addListener(tabId => onTabRemoved(tabId));
 	browser.runtime.onMessage.addListener((request, sender) => {
 		if (request.processProgress && request.maxIndex) {
@@ -121,6 +135,18 @@ singlefile.ui = (() => {
 					contexts: ["frame"],
 					title: "Save frame"
 				});
+				browser.menus.create({
+					id: "separator",
+					contexts: ["all"],
+					type: "separator"
+				});
+				browser.menus.create({
+					id: MENU_ID_AUTO_SAVE,
+					type: "checkbox",
+					title: "Auto-save this tab",
+					contexts: ["all"],
+					checked: false
+				});
 			} else {
 				await browser.menus.removeAll();
 			}
@@ -180,7 +206,7 @@ singlefile.ui = (() => {
 	function onTabEnd(tabId) {
 		refreshBadge(tabId, {
 			text: "OK",
-			color: [4, 229, 36, 255],
+			color: tabs[tabId].autoSave ? [255, 141, 1, 255] : [4, 229, 36, 255],
 			title: DEFAULT_TITLE,
 			path: DEFAULT_ICON_PATH,
 			progress: -1,
@@ -205,15 +231,15 @@ singlefile.ui = (() => {
 		delete tabs[tabId];
 	}
 
-	function onTabActivated(tabId, isActive) {
-		if (browser.browserAction && browser.browserAction.enable && browser.browserAction.disable) {
-			if (isActive) {
-				browser.browserAction.enable(tabId);
+	function onTabActivated(tab) {
+		if (isAllowedURL(tab.url) && browser.browserAction && browser.browserAction.enable && browser.browserAction.disable) {
+			if (isAllowedURL(tab.url)) {
+				browser.browserAction.enable(tab.id);
 				if (browser.runtime.lastError) {
 					/* ignored */
 				}
 			} else {
-				browser.browserAction.disable(tabId);
+				browser.browserAction.disable(tab.id);
 				if (browser.runtime.lastError) {
 					/* ignored */
 				}