Browse Source

disable autosave on unload as soon as the autosave is disabled or the corresponding option is unchecked

Gildas 7 years ago
parent
commit
6b3c0335e1

+ 33 - 7
extension/core/content/content-autosave-unload.js

@@ -18,16 +18,42 @@
  *   along with SingleFile.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-/* global browser, window, addEventListener, document, location, docHelper */
+/* global browser, window, addEventListener, removeEventListener, document, location, docHelper */
 
 this.singlefile.autoSave = this.singlefile.autoSave || (async () => {
 
-	const [isAutoSaveUnloadEnabled, options] = await Promise.all([browser.runtime.sendMessage({ isAutoSaveUnloadEnabled: true }), browser.runtime.sendMessage({ getConfig: true })]);
-	if (isAutoSaveUnloadEnabled) {
-		addEventListener("unload", () => {
-			const docData = docHelper.preProcessDoc(document, window, options);
-			browser.runtime.sendMessage({ processContent: true, content: docHelper.serialize(document), canvasData: docData.canvasData, emptyStyleRulesText: docData.emptyStyleRulesText, url: location.href });
-		});
+	let listenerAdded;
+	const [enabled, options] = await Promise.all([browser.runtime.sendMessage({ isAutoSaveUnloadEnabled: true }), browser.runtime.sendMessage({ getConfig: true })]);
+
+	enableAutoSaveUnload(enabled);
+
+	browser.runtime.onMessage.addListener(async message => {
+		if (message.autoSaveUnloadEnabled) {
+			refreshAutoSaveUnload();
+			return {};
+		}
+	});
+
+	async function refreshAutoSaveUnload() {
+		const enabled = await browser.runtime.sendMessage({ isAutoSaveUnloadEnabled: true });
+		enableAutoSaveUnload(enabled);
+	}
+
+	function enableAutoSaveUnload(enabled) {
+		if (enabled) {
+			if (!listenerAdded) {
+				addEventListener("unload", onUnload);
+				listenerAdded = true;
+			}
+		} else {
+			removeEventListener("unload", onUnload);
+			listenerAdded = false;
+		}
+	}
+
+	function onUnload() {
+		const docData = docHelper.preProcessDoc(document, window, options);
+		browser.runtime.sendMessage({ processContent: true, content: docHelper.serialize(document), canvasData: docData.canvasData, emptyStyleRulesText: docData.emptyStyleRulesText, url: location.href });
 	}
 
 })();

+ 4 - 1
extension/ui/bg/options.js

@@ -52,7 +52,10 @@
 		await update();
 	}, false);
 	maxResourceSizeEnabledInput.addEventListener("click", () => maxResourceSizeInput.disabled = !maxResourceSizeEnabledInput.checked, false);
-	autoSaveUnloadInput.addEventListener("click", () => autoSaveDelayInput.disabled = autoSaveUnloadInput.checked, false);
+	autoSaveUnloadInput.addEventListener("click", async () => {
+		autoSaveDelayInput.disabled = autoSaveUnloadInput.checked;
+		await bgPage.singlefile.ui.refreshAutoSaveUnload();
+	}, false);
 	document.body.onchange = update;
 	refresh();
 

+ 19 - 3
extension/ui/bg/ui.js

@@ -75,8 +75,8 @@ singlefile.ui = (() => {
 	browser.runtime.onMessage.addListener(async (request, sender) => {
 		if (request.processProgress) {
 			if (request.maxIndex) {
-			onTabProgress(sender.tab.id, request.index, request.maxIndex, request.options);
-		}
+				onTabProgress(sender.tab.id, request.index, request.maxIndex, request.options);
+			}
 			return {};
 		}
 		if (request.processEnd) {
@@ -97,7 +97,8 @@ singlefile.ui = (() => {
 	return {
 		update: refreshContextMenu,
 		onTabProgress,
-		onTabEnd
+		onTabEnd,
+		refreshAutoSaveUnload
 	};
 
 	async function initContextMenu() {
@@ -132,6 +133,7 @@ singlefile.ui = (() => {
 					}
 					tabsData[tab.id].autoSave = event.checked;
 					await browser.storage.local.set({ tabsData });
+					await refreshAutoSaveUnload();
 					refreshBadgeState(tab, { autoSave: true });
 				}
 				if (event.menuItemId == MENU_ID_AUTO_SAVE_DISABLED) {
@@ -139,18 +141,21 @@ singlefile.ui = (() => {
 					Object.keys(tabsData).forEach(tabId => tabsData[tabId].autoSave = false);
 					tabsData.autoSaveUnpinned = tabsData.autoSaveAll = false;
 					await browser.storage.local.set({ tabsData });
+					await refreshAutoSaveUnload();
 					refreshBadgeState(tab, { autoSave: false });
 				}
 				if (event.menuItemId == MENU_ID_AUTO_SAVE_ALL) {
 					const tabsData = await getPersistentTabsData();
 					tabsData.autoSaveAll = event.checked;
 					await browser.storage.local.set({ tabsData });
+					await refreshAutoSaveUnload();
 					refreshBadgeState(tab, { autoSave: true });
 				}
 				if (event.menuItemId == MENU_ID_AUTO_SAVE_UNPINNED) {
 					const tabsData = await getPersistentTabsData();
 					tabsData.autoSaveUnpinned = event.checked;
 					await browser.storage.local.set({ tabsData });
+					await refreshAutoSaveUnload();
 					refreshBadgeState(tab, { autoSave: true });
 				}
 			});
@@ -252,6 +257,17 @@ singlefile.ui = (() => {
 		}
 	}
 
+	async function refreshAutoSaveUnload() {
+		const tabs = await browser.tabs.query({});
+		return Promise.all(tabs.map(async tab => {
+			try {
+				await browser.tabs.sendMessage(tab.id, { autoSaveUnloadEnabled: true });
+			} catch (error) {
+				/* ignored */
+			}
+		}));
+	}
+
 	async function processTab(tab, options = {}) {
 		const tabId = tab.id;
 		try {