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