-
Notifications
You must be signed in to change notification settings - Fork 5
/
background.js
100 lines (81 loc) · 2.2 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var env = []
var theme_before_envify = {}
var ff_version = '';
async function start() {
theme_before_envify = await browser.theme.getCurrent();
ff_version = parseInt(window.navigator.userAgent.split('/').pop().split('.')[0], 10);
loadEnv()
setupListeners();
}
start();
function loadEnv() {
browser.storage.sync.get("environments")
.then(function(results){
env = []
let { environments } = results;
if (environments == undefined) {
return
}
Object.keys(environments).map(function(value){
env.push( { match: value, color: environments[value] });
});
env.sort(function(a, b) {
return a.match.length - b.match.length
})
})
}
function setupListeners() {
browser.storage.onChanged.addListener(loadEnv)
browser.runtime.onInstalled.addListener(function(details) {
browser.storage.sync.get("environments")
.then(function(results) {
if (!("environments" in results)) {
browser.storage.sync.set({"environments": { "": ""} })
browser.runtime.openOptionsPage()
}
})
})
browser.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (tab.active) {
setTabColor(tab)
}
})
browser.tabs.onActivated.addListener(function(activeInfo) {
browser.tabs.get(activeInfo.tabId).then(function(tab) {
setTabColor(tab)
})
})
}
function setTabColor(tab) {
var url = tab.url
var color = getColorFromUrl(env, url)
if (color) {
browser.theme.update(tab.windowId, generateThemeFromColor(color))
}
else {
browser.theme.update(tab.windowId, theme_before_envify)
}
}
function generateThemeFromColor(color) {
// "theme_frame" // >= 70
// "frame": colorLuminance(color, -0.3), // >= 70
// "tab_background_ext": invertColor(color, true), // >= 70
var theme = {
"images": { },
"colors": {
"toolbar": color
}
};
var headerURL = "headerURL";
var accentcolor = "accentcolor";
var textcolor = "textcolor";
if (ff_version >= 70) {
headerURL = "theme_frame";
accentcolor = "frame";
textcolor = "tab_background_text";
}
theme["images"][headerURL] = "";
theme["colors"][accentcolor] = colorLuminance(color, -0.3);
theme["colors"][textcolor] = invertColor(color, true);
return theme;
}