LEFT | RIGHT |
1 (function() { | 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2013 Eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 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/>. |
| 16 */ |
| 17 |
| 18 (function() |
| 19 { |
2 /* Events */ | 20 /* Events */ |
3 | 21 |
4 WrappedEventTarget = function(target) { | 22 WrappedEventTarget = function(target) |
| 23 { |
5 this._listeners = []; | 24 this._listeners = []; |
6 this._wrappedListeners = []; | 25 this._wrappedListeners = []; |
7 this._target = target; | 26 this._target = target; |
8 }; | 27 }; |
9 WrappedEventTarget.prototype = { | 28 WrappedEventTarget.prototype = { |
10 _prepareExtraArguments: function() { | 29 _prepareExtraArguments: function() |
| 30 { |
11 return []; | 31 return []; |
12 }, | 32 }, |
13 addListener: function(listener) { | 33 addListener: function(listener) |
14 var extaArgs = Array.prototype.slice.call(arguments, 1); | 34 { |
15 extaArgs = this._prepareExtraArguments.apply(this, extaArgs); | 35 var extraArgs = Array.prototype.slice.call(arguments, 1); |
| 36 extraArgs = this._prepareExtraArguments.apply(this, extraArgs); |
16 | 37 |
17 var wrappedListener = this._wrapListener(listener); | 38 var wrappedListener = this._wrapListener(listener); |
18 this._listeners.push(listener); | 39 this._listeners.push(listener); |
19 this._wrappedListeners.push(wrappedListener); | 40 this._wrappedListeners.push(wrappedListener); |
20 | 41 |
21 this._target.addListener.apply(this._target, [wrappedListener].concat(exta
Args)); | 42 this._target.addListener.apply(this._target, [wrappedListener].concat(extr
aArgs)); |
22 }, | 43 }, |
23 removeListener: function(listener) { | 44 removeListener: function(listener) |
| 45 { |
24 var idx = this._listeners.indexOf(listener); | 46 var idx = this._listeners.indexOf(listener); |
25 | 47 |
26 if (idx != -1) { | 48 if (idx != -1) { |
27 this._target.removeListener(this._wrappedListeners[idx]); | 49 this._target.removeListener(this._wrappedListeners[idx]); |
28 | 50 |
29 this._listeners.splice(idx, 1); | 51 this._listeners.splice(idx, 1); |
30 this._wrappedListeners.splice(idx, 1); | 52 this._wrappedListeners.splice(idx, 1); |
31 } | 53 } |
32 } | 54 } |
33 }; | 55 }; |
34 | 56 |
35 var MessageEventTarget = function() { | 57 var MessageEventTarget = function() |
| 58 { |
36 WrappedEventTarget.call(this, chrome.runtime.onMessage); | 59 WrappedEventTarget.call(this, chrome.runtime.onMessage); |
37 }; | 60 }; |
38 MessageEventTarget.prototype = { | 61 MessageEventTarget.prototype = { |
39 __proto__: WrappedEventTarget.prototype, | 62 __proto__: WrappedEventTarget.prototype, |
40 _wrapListener: function(listener) { | 63 _wrapListener: function(listener) { |
41 return function(message, sender, sendResponse) { | 64 return function(message, sender, sendResponse) |
| 65 { |
42 listener(message, {tab: sender.tab && new Tab(sender.tab)}, sendResponse
); | 66 listener(message, {tab: sender.tab && new Tab(sender.tab)}, sendResponse
); |
43 }; | 67 }; |
44 } | 68 } |
45 }; | 69 }; |
46 | 70 |
47 /* API */ | 71 /* API */ |
48 | 72 |
49 ext = { | 73 ext = { |
50 backgroundPage: { | 74 backgroundPage: { |
51 sendMessage: function(message, responseCallback) { | 75 sendMessage: function(message, responseCallback) |
| 76 { |
52 chrome.runtime.sendMessage(message, responseCallback); | 77 chrome.runtime.sendMessage(message, responseCallback); |
53 }, | 78 }, |
54 getDOMWindow: function() { | 79 getWindow: function() |
| 80 { |
55 return chrome.extension.getBackgroundPage(); | 81 return chrome.extension.getBackgroundPage(); |
56 } | 82 } |
57 }, | 83 }, |
58 getURL: chrome.extension.getURL, | 84 getURL: chrome.extension.getURL, |
59 onMessage: new MessageEventTarget(), | 85 onMessage: new MessageEventTarget(), |
60 i18n: chrome.i18n | 86 i18n: chrome.i18n |
61 }; | 87 }; |
62 })(); | 88 })(); |
LEFT | RIGHT |