Skip to content

Commit 288f45c

Browse files
committed
Version 0.2
1 parent 6d27358 commit 288f45c

File tree

5 files changed

+195
-240
lines changed

5 files changed

+195
-240
lines changed

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Changes in [0.2] (2019-01-05)
2+
============================================================================================
3+
4+
* Colorize all tabs on extension initialization.
5+
6+
* Fixed bug with not changing tab color when return to old domain.
7+
8+
* Added Options page with default options
9+
10+
* Allow customize CSS styles for all tabs, active and hovered tabs.
11+
12+
* Use simpler and quicker domain hash function
13+
14+
Changes in [0.1] (2019-01-04)
15+
======================================================================================================
16+
17+
* Initial version

background.js

Lines changed: 94 additions & 206 deletions
Original file line numberDiff line numberDiff line change
@@ -1,250 +1,138 @@
11
"use strict";
22

33
const TST_ID = "treestyletab@piro.sakura.ne.jp";
4-
// const DEFAULT_SETTINGS = {
5-
// };
6-
7-
// The current settings
8-
// let settings;
94

5+
const DEFAULT_SETTINGS = {
6+
saturation: 60,
7+
lightness: 70,
8+
css: '',
9+
active: {
10+
saturation: 60,
11+
lightness: 60,
12+
css: 'font-weight: bold;',
13+
},
14+
hover: {
15+
saturation: 60,
16+
lightness: 65,
17+
css: '',
18+
},
19+
};
1020

1121
var ColoredTabs = {
1222
init() {
13-
// console.log("colorizeTab init");
23+
browser.storage.sync.get(DEFAULT_SETTINGS).then(function(settings) {
24+
ColoredTabs.settings = settings;
25+
ColoredTabs.state.hashUsed = [];
26+
ColoredTabs.colorizeAllTabs();
1427
browser.tabs.onUpdated.addListener(ColoredTabs.checkTabChanges);
15-
// ColoredTabs.registerToTST();
28+
console.log("colorizeTab inited");
29+
ColoredTabs.state.inited = true;
30+
});
1631
},
1732

1833
checkTabChanges(tabId, changeInfo, tab) {
19-
// console.log("colorizeTab checkTabChanges tab id = " + tabId + " status " + changeInfo.status + " url " + changeInfo.url);
20-
if (typeof changeInfo.url === 'undefined') {
21-
return;
22-
}
23-
// console.log("colorizeTab checkTabChanges tab id = " + tabId + " url" );
24-
let host = new URL(tab.url);
25-
host = host.hostname.toString();
26-
ColoredTabs.colorizeTab(tabId, host);
34+
if(typeof changeInfo.url === 'undefined') {
35+
return;
36+
}
37+
let host = new URL(tab.url);
38+
host = host.hostname.toString();
39+
ColoredTabs.colorizeTab(tabId, host);
40+
},
41+
42+
colorizeAllTabs() {
43+
ColoredTabs.state.css = '';
44+
if(ColoredTabs.settings.active.css !== '') {
45+
ColoredTabs.state.css = ColoredTabs.state.css + `
46+
.tab.active {
47+
` + ColoredTabs.settings.active.css + `
48+
}
49+
`;
50+
}
51+
if(ColoredTabs.settings.hover.css !== '') {
52+
ColoredTabs.state.css = ColoredTabs.state.css + `
53+
.tab:hover {
54+
` + ColoredTabs.settings.hover.css + `
55+
}
56+
`;
57+
}
58+
browser.runtime.sendMessage(TST_ID, {
59+
type: "register-self",
60+
style: ColoredTabs.state.css,
61+
});
62+
63+
browser.tabs.query({}).then(function(tabs){
64+
for (let tab of tabs) {
65+
let host = new URL(tab.url);
66+
host = host.hostname.toString();
67+
console.log('colorize tab id ' + tab.id + ' host ' + host);
68+
ColoredTabs.colorizeTab(tab.id, host);
69+
}
70+
}, onError);
71+
console.log('colorizeAllTabs() fin');
2772
},
2873

2974
colorizeTab(tabId, host, oldHost = null) {
3075

31-
let hash = Math.abs(ColoredTabs.hostHash(host)) % 360;
76+
let hash = ColoredTabs.hash(host) % 360;
3277

33-
// let sat = await getOption("saturation");
34-
// let lum = await getOption("lightness");
78+
if(typeof ColoredTabs.state.tabHash[tabId] !== 'undefined') {
79+
browser.runtime.sendMessage(TST_ID, {
80+
type: 'remove-tab-state',
81+
tabs: [tabId],
82+
state: 'coloredTabs' + ColoredTabs.state.tabHash[tabId],
83+
});
84+
}
3585

36-
if(typeof ColoredTabs.settings.hashColors[hash] === 'undefined') {
86+
if(typeof ColoredTabs.state.hashUsed[hash] === 'undefined') {
3787

38-
ColoredTabs.settings.hashColors[hash] = true;
88+
ColoredTabs.state.hashUsed[hash] = true;
3989
let hashColor = 'hsl(' + hash + ',' + ColoredTabs.settings.saturation + '%,' + ColoredTabs.settings.lightness + '%)';
40-
let hashColorHover = 'hsl(' + hash + ',' + '100%,' + ColoredTabs.settings.lightness + '%)';
90+
let hashColorHover = 'hsl(' + hash + ',' + ColoredTabs.settings.hover.saturation + '%,' + ColoredTabs.settings.hover.lightness + '%)';
91+
let hashColorActive = 'hsl(' + hash + ',' + ColoredTabs.settings.active.saturation + '%,' + ColoredTabs.settings.active.lightness + '%)';
4192

42-
ColoredTabs.settings.css = ColoredTabs.settings.css + `
93+
ColoredTabs.state.css = ColoredTabs.state.css + `
4394
.tab.coloredTabs` + hash + ` {
4495
background-color: ` + hashColor + `;
4596
}
4697
.tab.coloredTabs` + hash + `:hover {
4798
background-color: ` + hashColorHover + `;
4899
}
100+
.tab.coloredTabs` + hash + `.active {
101+
background-color: ` + hashColorActive + `;
102+
}
49103
`;
50104

51105
browser.runtime.sendMessage(TST_ID, {
52-
type: "register-self",
53-
style: ColoredTabs.settings.css,
106+
type: "register-self",
107+
style: ColoredTabs.state.css,
54108
});
55-
// console.log(ColoredTabs.settings.hashColors);
56-
console.log(ColoredTabs.settings.css);
57109
}
58-
// hash = Math.floor(Math.random() * (3 - 1 + 1)) + 1;
59110

60-
// console.log("colorizeTab tab id = " + tabId + " host = " + host + " hash " + hash + " style coloredTabs" + hash);
61-
// browser.runtime.sendMessage(TST_ID, {
62-
// type: 'register-self',
63-
// style: `
64-
// .tab.coloredTabs` + hash + ` {
65-
// color: red;
66-
// }
67-
// `
68-
// });
69-
70111
browser.runtime.sendMessage(TST_ID, {
71112
type: 'add-tab-state',
72113
tabs: [tabId],
73114
state: 'coloredTabs' + hash,
74115
});
75-
116+
ColoredTabs.state.tabHash[tabId] = hash;
76117
},
77118

78-
// registerToTST() {
79-
// try {
80-
// const self = browser.management.getSelf();
81-
// // browser.runtime.sendMessage(TST_ID, {
82-
// // type: "register-self",
83-
// // style: `
84-
// // .tab.style1 {
85-
// // background-color: red !important;
86-
// // border: 2px solid green !important;
87-
// // }
88-
// // .tab.style2 {
89-
// // background-color: blue !important;
90-
// // border: 5px solid yellow !important;
91-
// // }
92-
// // .tab.style3 {
93-
// // background-color: green !important;
94-
// // border: 5px solid blue !important;
95-
// // }
96-
// // `
97-
// // });
98-
// } catch(e) {
99-
// // Could not register
100-
// console.log("Could not register to TST")
101-
// return false;
102-
// }
103-
//
104-
// return true;
105-
// },
106-
107-
hostHash(host) {
108-
var hash = ColoredTabs.sha256(host);
109-
var iClr, clrConst = 5381;
110-
for (iClr = 0; iClr < hash.length; iClr++) {
111-
clrConst = ((clrConst << 5) + clrConst) + hash.charCodeAt(iClr);
112-
}
113-
return clrConst;
114-
},
115-
// Generate a unique hash for a wider color spectrum
116-
sha256(s) {
117-
118-
var chrsz = 8;
119-
var hexcase = 0;
120-
121-
function safe_add(x, y) {
122-
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
123-
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
124-
return (msw << 16) | (lsw & 0xFFFF);
125-
}
126-
127-
function S(X, n) {
128-
return (X >>> n) | (X << (32 - n));
129-
}
130-
131-
function R(X, n) {
132-
return (X >>> n);
133-
}
134-
135-
function Ch(x, y, z) {
136-
return ((x & y) ^ ((~x) & z));
137-
}
138-
139-
function Maj(x, y, z) {
140-
return ((x & y) ^ (x & z) ^ (y & z));
141-
}
142-
143-
function Sigma0256(x) {
144-
return (S(x, 2) ^ S(x, 13) ^ S(x, 22));
145-
}
146-
147-
function Sigma1256(x) {
148-
return (S(x, 6) ^ S(x, 11) ^ S(x, 25));
149-
}
150-
151-
function Gamma0256(x) {
152-
return (S(x, 7) ^ S(x, 18) ^ R(x, 3));
153-
}
154-
155-
function Gamma1256(x) {
156-
return (S(x, 17) ^ S(x, 19) ^ R(x, 10));
157-
}
158-
159-
function core_sha256(m, l) {
160-
var K = new Array(0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 0xE49B69C1, 0xEFBE4786, 0xFC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA, 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x6CA6351, 0x14292967, 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070, 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2);
161-
var HASH = new Array(0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19);
162-
var W = new Array(64);
163-
var a, b, c, d, e, f, g, h, i, j;
164-
var T1, T2;
165-
m[l >> 5] |= 0x80 << (24 - l % 32);
166-
m[((l + 64 >> 9) << 4) + 15] = l;
167-
for (var i = 0; i < m.length; i += 16) {
168-
a = HASH[0];
169-
b = HASH[1];
170-
c = HASH[2];
171-
d = HASH[3];
172-
e = HASH[4];
173-
f = HASH[5];
174-
g = HASH[6];
175-
h = HASH[7];
176-
for (var j = 0; j < 64; j++) {
177-
if (j < 16) W[j] = m[j + i];
178-
else W[j] = safe_add(safe_add(safe_add(Gamma1256(W[j - 2]), W[j - 7]), Gamma0256(W[j - 15])), W[j - 16]);
179-
T1 = safe_add(safe_add(safe_add(safe_add(h, Sigma1256(e)), Ch(e, f, g)), K[j]), W[j]);
180-
T2 = safe_add(Sigma0256(a), Maj(a, b, c));
181-
h = g;
182-
g = f;
183-
f = e;
184-
e = safe_add(d, T1);
185-
d = c;
186-
c = b;
187-
b = a;
188-
a = safe_add(T1, T2);
189-
}
190-
HASH[0] = safe_add(a, HASH[0]);
191-
HASH[1] = safe_add(b, HASH[1]);
192-
HASH[2] = safe_add(c, HASH[2]);
193-
HASH[3] = safe_add(d, HASH[3]);
194-
HASH[4] = safe_add(e, HASH[4]);
195-
HASH[5] = safe_add(f, HASH[5]);
196-
HASH[6] = safe_add(g, HASH[6]);
197-
HASH[7] = safe_add(h, HASH[7]);
198-
}
199-
return HASH;
200-
}
201-
202-
function str2binb(str) {
203-
var bin = Array();
204-
var mask = (1 << chrsz) - 1;
205-
for (var i = 0; i < str.length * chrsz; i += chrsz) {
206-
bin[i >> 5] |= (str.charCodeAt(i / chrsz) & mask) << (24 - i % 32);
207-
}
208-
return bin;
209-
}
210-
211-
function Utf8Encode(string) {
212-
string = string.replace(/\r\n/g, "\n");
213-
var utftext = "";
214-
for (var n = 0; n < string.length; n++) {
215-
var c = string.charCodeAt(n);
216-
if (c < 128) {
217-
utftext += String.fromCharCode(c);
218-
} else if ((c > 127) && (c < 2048)) {
219-
utftext += String.fromCharCode((c >> 6) | 192);
220-
utftext += String.fromCharCode((c & 63) | 128);
221-
} else {
222-
utftext += String.fromCharCode((c >> 12) | 224);
223-
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
224-
utftext += String.fromCharCode((c & 63) | 128);
225-
}
226-
}
227-
return utftext;
228-
}
229-
230-
function binb2hex(binarray) {
231-
var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
232-
var str = "";
233-
for (var i = 0; i < binarray.length * 4; i++) {
234-
str += hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8 + 4)) & 0xF) +
235-
hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8)) & 0xF);
236-
}
237-
return str;
238-
}
239-
s = Utf8Encode(s);
240-
return binb2hex(core_sha256(str2binb(s), s.length * chrsz));
119+
hash(s) {
120+
for(var i=0, h=1; i<s.length; i++)
121+
h=Math.imul(h+s.charCodeAt(i)|0, 2654435761);
122+
return (h^h>>>17)>>>0;
241123
},
242124

243-
settings: {
244-
'saturation': 60,
245-
'lightness': 70,
246-
'hashColors': {},
125+
state: {
126+
'hashUsed': {},
127+
'tabHash': {},
128+
'inited': false,
247129
},
248130
}
249131

250-
ColoredTabs.init();
132+
if(ColoredTabs.state.inited != true) {
133+
ColoredTabs.init();
134+
}
135+
136+
function onError(error) {
137+
console.log(`Error: ${error}`);
138+
}

manifest.json

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

0 commit comments

Comments
 (0)