From 350f01b17d776fed1433b1ae9d5903255b71319d Mon Sep 17 00:00:00 2001 From: Alexey Murz Korepov Date: Sat, 5 Jan 2019 23:29:44 +0300 Subject: [PATCH] Reworked initialization process for handle session restore and sidebar show event --- CHANGELOG.md | 5 +++++ background.js | 59 +++++++++++++++++++++++++++++++++++++++++++++------ manifest.json | 6 +++--- 3 files changed, 60 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae91b60..e83a3c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +Changes in [0.3] (2019-01-05) +============================================================================================ + + * Reworked initialization process for handle session restore and sidebar show event. + Changes in [0.2] (2019-01-05) ============================================================================================ diff --git a/background.js b/background.js index 6bf71bc..16dee10 100644 --- a/background.js +++ b/background.js @@ -20,16 +20,25 @@ const DEFAULT_SETTINGS = { var ColoredTabs = { init() { + ColoredTabs.state.inited = true; browser.storage.sync.get(DEFAULT_SETTINGS).then(function(settings) { ColoredTabs.settings = settings; ColoredTabs.state.hashUsed = []; ColoredTabs.colorizeAllTabs(); browser.tabs.onUpdated.addListener(ColoredTabs.checkTabChanges); - console.log("colorizeTab inited"); - ColoredTabs.state.inited = true; + browser.tabs.onCreated.addListener(ColoredTabs.handleCreated); }); }, + handleCreated(tab) { + console.log("tab url " + tab.url); + if(tab.url.indexOf('about:') === 0) + return; + let host = new URL(tab.url); + host = host.hostname.toString(); + ColoredTabs.colorizeTab(tab.id, host); + }, + checkTabChanges(tabId, changeInfo, tab) { if(typeof changeInfo.url === 'undefined') { return; @@ -72,7 +81,7 @@ var ColoredTabs = { }, colorizeTab(tabId, host, oldHost = null) { - + console.log("colorizeTab tabId " + tabId + ", host " + host); let hash = ColoredTabs.hash(host) % 360; if(typeof ColoredTabs.state.tabHash[tabId] !== 'undefined') { @@ -129,10 +138,46 @@ var ColoredTabs = { }, } -if(ColoredTabs.state.inited != true) { - ColoredTabs.init(); -} - function onError(error) { console.log(`Error: ${error}`); } + +async function registerToTST() { + try { + const self = await browser.management.getSelf(); + await browser.runtime.sendMessage(TST_ID, { + type: "register-self", + name: self.id, + listeningTypes: ["ready", "sidebar-hide", "sidebar-show"], + }); + } catch(e) { + // Could not register + console.log("Tree Style Tab extension needed for TST Colored Tabs, but can't be detected. Please install or enable it.") + return false; + } + + return true; +} + +async function handleTSTMessage(message, sender) { + if(sender.id !== TST_ID) { + return; + } + + switch (message.type) { + case "ready": + registerToTST(); + case "sidebar-show": + if(ColoredTabs.state.inited != true) { + console.log('TST ready, initializing ColoredTabs'); + ColoredTabs.init(); + } else { + console.log('ColoredTabs already inited'); + ColoredTabs.colorizeAllTabs(); + } + break; + } +} + +registerToTST(); +browser.runtime.onMessageExternal.addListener(handleTSTMessage); diff --git a/manifest.json b/manifest.json index 2114856..6e59346 100644 --- a/manifest.json +++ b/manifest.json @@ -3,14 +3,14 @@ "name": "TST Colored Tabs", "short_name": "TSTColoredTabs", "description": "Colorize tabs in Tree Style Tab based on hostname.", - "version": "0.2", + "version": "0.3", "author": "Alexey Murz Korepov", "homepage_url": "https://github.com/MurzNN/tst-colored-tabs", "applications": { "gecko": { - "id": "tst-colored-tabs@murz" - "strict_min_version": "57.0", + "id": "tst-colored-tabs@murz", + "strict_min_version": "57.0" } },