| 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   52     // Older versions of Chrome have a typo: |   52     // Older versions of Chrome have a typo: | 
|   53     // https://crrev.com/c33f51726eacdcc1a487b21a13611f7eab580d6d |   53     // https://crrev.com/c33f51726eacdcc1a487b21a13611f7eab580d6d | 
|   54     /^The message port closed before a res?ponse was received\.$/; |   54     /^The message port closed before a res?ponse was received\.$/; | 
|   55  |   55  | 
|   56   // This is the error Firefox throws when a message listener is not a |   56   // This is the error Firefox throws when a message listener is not a | 
|   57   // function. |   57   // function. | 
|   58   const invalidMessageListenerError = "Invalid listener for runtime.onMessage."; |   58   const invalidMessageListenerError = "Invalid listener for runtime.onMessage."; | 
|   59  |   59  | 
|   60   let messageListeners = new WeakMap(); |   60   let messageListeners = new WeakMap(); | 
|   61  |   61  | 
|   62   function wrapAPI(api) |   62   function wrapAsyncAPI(api) | 
|   63   { |   63   { | 
|   64     let object = browser; |   64     let object = browser; | 
|   65     let path = api.split("."); |   65     let path = api.split("."); | 
|   66     let name = path.pop(); |   66     let name = path.pop(); | 
|   67  |   67  | 
|   68     for (let node of path) |   68     for (let node of path) | 
|   69     { |   69     { | 
|   70       object = object[node]; |   70       object = object[node]; | 
|   71  |   71  | 
|   72       if (!object) |   72       if (!object) | 
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  120           { |  120           { | 
|  121             resolve(result); |  121             resolve(result); | 
|  122           } |  122           } | 
|  123         }); |  123         }); | 
|  124       }); |  124       }); | 
|  125     }; |  125     }; | 
|  126  |  126  | 
|  127     Object.defineProperty(object, name, descriptor); |  127     Object.defineProperty(object, name, descriptor); | 
|  128   } |  128   } | 
|  129  |  129  | 
|  130   function wrapMessageAPIs() |  130   function wrapRuntimeOnMessage() | 
|  131   { |  131   { | 
|  132     let {onMessage} = browser.runtime; |  132     let {onMessage} = browser.runtime; | 
|  133     let {addListener, removeListener, hasListener} = onMessage; |  133     let {addListener, removeListener, hasListener} = onMessage; | 
|  134  |  134  | 
|  135     onMessage.addListener = function(listener) |  135     onMessage.addListener = function(listener) | 
|  136     { |  136     { | 
|  137       if (typeof listener != "function") |  137       if (typeof listener != "function") | 
|  138         throw new Error(invalidMessageListenerError); |  138         throw new Error(invalidMessageListenerError); | 
|  139  |  139  | 
|  140       // Don't add the same listener twice or we end up with multiple wrappers. |  140       // Don't add the same listener twice or we end up with multiple wrappers. | 
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  206  |  206  | 
|  207   if (shouldWrapAPIs()) |  207   if (shouldWrapAPIs()) | 
|  208   { |  208   { | 
|  209     // Unlike Firefox and Microsoft Edge, Chrome doesn't have a "browser" |  209     // Unlike Firefox and Microsoft Edge, Chrome doesn't have a "browser" | 
|  210     // object, but provides the extension API through the "chrome" namespace |  210     // object, but provides the extension API through the "chrome" namespace | 
|  211     // (non-standard). |  211     // (non-standard). | 
|  212     if (typeof browser == "undefined") |  212     if (typeof browser == "undefined") | 
|  213       window.browser = chrome; |  213       window.browser = chrome; | 
|  214  |  214  | 
|  215     for (let api of asyncAPIs) |  215     for (let api of asyncAPIs) | 
|  216       wrapAPI(api); |  216       wrapAsyncAPI(api); | 
|  217  |  217  | 
|  218     wrapMessageAPIs(); |  218     wrapRuntimeOnMessage(); | 
|  219   } |  219   } | 
|  220  |  220  | 
|  221   // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList |  221   // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList | 
|  222   // didn't have iterator support before Chrome 51. |  222   // didn't have iterator support before Chrome 51. | 
|  223   // https://bugs.chromium.org/p/chromium/issues/detail?id=401699 |  223   // https://bugs.chromium.org/p/chromium/issues/detail?id=401699 | 
|  224   for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList]) |  224   for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList]) | 
|  225   { |  225   { | 
|  226     if (!(Symbol.iterator in object.prototype)) |  226     if (!(Symbol.iterator in object.prototype)) | 
|  227       object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator]; |  227       object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator]; | 
|  228   } |  228   } | 
|  229 } |  229 } | 
| LEFT | RIGHT |