Skip to content

Commit

Permalink
Reworked initialization process for handle session restore and sideba…
Browse files Browse the repository at this point in the history
…r show event
  • Loading branch information
MurzNN committed Jan 5, 2019
1 parent 8f8bced commit 350f01b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
============================================================================================

Expand Down
59 changes: 52 additions & 7 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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);
6 changes: 3 additions & 3 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},

Expand Down

0 comments on commit 350f01b

Please sign in to comment.