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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2017 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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 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/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 "use strict"; | 18 "use strict"; |
19 | 19 |
| 20 (function() |
20 { | 21 { |
21 // Both Edge and Mozilla Web Extensions use the namespace 'browser' instead of
'chrome' | 22 // Both Edge and Mozilla Web Extensions use the namespace 'browser' instead of
'chrome' |
22 if (typeof browser != 'undefined') | 23 if (typeof browser != 'undefined') |
23 window.chrome = window.browser; | 24 window.chrome = window.browser; |
24 | 25 |
25 window.ext = {}; | 26 window.ext = {}; |
| 27 |
26 let EventTarget = ext._EventTarget = function() | 28 let EventTarget = ext._EventTarget = function() |
27 { | 29 { |
28 this._listeners = []; | 30 this._listeners = new Set(); |
29 }; | 31 }; |
30 EventTarget.prototype = { | 32 EventTarget.prototype = { |
31 addListener(listener) | 33 addListener(listener) |
32 { | 34 { |
33 if (this._listeners.indexOf(listener) == -1) | 35 this._listeners.add(listener); |
34 this._listeners.push(listener); | |
35 }, | 36 }, |
36 removeListener(listener) | 37 removeListener(listener) |
37 { | 38 { |
38 let idx = this._listeners.indexOf(listener); | 39 this._listeners.delete(listener); |
39 if (idx != -1) | |
40 this._listeners.splice(idx, 1); | |
41 }, | 40 }, |
42 _dispatch() | 41 _dispatch(...args) |
43 { | 42 { |
44 let results = []; | 43 let results = []; |
45 let listeners = this._listeners.slice(); | |
46 | 44 |
47 for (let listener of listeners) | 45 for (let listener of this._listeners) |
48 results.push(listener.apply(null, arguments)); | 46 results.push(listener(...args)); |
49 | 47 |
50 return results; | 48 return results; |
51 } | 49 } |
52 }; | 50 }; |
53 } | 51 |
| 52 // Workaround since HTMLCollection and NodeList didn't have iterator support |
| 53 // before Chrome 51. |
| 54 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699 |
| 55 let arrayIterator = Array.prototype[Symbol.iterator]; |
| 56 if (!(Symbol.iterator in HTMLCollection.prototype)) |
| 57 HTMLCollection.prototype[Symbol.iterator] = arrayIterator; |
| 58 if (!(Symbol.iterator in NodeList.prototype)) |
| 59 NodeList.prototype[Symbol.iterator] = arrayIterator; |
| 60 |
| 61 /* Message passing */ |
| 62 |
| 63 ext.onMessage = new ext._EventTarget(); |
| 64 |
| 65 |
| 66 /* Background page */ |
| 67 |
| 68 ext.backgroundPage = { |
| 69 sendMessage: chrome.runtime.sendMessage, |
| 70 getWindow() |
| 71 { |
| 72 return chrome.extension.getBackgroundPage(); |
| 73 } |
| 74 }; |
| 75 |
| 76 |
| 77 /* Utils */ |
| 78 |
| 79 ext.getURL = chrome.extension.getURL; |
| 80 ext.i18n = chrome.i18n; |
| 81 }()); |
OLD | NEW |