OLD | NEW |
(Empty) | |
| 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 { |
| 20 /* Events */ |
| 21 |
| 22 WrappedEventTarget = function(target) |
| 23 { |
| 24 this._listeners = []; |
| 25 this._wrappedListeners = []; |
| 26 this._target = target; |
| 27 }; |
| 28 WrappedEventTarget.prototype = { |
| 29 _prepareExtraArguments: function() |
| 30 { |
| 31 return []; |
| 32 }, |
| 33 addListener: function(listener) |
| 34 { |
| 35 var extraArgs = Array.prototype.slice.call(arguments, 1); |
| 36 extraArgs = this._prepareExtraArguments.apply(this, extraArgs); |
| 37 |
| 38 var wrappedListener = this._wrapListener(listener); |
| 39 this._listeners.push(listener); |
| 40 this._wrappedListeners.push(wrappedListener); |
| 41 |
| 42 this._target.addListener.apply(this._target, [wrappedListener].concat(extr
aArgs)); |
| 43 }, |
| 44 removeListener: function(listener) |
| 45 { |
| 46 var idx = this._listeners.indexOf(listener); |
| 47 |
| 48 if (idx != -1) { |
| 49 this._target.removeListener(this._wrappedListeners[idx]); |
| 50 |
| 51 this._listeners.splice(idx, 1); |
| 52 this._wrappedListeners.splice(idx, 1); |
| 53 } |
| 54 } |
| 55 }; |
| 56 |
| 57 var MessageEventTarget = function() |
| 58 { |
| 59 WrappedEventTarget.call(this, chrome.runtime.onMessage); |
| 60 }; |
| 61 MessageEventTarget.prototype = { |
| 62 __proto__: WrappedEventTarget.prototype, |
| 63 _wrapListener: function(listener) { |
| 64 return function(message, sender, sendResponse) |
| 65 { |
| 66 listener(message, {tab: sender.tab && new Tab(sender.tab)}, sendResponse
); |
| 67 }; |
| 68 } |
| 69 }; |
| 70 |
| 71 /* API */ |
| 72 |
| 73 ext = { |
| 74 backgroundPage: { |
| 75 sendMessage: function(message, responseCallback) |
| 76 { |
| 77 chrome.runtime.sendMessage(message, responseCallback); |
| 78 }, |
| 79 getWindow: function() |
| 80 { |
| 81 return chrome.extension.getBackgroundPage(); |
| 82 } |
| 83 }, |
| 84 getURL: chrome.extension.getURL, |
| 85 onMessage: new MessageEventTarget(), |
| 86 i18n: chrome.i18n |
| 87 }; |
| 88 })(); |
OLD | NEW |