forked from adblockplus/adblockpluschrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolyfill.js
149 lines (131 loc) · 4.17 KB
/
polyfill.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
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
/*
* 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.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.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\.$/;
function wrapAPI(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;
object[name] = 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();
return new Promise((resolve, reject) =>
{
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;
}
reject(error);
}
else
{
resolve(result);
}
});
});
};
}
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)
wrapAPI(api);
}
// 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];
}
}