From 1f8fb529d64e92c91b1024c25b032d28cc0a6fee Mon Sep 17 00:00:00 2001 From: s2 Date: Tue, 12 Dec 2017 22:28:29 +0100 Subject: [PATCH] fix icons --- src/bg.js | 26 +++++++++++++++++--------- src/manifest.json | 2 +- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/bg.js b/src/bg.js index 875293e..672e20c 100644 --- a/src/bg.js +++ b/src/bg.js @@ -1,5 +1,6 @@ const kTST_ID = 'treestyletab@piro.sakura.ne.jp'; const MY_EXTENSION_NAME = 'tab-groupcolor'; +let tabIcons = {}; var changeIcon = function(color, currentIcon, options) { var canvas = document.createElement('canvas'); @@ -58,21 +59,21 @@ var registerToTST = async function() { }; //returns the index of the tabs array object that contains the tab with the passed id -var getTabParent = async (tabs, id, currentTopLevelTabPos) => { +var getTabParent = (tabs, id, currentTopLevelTabPos) => { for (let i = 0; i < tabs.length; i++) { //on a root node because there is no opener - if (typeof(tabs[i].openerTabId) === 'undefined' || tabs[i].openerTabId === tabs[i].id) { + if (!tabs[i].openerTabId || typeof(tabs[i].openerTabId) === 'undefined' || tabs[i].openerTabId === tabs[i].id) { currentTopLevelTabPos = i; } if (tabs[i].id === id) { //get original favicon for this tab - let orginalFavIconUrl = await browser.sessions.getTabValue(id, 'orginalFavIconUrl'); + let orginalFavIconUrl = tabIcons[id]; if (!orginalFavIconUrl) { orginalFavIconUrl = tabs[i].favIconUrl; - await browser.sessions.setTabValue(id, 'orginalFavIconUrl', orginalFavIconUrl); + tabIcons[id] = orginalFavIconUrl; } return { @@ -81,7 +82,7 @@ var getTabParent = async (tabs, id, currentTopLevelTabPos) => { }; } else { if (tabs[i].children && tabs[i].children.length > 0) { - let ret = await getTabParent(tabs[i].children, id, currentTopLevelTabPos); + let ret = getTabParent(tabs[i].children, id, currentTopLevelTabPos); if (ret) { return ret; } @@ -95,7 +96,7 @@ var updateAllColorsOnAllTabs = async () => { allWindows.forEach(async w => { var tstTabs = await browser.runtime.sendMessage(kTST_ID, { type: 'get-tree', - window: w.id + window: w.id }); var changeTabs = async (tabs) => { @@ -103,7 +104,7 @@ var updateAllColorsOnAllTabs = async () => { for (let i = 0; i < tabs.length; i++) { if (tabs[i].status === 'complete') { - let t = await getTabParent(tstTabs, tabs[i].id); + let t = getTabParent(tstTabs, tabs[i].id); let options = await loadOptions(); browser.tabs.executeScript(tabs[i].id, { @@ -137,13 +138,20 @@ browser.runtime.onMessageExternal.addListener((aMessage, aSender) => { }); //when something happens with a tab -let tabEvents = ['onUpdated', 'onAttached', 'onDetached', 'onMoved', 'onRemoved'] +let tabEvents = ['onAttached', 'onDetached', 'onMoved', 'onRemoved'] tabEvents.forEach((ev) => { - browser.tabs[ev].addListener(async (tabId, changeInfo, tab) => { + browser.tabs[ev].addListener((tabId, changeInfo, tab) => { updateAllColorsOnAllTabs(); }); }); +//when a tab reloads +browser.webNavigation.onCompleted.addListener((details) => { + delete tabIcons[details.tabId]; + updateAllColorsOnAllTabs(); +}); + + //when the options are updated browser.storage.onChanged.addListener((changes) => { updateAllColorsOnAllTabs(); diff --git a/src/manifest.json b/src/manifest.json index 81cc045..e31fa7a 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -18,7 +18,7 @@ }, "permissions": [ "storage", - "sessions", + "webNavigation", "" ], "options_ui": {