Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: polyfill.js

Issue 29570614: Issue 5028 - Use browser namespace (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Promisify Created Oct. 11, 2017, 3:22 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « options.js ('k') | popup.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
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/>.
16 */
17
18 "use strict";
19
20 // Set up a "browser" namespace with a promisified version of the extension
21 // APIs on Chrome and Edge.
22 if (typeof browser == "undefined" || typeof chrome == "undefined" ||
23 typeof chrome != "undefined" && !("extension" in chrome))
24 {
25 const callbackApi = new Set([
26 "create"
Manish Jethani 2017/10/11 03:26:44 Right now only covers tabs.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
82 // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList
83 // didn't have iterator support before Chrome 51.
84 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699
85 for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList])
86 {
87 if (!(Symbol.iterator in object.prototype))
88 object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
89 }
OLDNEW
« no previous file with comments | « options.js ('k') | popup.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld