Browse Source

moved brower.tabs listeners into tabs.js

Gildas 7 years ago
parent
commit
ad4c272cd8

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

@@ -22,7 +22,6 @@
 
 singlefile.autosave = (() => {
 
-	browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => onTabUpdated(tabId, changeInfo, tab));
 	browser.runtime.onMessage.addListener((message, sender) => {
 		if (message.isAutoSaveEnabled) {
 			return isAutoSaveEnabled(sender.tab);
@@ -50,6 +49,7 @@ singlefile.autosave = (() => {
 	}
 
 	return {
+		onTabUpdated,
 		enabled,
 		refresh
 	};

+ 1 - 1
extension/core/bg/tabs-data.js

@@ -23,9 +23,9 @@
 singlefile.tabsData = (() => {
 
 	let persistentData, temporaryData;
-	browser.tabs.onRemoved.addListener(tabId => onTabRemoved(tabId));
 	getPersistent().then(tabsData => persistentData = tabsData);
 	return {
+		onTabRemoved,
 		getTemporary,
 		get: getPersistent,
 		set: setPersistent

+ 49 - 0
extension/core/bg/tabs.js

@@ -0,0 +1,49 @@
+/*
+ * Copyright 2010-2019 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, singlefile */
+
+singlefile.tabs = (() => {
+
+	browser.tabs.onCreated.addListener(tab => onTabCreated(tab));
+	browser.tabs.onActivated.addListener(activeInfo => onTabActivated(activeInfo));
+	browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => onTabUpdated(tabId, changeInfo, tab));
+	browser.tabs.onRemoved.addListener(tabId => onTabRemoved(tabId));
+
+	function onTabCreated(tab) {
+		singlefile.ui.button.onTabCreated(tab);
+		singlefile.ui.menu.onTabCreated(tab);
+	}
+
+	async function onTabActivated(activeInfo) {
+		const tab = await browser.tabs.get(activeInfo.tabId);
+		singlefile.ui.menu.onTabActivated(tab, activeInfo);
+	}
+
+	function onTabUpdated(tabId, changeInfo, tab) {
+		singlefile.autosave.onTabUpdated(tabId, changeInfo, tab);
+		singlefile.ui.menu.onTabActivated(tabId, changeInfo, tab);
+	}
+
+	function onTabRemoved(tabId) {
+		singlefile.tabsData.onTabRemoved(tabId);
+	}
+
+})();

+ 6 - 8
extension/ui/bg/ui-button.js

@@ -37,9 +37,6 @@ singlefile.ui.button = (() => {
 			}
 		}
 	});
-	browser.tabs.onActivated.addListener(activeInfo => onTabActivated(activeInfo));
-	browser.tabs.onCreated.addListener(tab => onTabCreated(tab));
-	browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => onTabActivated(tab));
 	browser.runtime.onMessage.addListener((request, sender) => {
 		if (request.processReset) {
 			onReset(sender.tab.id);
@@ -64,6 +61,8 @@ singlefile.ui.button = (() => {
 	});
 
 	return {
+		onTabCreated,
+		onTabUpdated,
 		onInitialize,
 		onProgress,
 		onEnd,
@@ -76,14 +75,13 @@ singlefile.ui.button = (() => {
 		}
 	};
 
-	async function onTabActivated(activeInfo) {
-		const tab = await browser.tabs.get(activeInfo.tabId);
-		onActivated(tab);
+	function onTabUpdated(tabId, changeInfo, tab) {
+		refreshTab(tab);
 	}
 
 	async function onTabCreated(tab) {
 		await refreshProperty(tab.id, "setBadgeBackgroundColor", { color: DEFAULT_COLOR });
-		onActivated(tab);
+		refreshTab(tab);
 	}
 
 	function onReset(tabId) {
@@ -116,7 +114,7 @@ singlefile.ui.button = (() => {
 		refresh(tabId, getProperties(options, "", [4, 229, 36, 255], browser.i18n.getMessage("buttonSaveProgressTooltip") + (progress * 5) + "%", path, [128, 128, 128, 255]));
 	}
 
-	async function onActivated(tab) {
+	async function refreshTab(tab) {
 		const autoSave = await singlefile.autosave.enabled(tab.id);
 		const properties = getCurrentProperties(tab.id, { autoSave });
 		await refresh(tab.id, properties, true);

+ 4 - 4
extension/ui/bg/ui-menu.js

@@ -46,14 +46,14 @@ singlefile.ui.menu = (() => {
 
 	let profileIndexes = new Map();
 	initialize();
-	browser.tabs.onActivated.addListener(activeInfo => onTabActivated(activeInfo));
-	browser.tabs.onCreated.addListener(tab => refreshTab(tab));
 	return {
+		onTabCreated: refreshTab,
+		onTabActivated: refreshTab,
+		onTabUpdated: onTabUpdated,
 		refresh
 	};
 
-	async function onTabActivated(activeInfo) {
-		const tab = await browser.tabs.get(activeInfo.tabId);
+	function onTabUpdated(tabId, changeInfo, tab) {
 		refreshTab(tab);
 	}
 

+ 1 - 0
manifest.json

@@ -56,6 +56,7 @@
 			"extension/core/bg/download.js",
 			"extension/core/bg/runner.js",
 			"extension/core/bg/autosave.js",
+			"extension/core/bg/tabs.js",
 			"extension/core/bg/config.js",
 			"extension/core/bg/core.js",
 			"extension/ui/bg/bg-ui.js",