| Index: ext/common.js |
| =================================================================== |
| --- a/ext/common.js |
| +++ b/ext/common.js |
| @@ -20,16 +20,105 @@ |
| (function() |
| { |
| // Both Edge and Mozilla Web Extensions use the namespace |
| // 'browser' instead of 'chrome'. Edge has chrome namespace defined, |
| // in some cases, but only with one property: 'app'. |
| if (typeof chrome == "undefined" || typeof chrome.extension == "undefined") |
| window.chrome = window.browser; |
| + const asyncAPIs = [ |
| + "contextMenus.removeAll", |
| + "devtools.panels.create", |
| + "notifications.clear", |
| + "notifications.create", |
| + "runtime.openOptionsPage", |
| + "runtime.sendMessage", |
| + "runtime.setUninstallURL", |
| + "storage.local.get", |
| + "storage.local.remove", |
| + "storage.local.set", |
| + "storage.managed.get", |
| + "tabs.create", |
| + "tabs.get", |
| + "tabs.insertCSS", |
| + "tabs.query", |
| + "tabs.reload", |
| + "tabs.sendMessage", |
| + "tabs.update", |
| + "webNavigation.getAllFrames", |
| + "webRequest.handlerBehaviorChanged", |
| + "windows.create", |
| + "windows.update" |
| + ]; |
| + |
| + function wrapAPI(api) |
| + { |
| + let object = chrome; |
| + let path = api.split("."); |
| + let name = path.pop(); |
| + |
| + for (let node of path) |
| + { |
| + object = object[node]; |
| + |
| + if (!object) |
| + return; |
| + } |
| + |
| + let func = object[name]; |
| + object[name] = function(...args) |
| + { |
| + if (typeof args[args.length - 1] == "function") |
| + return func.apply(object, args); |
| + |
| + // If the last argument is undefined, we drop it from the list assuming |
| + // it stands for the optional callback. We must do this, because we have |
| + // to replace it with our own callback. If we simply append our own |
| + // callback to the list, it won't match the signature of the function and |
| + // will cause an exception. |
| + 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.
|
| + args.pop(); |
| + |
| + return new Promise((resolve, reject) => |
| + { |
| + func.call(object, ...args, result => |
| + { |
| + let error = chrome.runtime.lastError; |
| + if (error) |
| + reject(error); |
| + else |
| + resolve(result); |
| + }); |
| + }); |
| + }; |
| + } |
| + |
| + function shouldWrapAPIs() |
| + { |
| + 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.
|
| + return true; |
| + |
| + try |
| + { |
| + return !(browser.storage.local.get([]) instanceof Promise); |
| + } |
| + catch (error) |
| + { |
| + } |
| + |
| + return true; |
| + } |
| + |
| + if (shouldWrapAPIs()) |
| + { |
| + for (let api of asyncAPIs) |
| + wrapAPI(api); |
| + } |
| + |
| window.ext = {}; |
| let EventTarget = ext._EventTarget = function() |
| { |
| this._listeners = new Set(); |
| }; |
| EventTarget.prototype = { |
| addListener(listener) |