Skip to content

Commit 350f01b

Browse files
committed
Reworked initialization process for handle session restore and sidebar show event
1 parent 8f8bced commit 350f01b

3 files changed

Lines changed: 60 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Changes in [0.3] (2019-01-05)
2+
============================================================================================
3+
4+
* Reworked initialization process for handle session restore and sidebar show event.
5+
16
Changes in [0.2] (2019-01-05)
27
============================================================================================
38

background.js

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,25 @@ const DEFAULT_SETTINGS = {
2020

2121
var ColoredTabs = {
2222
init() {
23+
ColoredTabs.state.inited = true;
2324
browser.storage.sync.get(DEFAULT_SETTINGS).then(function(settings) {
2425
ColoredTabs.settings = settings;
2526
ColoredTabs.state.hashUsed = [];
2627
ColoredTabs.colorizeAllTabs();
2728
browser.tabs.onUpdated.addListener(ColoredTabs.checkTabChanges);
28-
console.log("colorizeTab inited");
29-
ColoredTabs.state.inited = true;
29+
browser.tabs.onCreated.addListener(ColoredTabs.handleCreated);
3030
});
3131
},
3232

33+
handleCreated(tab) {
34+
console.log("tab url " + tab.url);
35+
if(tab.url.indexOf('about:') === 0)
36+
return;
37+
let host = new URL(tab.url);
38+
host = host.hostname.toString();
39+
ColoredTabs.colorizeTab(tab.id, host);
40+
},
41+
3342
checkTabChanges(tabId, changeInfo, tab) {
3443
if(typeof changeInfo.url === 'undefined') {
3544
return;
@@ -72,7 +81,7 @@ var ColoredTabs = {
7281
},
7382

7483
colorizeTab(tabId, host, oldHost = null) {
75-
84+
console.log("colorizeTab tabId " + tabId + ", host " + host);
7685
let hash = ColoredTabs.hash(host) % 360;
7786

7887
if(typeof ColoredTabs.state.tabHash[tabId] !== 'undefined') {
@@ -129,10 +138,46 @@ var ColoredTabs = {
129138
},
130139
}
131140

132-
if(ColoredTabs.state.inited != true) {
133-
ColoredTabs.init();
134-
}
135-
136141
function onError(error) {
137142
console.log(`Error: ${error}`);
138143
}
144+
145+
async function registerToTST() {
146+
try {
147+
const self = await browser.management.getSelf();
148+
await browser.runtime.sendMessage(TST_ID, {
149+
type: "register-self",
150+
name: self.id,
151+
listeningTypes: ["ready", "sidebar-hide", "sidebar-show"],
152+
});
153+
} catch(e) {
154+
// Could not register
155+
console.log("Tree Style Tab extension needed for TST Colored Tabs, but can't be detected. Please install or enable it.")
156+
return false;
157+
}
158+
159+
return true;
160+
}
161+
162+
async function handleTSTMessage(message, sender) {
163+
if(sender.id !== TST_ID) {
164+
return;
165+
}
166+
167+
switch (message.type) {
168+
case "ready":
169+
registerToTST();
170+
case "sidebar-show":
171+
if(ColoredTabs.state.inited != true) {
172+
console.log('TST ready, initializing ColoredTabs');
173+
ColoredTabs.init();
174+
} else {
175+
console.log('ColoredTabs already inited');
176+
ColoredTabs.colorizeAllTabs();
177+
}
178+
break;
179+
}
180+
}
181+
182+
registerToTST();
183+
browser.runtime.onMessageExternal.addListener(handleTSTMessage);

manifest.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
"name": "TST Colored Tabs",
44
"short_name": "TSTColoredTabs",
55
"description": "Colorize tabs in Tree Style Tab based on hostname.",
6-
"version": "0.2",
6+
"version": "0.3",
77
"author": "Alexey Murz Korepov",
88
"homepage_url": "https://github.com/MurzNN/tst-colored-tabs",
99

1010
"applications": {
1111
"gecko": {
12-
"id": "tst-colored-tabs@murz"
13-
"strict_min_version": "57.0",
12+
"id": "tst-colored-tabs@murz",
13+
"strict_min_version": "57.0"
1414
}
1515
},
1616

0 commit comments

Comments
 (0)