forked from Loirooriol/tab-counter-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindowId-polyfill.js
More file actions
157 lines (145 loc) · 4.94 KB
/
windowId-polyfill.js
File metadata and controls
157 lines (145 loc) · 4.94 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
/*
* Copyright 2018 Oriol Brufau
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
async function windowIdPolyfill(params, windowIdWillAlwaysBeUndefined) {
let originalMethod = Object.create(null);
let activeTab = Symbol();
let windowData = new Map();
let {browserAction} = browser;
for (let [key, method] of Object.entries(params)) {
originalMethod[key] = browserAction[method];
browserAction[method] = function(details) {
let {windowId} = details;
if (windowIdWillAlwaysBeUndefined || windowId == null) {
delete details.windowId;
return Reflect.apply(originalMethod[key], browserAction, [details]);
}
let data = windowData.get(windowId);
if (!data) {
data = Object.create(null);
data[activeTab] = null;
windowData.set(windowId, data);
}
data[key] = details[key];
update(windowId, data, [key]);
};
}
if (windowIdWillAlwaysBeUndefined) {
// Avoid adding the event listeners if they are unnecessary.
return;
}
function handleError(error) {
if (error.message.includes("Invalid tab ID")) {
// The tab has been removed
} else {
throw error;
}
}
function setData(data, tabId = undefined, keys = Object.keys(data)) {
for (let key of keys) {
let details = {tabId, [key]: data[key]};
Reflect.apply(originalMethod[key], browserAction, [details]).catch(handleError);
}
}
function clearData(data, tabId = undefined, keys = Object.keys(data)) {
try {
for (let key of keys) {
let details = {tabId, [key]: null};
Reflect.apply(originalMethod[key], browserAction, [details]).catch(handleError);
}
} catch (error) {
// Tab-specific browserAction cannot be cleared before Firefox 59.
}
}
async function update(windowId, data, keys) {
// Get active tab if necessary.
if (data[activeTab] == null) {
let [tab] = await browser.tabs.query({active: true, windowId});
if (!tab) {
// The window does not exist
windowData.delete(windowId);
return;
}
data[activeTab] = tab.id;
}
// Set tab-specific data in the active tab.
setData(data, data[activeTab], keys);
let {focused} = await browser.windows.get(windowId);
if (focused) {
// Set global data to avoid flickering when active tab changes.
setData(data, undefined, keys);
}
}
for (let {windowId, id} of await browser.tabs.query({active: true})) {
let data = Object.create(null);
data[activeTab] = id;
windowData.set(windowId, data);
}
browser.windows.onCreated.addListener(function ({id: windowId}) {
if (!windowData.has(windowId)) {
let data = Object.create(null);
data[activeTab] = null;
windowData.set(windowId, data);
}
});
browser.windows.onRemoved.addListener(function (windowId) {
let data = windowData.get(windowId);
if (!data) {
return;
}
windowData.delete(windowId);
// If the last tab was moved into another window while we still thought
// it was the active tab of the window being removed, then it needs to
// be updated.
let active = data[activeTab];
if (active != null) {
browser.tabs.get(active).then(function(tab) {
if (tab.active && tab.windowId != windowId) {
let {windowId, id} = tab;
let data = windowData.get(windowId);
data[activeTab] = id;
update(windowId, data);
}
}, handleError);
}
});
browser.windows.onFocusChanged.addListener(async function (windowId) {
// The focus can go to a non-browser window like a console.
if (windowData.has(windowId)) {
// Update global values when focused window changes.
let data = windowData.get(windowId);
setData(data, undefined);
}
});
browser.tabs.onActivated.addListener(function ({windowId, tabId}) {
// Clear the data of the previously active tab.
let data = windowData.get(windowId);
let active = data[activeTab];
if (active != null) {
clearData(data, active);
}
// Set the data to the current active tab.
data[activeTab] = tabId;
setData(data, tabId);
});
browser.tabs.onUpdated.addListener(function (tabId, {status}, {active, windowId}) {
if (status && active) {
// An active tab navigated to a new page and its tab-specific
// browserAction was cleared. It needs to be restored.
let data = windowData.get(windowId);
setData(data, tabId);
}
});
}