LEFT | RIGHT |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 "use strict"; | 18 "use strict"; |
19 | 19 |
20 // Set up a "browser" namespace with a promisified version of the extension | 20 // Unlike Firefox and Microsoft Edge, Chrome doesn't have a "browser" object, |
21 // APIs on Chrome and Edge. | 21 // but provides the extension API through the "chrome" namespace |
22 if (typeof browser == "undefined" || typeof chrome == "undefined" || | 22 // (non-standard). |
23 typeof chrome != "undefined" && !("extension" in chrome)) | 23 if (typeof browser == "undefined") |
24 { | 24 window.browser = chrome; |
25 const callbackApi = new Set([ | |
26 "create" | |
27 ]); | |
28 | |
29 if (typeof browser == "undefined") | |
30 window.browser = chrome; | |
31 | |
32 window.browser = new Proxy(browser, { | |
33 get(target, property) | |
34 { | |
35 let value = target[property]; | |
36 | |
37 // If this is a non-null object and the name does not begin with "on" | |
38 // (e.g. "onUpdated"), wrap it into a proxy. | |
39 if (typeof value == "object" && value != null && | |
40 !/^on[A-Z]/.test(property)) | |
41 { | |
42 return new Proxy(value, this); | |
43 } | |
44 | |
45 // If this is one of the known API functions that take an optional | |
46 // callback as the last argument, wrap it into a special proxy with a | |
47 // promise-based interface. | |
48 if (typeof value == "function" && callbackApi.has(property)) | |
49 { | |
50 return new Proxy(value, { | |
51 apply(target, thisArg, argumentsList) | |
52 { | |
53 // If the caller has already supplied a callback, just call and | |
54 // return. | |
55 if (typeof argumentsList[argumentsList.length - 1] == "function") | |
56 return target.apply(thisArg, argumentsList); | |
57 | |
58 // Wrap the API call into a promise. | |
59 return new Promise((resolve, reject) => | |
60 { | |
61 target.apply(thisArg, [ | |
62 ...argumentsList, | |
63 result => | |
64 { | |
65 let error = browser.runtime.lastError; | |
66 if (error) | |
67 reject(error); | |
68 else | |
69 resolve(result); | |
70 } | |
71 ]); | |
72 }); | |
73 } | |
74 }); | |
75 } | |
76 | |
77 return value; | |
78 } | |
79 }); | |
80 } | |
81 | 25 |
82 // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList | 26 // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList |
83 // didn't have iterator support before Chrome 51. | 27 // didn't have iterator support before Chrome 51. |
84 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699 | 28 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699 |
85 for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList]) | 29 for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList]) |
86 { | 30 { |
87 if (!(Symbol.iterator in object.prototype)) | 31 if (!(Symbol.iterator in object.prototype)) |
88 object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator]; | 32 object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator]; |
89 } | 33 } |
LEFT | RIGHT |