| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 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 (function() | 20 (function() |
| 21 { | 21 { |
| 22 // Both Edge and Mozilla Web Extensions use the namespace | 22 // Both Edge and Mozilla Web Extensions use the namespace |
| 23 // 'browser' instead of 'chrome'. Edge has chrome namespace defined, | 23 // 'browser' instead of 'chrome'. Edge has chrome namespace defined, |
| 24 // in some cases, but only with one property: 'app'. | 24 // in some cases, but only with one property: 'app'. |
| 25 if (typeof chrome == "undefined" || typeof chrome.extension == "undefined") | 25 if (typeof chrome == "undefined" || typeof chrome.extension == "undefined") |
| 26 window.chrome = window.browser; | 26 window.chrome = window.browser; |
| 27 | 27 |
| 28 const asyncAPIs = [ | |
| 29 "contextMenus.removeAll", | |
| 30 "devtools.panels.create", | |
| 31 "notifications.clear", | |
| 32 "notifications.create", | |
| 33 "runtime.openOptionsPage", | |
| 34 "runtime.sendMessage", | |
| 35 "runtime.setUninstallURL", | |
| 36 "storage.local.get", | |
| 37 "storage.local.remove", | |
| 38 "storage.local.set", | |
| 39 "storage.managed.get", | |
| 40 "tabs.create", | |
| 41 "tabs.get", | |
| 42 "tabs.insertCSS", | |
| 43 "tabs.query", | |
| 44 "tabs.reload", | |
| 45 "tabs.sendMessage", | |
| 46 "tabs.update", | |
| 47 "webNavigation.getAllFrames", | |
| 48 "webRequest.handlerBehaviorChanged", | |
| 49 "windows.create", | |
| 50 "windows.update" | |
| 51 ]; | |
| 52 | |
| 53 function wrapAPI(api) | |
| 54 { | |
| 55 let object = chrome; | |
| 56 let path = api.split("."); | |
| 57 let name = path.pop(); | |
| 58 | |
| 59 for (let node of path) | |
| 60 { | |
| 61 object = object[node]; | |
| 62 | |
| 63 if (!object) | |
| 64 return; | |
| 65 } | |
| 66 | |
| 67 let func = object[name]; | |
| 68 object[name] = function(...args) | |
| 69 { | |
| 70 if (typeof args[args.length - 1] == "function") | |
| 71 return func.apply(object, args); | |
| 72 | |
| 73 // If the last argument is undefined, we drop it from the list assuming | |
| 74 // it stands for the optional callback. We must do this, because we have | |
| 75 // to replace it with our own callback. If we simply append our own | |
| 76 // callback to the list, it won't match the signature of the function and | |
| 77 // will cause an exception. | |
| 78 if (typeof args[args.length - 1] == "undefined") | |
|
Wladimir Palant
2017/10/13 09:53:14
I don't get why this is necessary - the last argum
Manish Jethani
2017/10/13 15:15:16
Sometimes there's code like this:
function setP
Sebastian Noack
2017/10/14 00:44:07
If we assume the last argument if undefined being
Manish Jethani
2017/10/14 01:07:30
You're right, that's how it should have been.
Don
Sebastian Noack
2017/10/14 01:31:21
Actually, I just noticed, the way you had it befor
Manish Jethani
2017/10/14 02:01:30
Done.
| |
| 79 args.pop(); | |
| 80 | |
| 81 return new Promise((resolve, reject) => | |
| 82 { | |
| 83 func.call(object, ...args, result => | |
| 84 { | |
| 85 let error = chrome.runtime.lastError; | |
| 86 if (error) | |
| 87 reject(error); | |
| 88 else | |
| 89 resolve(result); | |
| 90 }); | |
| 91 }); | |
| 92 }; | |
| 93 } | |
| 94 | |
| 95 function shouldWrapAPIs() | |
| 96 { | |
| 97 if (typeof browser == "undefined") | |
|
Sebastian Noack
2017/10/14 00:44:07
Is this shortcut worth it?
Manish Jethani
2017/10/14 01:13:02
I'm not sure about this one honestly. I'd say it d
Sebastian Noack
2017/10/14 01:31:21
Well, we generally do feature detection, not brows
Manish Jethani
2017/10/14 02:01:30
OK, that makes sense.
Done.
| |
| 98 return true; | |
| 99 | |
| 100 try | |
| 101 { | |
| 102 return !(browser.storage.local.get([]) instanceof Promise); | |
| 103 } | |
| 104 catch (error) | |
| 105 { | |
| 106 } | |
| 107 | |
| 108 return true; | |
| 109 } | |
| 110 | |
| 111 if (shouldWrapAPIs()) | |
| 112 { | |
| 113 for (let api of asyncAPIs) | |
| 114 wrapAPI(api); | |
| 115 } | |
| 116 | |
| 28 window.ext = {}; | 117 window.ext = {}; |
| 29 | 118 |
| 30 let EventTarget = ext._EventTarget = function() | 119 let EventTarget = ext._EventTarget = function() |
| 31 { | 120 { |
| 32 this._listeners = new Set(); | 121 this._listeners = new Set(); |
| 33 }; | 122 }; |
| 34 EventTarget.prototype = { | 123 EventTarget.prototype = { |
| 35 addListener(listener) | 124 addListener(listener) |
| 36 { | 125 { |
| 37 this._listeners.add(listener); | 126 this._listeners.add(listener); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 77 { | 166 { |
| 78 return chrome.extension.getBackgroundPage(); | 167 return chrome.extension.getBackgroundPage(); |
| 79 } | 168 } |
| 80 }; | 169 }; |
| 81 | 170 |
| 82 | 171 |
| 83 /* Utils */ | 172 /* Utils */ |
| 84 | 173 |
| 85 ext.getURL = chrome.extension.getURL; | 174 ext.getURL = chrome.extension.getURL; |
| 86 }()); | 175 }()); |
| OLD | NEW |