| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 71 object = object[node]; | 71 object = object[node]; |
| 72 | 72 |
| 73 if (!object) | 73 if (!object) |
| 74 return; | 74 return; |
| 75 } | 75 } |
| 76 | 76 |
| 77 let func = object[name]; | 77 let func = object[name]; |
| 78 if (!func) | 78 if (!func) |
| 79 return; | 79 return; |
| 80 | 80 |
| 81 let descriptor; | 81 // If the property is not writable assigning it will fail, so we use |
| 82 let objectProto = object; | 82 // Object.defineProperty here instead. Assuming the property isn't |
| 83 do | 83 // inherited its other attributes (e.g. enumerable) are preserved, |
|
Manish Jethani
2018/02/15 13:17:51
By the way I find this nicer too:
let descrip
| |
| 84 { | 84 // except for accessor attributes (e.g. get and set) which are discarded |
| 85 descriptor = Object.getOwnPropertyDescriptor(objectProto, name); | 85 // since we're specifying a value. |
| 86 } | 86 Object.defineProperty(object, name, { |
| 87 while (!descriptor && (objectProto = Object.getPrototypeOf(objectProto))); | 87 value(...args) |
|
Manish Jethani
2018/02/15 09:59:24
I think we should have a separate function called
| |
| 88 | |
| 89 delete descriptor["get"]; | |
| 90 delete descriptor["set"]; | |
| 91 | |
| 92 descriptor.value = function(...args) | |
| 93 { | |
| 94 let callStack = new Error().stack; | |
| 95 | |
| 96 if (typeof args[args.length - 1] == "function") | |
| 97 return func.apply(object, args); | |
| 98 | |
| 99 // If the last argument is undefined, we drop it from the list assuming | |
| 100 // it stands for the optional callback. We must do this, because we have | |
| 101 // to replace it with our own callback. If we simply append our own | |
| 102 // callback to the list, it won't match the signature of the function and | |
| 103 // will cause an exception. | |
| 104 if (typeof args[args.length - 1] == "undefined") | |
| 105 args.pop(); | |
| 106 | |
| 107 let resolvePromise = null; | |
| 108 let rejectPromise = null; | |
| 109 | |
| 110 func.call(object, ...args, result => | |
| 111 { | 88 { |
| 112 let error = browser.runtime.lastError; | 89 let callStack = new Error().stack; |
| 113 if (error && !portClosedBeforeResponseError.test(error.message)) | 90 |
| 91 if (typeof args[args.length - 1] == "function") | |
| 92 return func.apply(object, args); | |
| 93 | |
| 94 // If the last argument is undefined, we drop it from the list assuming | |
| 95 // it stands for the optional callback. We must do this, because we have | |
| 96 // to replace it with our own callback. If we simply append our own | |
| 97 // callback to the list, it won't match the signature of the function | |
| 98 // and will cause an exception. | |
| 99 if (typeof args[args.length - 1] == "undefined") | |
| 100 args.pop(); | |
| 101 | |
| 102 let resolvePromise = null; | |
| 103 let rejectPromise = null; | |
| 104 | |
| 105 func.call(object, ...args, result => | |
| 114 { | 106 { |
| 115 // runtime.lastError is already an Error instance on Edge, while on | 107 let error = browser.runtime.lastError; |
| 116 // Chrome it is a plain object with only a message property. | 108 if (error && !portClosedBeforeResponseError.test(error.message)) |
| 117 if (!(error instanceof Error)) | |
| 118 { | 109 { |
| 119 error = new Error(error.message); | 110 // runtime.lastError is already an Error instance on Edge, while on |
| 120 | 111 // Chrome it is a plain object with only a message property. |
| 121 // Add a more helpful stack trace. | 112 if (!(error instanceof Error)) |
| 122 error.stack = callStack; | 113 { |
| 114 error = new Error(error.message); | |
| 115 | |
| 116 // Add a more helpful stack trace. | |
| 117 error.stack = callStack; | |
| 118 } | |
| 119 | |
| 120 rejectPromise(error); | |
| 123 } | 121 } |
| 124 | 122 else |
| 125 rejectPromise(error); | 123 { |
| 126 } | 124 resolvePromise(result); |
| 127 else | 125 } |
| 126 }); | |
| 127 | |
| 128 return new Promise((resolve, reject) => | |
| 128 { | 129 { |
| 129 resolvePromise(result); | 130 resolvePromise = resolve; |
| 130 } | 131 rejectPromise = reject; |
| 131 }); | 132 }); |
| 132 | 133 } |
| 133 return new Promise((resolve, reject) => | 134 }); |
| 134 { | |
| 135 resolvePromise = resolve; | |
| 136 rejectPromise = reject; | |
| 137 }); | |
| 138 }; | |
| 139 | |
| 140 Object.defineProperty(object, name, descriptor); | |
| 141 } | 135 } |
| 142 | 136 |
| 143 function wrapRuntimeOnMessage() | 137 function wrapRuntimeOnMessage() |
| 144 { | 138 { |
| 145 let {onMessage} = browser.runtime; | 139 let {onMessage} = browser.runtime; |
| 146 let {addListener, removeListener} = onMessage; | 140 let {addListener, removeListener} = onMessage; |
| 147 | 141 |
| 148 onMessage.addListener = function(listener) | 142 onMessage.addListener = function(listener) |
| 149 { | 143 { |
| 150 if (typeof listener != "function") | 144 if (typeof listener != "function") |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 234 | 228 |
| 235 // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList | 229 // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList |
| 236 // didn't have iterator support before Chrome 51. | 230 // didn't have iterator support before Chrome 51. |
| 237 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699 | 231 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699 |
| 238 for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList]) | 232 for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList]) |
| 239 { | 233 { |
| 240 if (!(Symbol.iterator in object.prototype)) | 234 if (!(Symbol.iterator in object.prototype)) |
| 241 object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator]; | 235 object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator]; |
| 242 } | 236 } |
| 243 } | 237 } |
| LEFT | RIGHT |