Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
198 lines (181 loc) · 7.3 KB
/
Copy pathbackground.js
File metadata and controls
198 lines (181 loc) · 7.3 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Chrome runs background as a service worker (background.service_worker), shared code is loaded here.
// Firefox runs background.scripts (common.js is listed before this file in manifest).
if (typeof importScripts === 'function' && typeof TRIGGERS === 'undefined') {
importScripts('common.js');
}
function getIcons(color) {
return {
"16": "icons/bug-" + color + "-16.png",
"32": "icons/bug-" + color + "-32.png",
"64": "icons/bug-" + color + "-64.png",
"128": "icons/bug-" + color + "-128.png"
};
}
// Calls for the same tab are serialized: otherwise a slow call with an old state
// may overwrite the icon after a newer one
const iconQueue = new Map();
function updateIcon(tab) {
if (!tab) {
log('updateIcon: no tab');
return Promise.resolve();
}
let next = (iconQueue.get(tab.id) || Promise.resolve())
.then(() => renderIcon(tab))
.catch(e => logError('updateIcon failed', e));
iconQueue.set(tab.id, next);
next.then(() => {
if (iconQueue.get(tab.id) === next) {
iconQueue.delete(tab.id);
}
});
return next;
}
async function renderIcon(tab) {
if (!tab) {
log('updateIcon: no tab');
return;
}
let states = await getStates(tab);
let triggers = await getTriggerSettings(getHost(tab.url));
let colors = await getColors();
let enabled = triggers.filter(t => states[t.id]);
let badge = enabled.map(t => t.badge).join('');
let title = enabled.length
? t('titleOn', enabled.map(trigger => trigger.label + ' (' + trigger.name + ')').join(', '))
: t('titleOff');
if (!isSupportedUrl(tab.url)) {
title = t('titleUnavailable');
} else if (!await hasHostPermission()) {
title = t('titleNoPermission');
}
log('updateIcon: tab', tab.id, tab.url, 'enabled:', badge || '-');
try {
await callApi('action.setIcon', [{ path: getIcons(enabled.length ? colors.on : colors.off), tabId: tab.id }]);
await callApi('action.setTitle', [{ title: title, tabId: tab.id }]);
await callApi('action.setBadgeText', [{ text: badge, tabId: tab.id }]);
await callApi('action.setBadgeBackgroundColor', [{ color: '#c0392b', tabId: tab.id }]);
} catch (e) {
// The tab may be closed between query and update
logError('updateIcon failed', e);
}
}
// Service worker has no persistent state, so active tabs are always queried
async function updateActiveTabs() {
let tabs = await callApi('tabs.query', [{ active: true }], []);
for (let tab of tabs) {
await updateIcon(tab);
}
}
// Removes cookies with the old name after it has been changed in options,
// otherwise they stay in the browser and keep triggering Xdebug.
async function removeCookiesByName(name, value) {
let stores = await callApi('cookies.getAllCookieStores', [], []);
for (let store of stores) {
let query = { name: name, storeId: store.id };
if (IS_FIREFOX) {
query.firstPartyDomain = null;
}
let cookies = [];
try {
cookies = await requireApi('cookies.getAll', [query]);
} catch (e) {
logError('removeCookiesByName: getAll failed for store', store.id, e);
continue;
}
for (let cookie of cookies) {
if (cookie.value != value) {
continue;
}
try {
await removeCookie(cookie);
} catch (e) {
logError('removeCookiesByName: remove failed for', cookie.domain, e);
}
}
}
}
async function onSettingsChanged(changes, area) {
log('settings changed in', area, changes);
if (area === SETTINGS_AREA) {
let triggers = await getTriggerSettings();
for (let trigger of TRIGGERS) {
let nameChange = changes[trigger.key];
let valueChange = changes[trigger.key + '_value'];
if (!nameChange && !valueChange) {
continue;
}
let current = triggers.find(t => t.id === trigger.id);
// Empty/missing values mean defaults
let oldName = (nameChange ? nameChange.oldValue : current.name) || trigger.defaultName;
let oldValue = (valueChange ? valueChange.oldValue : current.value) || trigger.defaultValue;
if (oldName !== current.name || oldValue !== current.value) {
log('trigger', trigger.id, 'changed from', oldName + '=' + oldValue, 'to', current.name + '=' + current.value);
await removeCookiesByName(oldName, oldValue);
}
}
}
await updateActiveTabs();
}
// Updates all tabs the changed cookie belongs to (not only active ones: popup may be open in another tab)
async function onCookieChanged(changeInfo) {
let triggers = await getTriggerSettings();
if (!triggers.some(t => t.name === changeInfo.cookie.name)) {
return;
}
log('cookie changed:', changeInfo.cookie.name, changeInfo.cookie.domain, 'removed:', changeInfo.removed, 'cause:', changeInfo.cause);
let domain = changeInfo.cookie.domain.replace(/^\./, '');
let tabs = await callApi('tabs.query', [{}], []);
for (let tab of tabs) {
let host = getHost(tab.url);
if (host === domain || host.endsWith('.' + domain)) {
await updateIcon(tab);
}
}
}
function onTabUpdated(tabId, changeInfo, tab) {
// React only to navigation, not to title/favicon/audio updates.
// Tab-specific badge is reset by the browser on navigation, so background tabs are updated too.
if (changeInfo.url || changeInfo.status === 'complete') {
updateIcon(tab);
}
}
async function onTabActivated(activeInfo) {
try {
updateIcon(await requireApi('tabs.get', [activeInfo.tabId]));
} catch (e) {
logError('onTabActivated: tabs.get failed', e);
}
}
// Keyboard shortcuts toggle triggers without opening the popup
async function onCommand(command) {
let triggerId = command.replace(/^toggle-/, '');
let tab = await getActiveTab();
log('command', command, 'tab', tab && tab.url);
try {
await toggleTrigger(tab, triggerId);
} catch (e) {
logError('command', command, 'failed', e);
}
await updateIcon(tab);
}
// Firefox MV3 treats host permissions as optional: open options with "Grant access" button if needed
async function onInstalled(details) {
log('installed:', details.reason);
// On first install the options page asks to choose the IDE
if (details.reason === 'install' || !await hasHostPermission()) {
callApi('runtime.openOptionsPage', []);
}
await updateActiveTabs();
}
// Listeners must be registered synchronously at top level (service worker requirement).
// Missing events are skipped: e.g. Firefox for Android has no windows and commands (keyboard shortcuts) APIs.
addApiListener('tabs.onUpdated', onTabUpdated);
addApiListener('tabs.onActivated', onTabActivated);
addApiListener('cookies.onChanged', onCookieChanged);
addApiListener('storage.onChanged', onSettingsChanged);
addApiListener('runtime.onInstalled', onInstalled);
addApiListener('windows.onFocusChanged', updateActiveTabs);
addApiListener('commands.onCommand', onCommand);
addApiListener('permissions.onAdded', updateActiveTabs);
addApiListener('permissions.onRemoved', updateActiveTabs);
updateActiveTabs();