| 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 = [ | |
|
Manish Jethani
2017/10/11 19:23:51
This is the list of async APIs we actually use.
I
| |
| 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, object) | |
| 54 { | |
| 55 if (!object) | |
| 56 return; | |
| 57 | |
| 58 let [root, ...subPath] = api.split("."); | |
| 59 | |
| 60 if (subPath.length > 0) | |
| 61 { | |
| 62 wrapAPI(subPath.join("."), object[root]); | |
| 63 return; | |
| 64 } | |
| 65 | |
| 66 let func = object[root]; | |
| 67 object[root] = function(...args) | |
| 68 { | |
| 69 if (typeof args[args.length - 1] == "function") | |
| 70 return func.apply(object, args); | |
| 71 | |
| 72 // If the last argument is undefined, we drop it from the list assuming | |
| 73 // it stands for the optional callback. We must do this, because we have | |
| 74 // to replace it with our own callback. If we simply append our own | |
| 75 // callback to the list, it won't match the signature of the function and | |
| 76 // will cause an exception. | |
| 77 if (typeof args[args.length - 1] == "undefined") | |
|
Manish Jethani
2017/10/11 19:23:51
This should work, but if it doesn't we can always
| |
| 78 args.pop(); | |
| 79 | |
| 80 return new Promise((resolve, reject) => | |
| 81 { | |
| 82 func.call(object, ...args, result => | |
| 83 { | |
| 84 let error = chrome.runtime.lastError; | |
|
Manish Jethani
2017/10/11 19:23:51
It's OK to refer to chrome directly here. It will
| |
| 85 if (error) | |
| 86 reject(error); | |
| 87 else | |
| 88 resolve(result); | |
| 89 }); | |
| 90 }); | |
| 91 }; | |
| 92 } | |
| 93 | |
| 94 for (let api of asyncAPIs) | |
| 95 wrapAPI(api, chrome); | |
| 96 | |
| 28 window.ext = {}; | 97 window.ext = {}; |
| 29 | 98 |
| 30 let EventTarget = ext._EventTarget = function() | 99 let EventTarget = ext._EventTarget = function() |
| 31 { | 100 { |
| 32 this._listeners = new Set(); | 101 this._listeners = new Set(); |
| 33 }; | 102 }; |
| 34 EventTarget.prototype = { | 103 EventTarget.prototype = { |
| 35 addListener(listener) | 104 addListener(listener) |
| 36 { | 105 { |
| 37 this._listeners.add(listener); | 106 this._listeners.add(listener); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 77 { | 146 { |
| 78 return chrome.extension.getBackgroundPage(); | 147 return chrome.extension.getBackgroundPage(); |
| 79 } | 148 } |
| 80 }; | 149 }; |
| 81 | 150 |
| 82 | 151 |
| 83 /* Utils */ | 152 /* Utils */ |
| 84 | 153 |
| 85 ext.getURL = chrome.extension.getURL; | 154 ext.getURL = chrome.extension.getURL; |
| 86 }()); | 155 }()); |
| OLD | NEW |