forked from rNeomy/auto-tab-discard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firefox.js
81 lines (76 loc) · 2.27 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
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
/*
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 = () => {}) {
if ('status' in queryInfo) {
callback([]);
return;
}
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]);
}
// FF onCreated is called when tab.url is still about:blank
if (isFirefox) {
const pa = chrome.tabs.onCreated.addListener;
chrome.tabs.onCreated.addListener = c => {
c._ = tab => {
if (tab.url === 'about:blank') {
const observe = (id, info) => {
if (id === tab.id && info.title) {
chrome.tabs.onUpdated.removeListener(observe);
setTimeout(c, 1000, tab);
}
};
chrome.tabs.onUpdated.addListener(observe);
setTimeout(() => {
if (chrome.tabs.onUpdated.hasListener(observe)) {
c(tab);
chrome.tabs.onUpdated.removeListener(observe);
}
}, 10000);
}
else {
c(tab);
}
};
pa.call(chrome.tabs.onCreated, c._);
};
const pr = chrome.tabs.onCreated.removeListener;
chrome.tabs.onCreated.removeListener = c => {
pr.call(this, c._ || c);
};
}