-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
144 lines (122 loc) · 4.76 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2019 Johannes Marbach
//
// This file is part of Wallabaggerini, hereafter referred to as the program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
(() => {
let isDarkTheme = false;
let tabIdsInSyncWithTheme = new Set([]);
browser.pageAction.onClicked.addListener(handlePageAction)
// Try to explicitly show the page action on Android (doesn't seem to work with
// the manifest matches definition alone)
browser.runtime.getPlatformInfo().then(platformInfo => {
if (platformInfo.os === 'android') {
browser.tabs.onActivated.addListener(handleActivated)
}
})
browser.theme.getCurrent().then(theme => handleThemeChanged(theme));
browser.theme.onUpdated.addListener(updateInfo => handleThemeChanged(updateInfo.theme));
browser.tabs.onActivated.addListener(activeInfo => handleTabActivated(activeInfo));
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => handleTabUpdated(tabId, changeInfo, tab));
async function handlePageAction(tab) {
let settings = await loadSettings()
if (!settings.installationUrl) {
browser.runtime.openOptionsPage()
return
}
browser.tabs.create({ url: `${settings.installationUrl}/bookmarklet?url=${encodeURIComponent(tab.url)}` })
}
async function loadSettings() {
let result = await browser.storage.sync.get(['installationUrl'])
return {
installationUrl: result.hasOwnProperty('installationUrl') ? result.installationUrl : ''
}
}
function handleActivated(activeInfo) {
browser.pageAction.show(activeInfo.tabId)
}
function handleThemeChanged(theme) {
const isDarkThemeNew = checkIsDarkTheme(theme);
if (isDarkThemeNew === isDarkTheme) {
return;
}
isDarkTheme = isDarkThemeNew;
tabIdsInSyncWithTheme.clear();
}
function checkIsDarkTheme(theme) {
return theme?.colors?.frame && colorIsDark(colorToRgb(theme.colors.frame));
}
function colorIsDark(rgb) {
return rgb[0] + rgb[1] + rgb[2] < 3 * 127;
}
function colorToRgb(color) {
if (color.charAt(0) === "#") {
return [parseInt(color.substring(1, 3), 16), parseInt(color.substring(3, 5), 16), parseInt(color.substring(5, 7), 16)];
}
if (color.indexOf("rgb") >= 0) {
return color.replace(/^(rgb|rgba)\(/, '').replace(/\)$/, '').replace(/\s/g, '').split(',').map(c => parseInt(c));
}
if (color.indexOf("hsl") >= 0) {
const hsl = color.replace(/^(hsl|hsla)\(/, '').replace(/\)$/, '').replace(/\s/g, '').split(',');
const h = parseFloat(hsl[0]) / 360.0;
const s = parseFloat(hsl[1].replace(/%$/, '')) / 100.0;
const l = parseFloat(hsl[2].replace(/%$/, '')) / 100.0;
// https://stackoverflow.com/questions/2353211/hsl-to-rgb-color-conversion
let r, g, b;
if (s == 0){
r = g = b = l;
} else {
let hue2rgb = function hue2rgb(p, q, t) {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1/6) return p + (q - p) * 6 * t;
if (t < 1/2) return q;
if (t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
}
let q = l < 0.5 ? l * (1 + s) : l + s - l * s;
let p = 2 * l - q;
r = hue2rgb(p, q, h + 1/3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1/3);
}
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
}
function handleTabActivated(activeInfo) {
updatePageActionIconIfNeeded(activeInfo.tabId);
}
function handleTabUpdated(tabId, changeInfo, tab) {
updatePageActionIconIfNeeded(tabId);
}
function updatePageActionIconIfNeeded(tabId) {
if (tabIdsInSyncWithTheme.has(tabId)) {
return;
}
browser.pageAction.isShown({ tabId: tabId }).then(isShown => {
if (!isShown) {
return;
}
const paths = getPageActionIconPaths(isDarkTheme);
browser.pageAction.setIcon({ path: paths, tabId: tabId });
tabIdsInSyncWithTheme.add(tabId);
});
}
function getPageActionIconPaths(forDarkTheme) {
if (forDarkTheme) {
return { "19": "icons/icon-light-19.png", "38": "icons/icon-light-38.png" };
}
return { "19": "icons/icon-19.png", "38": "icons/icon-38.png" };
}
})()