forked from adblockplus/adblockpluschrome
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolyfill.js
More file actions
237 lines (199 loc) · 6.34 KB
/
polyfill.js
File metadata and controls
237 lines (199 loc) · 6.34 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* This file is part of Adblock Plus <https://adblockplus.org/>,
* Copyright (C) 2006-present eyeo GmbH
*
* Adblock Plus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* Adblock Plus 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 Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
{
const asyncAPIs = [
"contextMenus.removeAll",
"devtools.panels.create",
"notifications.clear",
"notifications.create",
"runtime.getBrowserInfo",
"runtime.openOptionsPage",
"runtime.sendMessage",
"runtime.setUninstallURL",
"storage.local.get",
"storage.local.remove",
"storage.local.set",
"storage.managed.get",
"tabs.create",
"tabs.get",
"tabs.getCurrent",
"tabs.insertCSS",
"tabs.query",
"tabs.reload",
"tabs.removeCSS",
"tabs.sendMessage",
"tabs.update",
"webNavigation.getAllFrames",
"webRequest.handlerBehaviorChanged",
"windows.create",
"windows.update"
];
// Since we add a callback for all messaging API calls in our wrappers,
// Chrome assumes we're interested in the response; when there's no response,
// it sets runtime.lastError
const portClosedBeforeResponseError =
// Older versions of Chrome have a typo:
// https://crrev.com/c33f51726eacdcc1a487b21a13611f7eab580d6d
/^The message port closed before a res?ponse was received\.$/;
// This is the error Firefox throws when a message listener is not a
// function.
const invalidMessageListenerError = "Invalid listener for runtime.onMessage.";
let messageListeners = new WeakMap();
function wrapAsyncAPI(api)
{
let object = browser;
let path = api.split(".");
let name = path.pop();
for (let node of path)
{
object = object[node];
if (!object)
return;
}
let func = object[name];
if (!func)
return;
let descriptor = Object.getOwnPropertyDescriptor(object, name);
delete descriptor["get"];
delete descriptor["set"];
descriptor.value = function(...args)
{
let callStack = new Error().stack;
if (typeof args[args.length - 1] == "function")
return func.apply(object, args);
// If the last argument is undefined, we drop it from the list assuming
// it stands for the optional callback. We must do this, because we have
// to replace it with our own callback. If we simply append our own
// callback to the list, it won't match the signature of the function and
// will cause an exception.
if (typeof args[args.length - 1] == "undefined")
args.pop();
let resolvePromise = null;
let rejectPromise = null;
func.call(object, ...args, result =>
{
let error = browser.runtime.lastError;
if (error && !portClosedBeforeResponseError.test(error.message))
{
// runtime.lastError is already an Error instance on Edge, while on
// Chrome it is a plain object with only a message property.
if (!(error instanceof Error))
{
error = new Error(error.message);
// Add a more helpful stack trace.
error.stack = callStack;
}
rejectPromise(error);
}
else
{
resolvePromise(result);
}
});
return new Promise((resolve, reject) =>
{
resolvePromise = resolve;
rejectPromise = reject;
});
};
Object.defineProperty(object, name, descriptor);
}
function wrapRuntimeOnMessage()
{
let {onMessage} = browser.runtime;
let {addListener, removeListener} = onMessage;
onMessage.addListener = function(listener)
{
if (typeof listener != "function")
throw new Error(invalidMessageListenerError);
// Don't add the same listener twice or we end up with multiple wrappers.
if (messageListeners.has(listener))
return;
let wrapper = (message, sender, sendResponse) =>
{
let wait = listener(message, sender, sendResponse);
if (wait instanceof Promise)
{
wait.then(sendResponse, reason =>
{
try
{
sendResponse();
}
catch (error)
{
// sendResponse can throw if the internal port is closed; be sure
// to throw the original error.
}
throw reason;
});
}
return !!wait;
};
addListener.call(onMessage, wrapper);
messageListeners.set(listener, wrapper);
};
onMessage.removeListener = function(listener)
{
if (typeof listener != "function")
throw new Error(invalidMessageListenerError);
let wrapper = messageListeners.get(listener);
if (wrapper)
{
removeListener.call(onMessage, wrapper);
messageListeners.delete(listener);
}
};
onMessage.hasListener = function(listener)
{
if (typeof listener != "function")
throw new Error(invalidMessageListenerError);
return messageListeners.has(listener);
};
}
function shouldWrapAPIs()
{
try
{
return !(browser.storage.local.get([]) instanceof Promise);
}
catch (error)
{
}
return true;
}
if (shouldWrapAPIs())
{
// Unlike Firefox and Microsoft Edge, Chrome doesn't have a "browser"
// object, but provides the extension API through the "chrome" namespace
// (non-standard).
if (typeof browser == "undefined")
window.browser = chrome;
for (let api of asyncAPIs)
wrapAsyncAPI(api);
wrapRuntimeOnMessage();
}
// Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList
// didn't have iterator support before Chrome 51.
// https://bugs.chromium.org/p/chromium/issues/detail?id=401699
for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList])
{
if (!(Symbol.iterator in object.prototype))
object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
}
}