| Index: polyfill.js |
| =================================================================== |
| new file mode 100644 |
| --- /dev/null |
| +++ b/polyfill.js |
| @@ -0,0 +1,89 @@ |
| +/* |
| + * 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"; |
| + |
| +// Set up a "browser" namespace with a promisified version of the extension |
| +// APIs on Chrome and Edge. |
| +if (typeof browser == "undefined" || typeof chrome == "undefined" || |
| + typeof chrome != "undefined" && !("extension" in chrome)) |
| +{ |
| + const callbackApi = new Set([ |
| + "create" |
|
Manish Jethani
2017/10/11 03:26:44
Right now only covers tabs.create
|
| + ]); |
| + |
| + if (typeof browser == "undefined") |
| + window.browser = chrome; |
| + |
| + window.browser = new Proxy(browser, { |
| + get(target, property) |
| + { |
| + let value = target[property]; |
| + |
| + // If this is a non-null object and the name does not begin with "on" |
| + // (e.g. "onUpdated"), wrap it into a proxy. |
| + if (typeof value == "object" && value != null && |
| + !/^on[A-Z]/.test(property)) |
| + { |
| + return new Proxy(value, this); |
| + } |
| + |
| + // If this is one of the known API functions that take an optional |
| + // callback as the last argument, wrap it into a special proxy with a |
| + // promise-based interface. |
| + if (typeof value == "function" && callbackApi.has(property)) |
| + { |
| + return new Proxy(value, { |
| + apply(target, thisArg, argumentsList) |
| + { |
| + // If the caller has already supplied a callback, just call and |
| + // return. |
| + if (typeof argumentsList[argumentsList.length - 1] == "function") |
| + return target.apply(thisArg, argumentsList); |
| + |
| + // Wrap the API call into a promise. |
| + return new Promise((resolve, reject) => |
| + { |
| + target.apply(thisArg, [ |
| + ...argumentsList, |
| + result => |
| + { |
| + let error = browser.runtime.lastError; |
| + if (error) |
| + reject(error); |
| + else |
| + resolve(result); |
| + } |
| + ]); |
| + }); |
| + } |
| + }); |
| + } |
| + |
| + return value; |
| + } |
| + }); |
| +} |
| + |
| +// 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]; |
| +} |