-
Notifications
You must be signed in to change notification settings - Fork 76
/
firefox.js
43 lines (41 loc) · 1.3 KB
/
firefox.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
/*
add "autoDiscardable" support to "chrome.tabs.query" and "chrome.tabs.update"
*/
const isFirefox = /Firefox/.test(navigator.userAgent) || typeof InstallTrigger !== 'undefined';
if (isFirefox) {
const cache = {};
const query = chrome.tabs.query;
chrome.tabs.query = function(queryInfo, callback = () => {}) {
const b = 'autoDiscardable' in queryInfo;
const v = queryInfo.autoDiscardable;
delete queryInfo.autoDiscardable;
query.apply(this, [queryInfo, tabs => {
if (b) {
tabs = tabs.filter(tab => v ? cache[tab.id] !== false : cache[tab.id] === false);
}
for (const tab of tabs) {
tab.autoDiscardable = tab.id in cache ? cache[tab.id] : true;
}
callback(tabs);
}]);
};
const update = chrome.tabs.update;
chrome.tabs.update = function(tabId, updateProperties, callback = () => {}) {
const b = 'autoDiscardable' in updateProperties;
const v = updateProperties.autoDiscardable;
delete updateProperties.autoDiscardable;
const next = () => {
if (b) {
cache[tabId] = v;
}
callback();
};
if (Object.keys(updateProperties).length) {
update.apply(this, [tabId, updateProperties, next]);
}
else {
next();
}
};
chrome.tabs.onRemoved.addListener(tabId => delete cache[tabId]);
}