OLD | NEW |
(Empty) | |
| 1 (function() { |
| 2 /* Events */ |
| 3 |
| 4 WrappedEventTarget = function(target) { |
| 5 this._listeners = []; |
| 6 this._wrappedListeners = []; |
| 7 this._target = target; |
| 8 }; |
| 9 WrappedEventTarget.prototype = { |
| 10 _prepareExtraArguments: function() { |
| 11 return []; |
| 12 }, |
| 13 addListener: function(listener) { |
| 14 var extaArgs = Array.prototype.slice.call(arguments, 1); |
| 15 extaArgs = this._prepareExtraArguments.apply(this, extaArgs); |
| 16 |
| 17 var wrappedListener = this._wrapListener(listener); |
| 18 this._listeners.push(listener); |
| 19 this._wrappedListeners.push(wrappedListener); |
| 20 |
| 21 this._target.addListener.apply(this._target, [wrappedListener].concat(exta
Args)); |
| 22 }, |
| 23 removeListener: function(listener) { |
| 24 var idx = this._listeners.indexOf(listener); |
| 25 |
| 26 if (idx != -1) { |
| 27 this._target.removeListener(this._wrappedListeners[idx]); |
| 28 |
| 29 this._listeners.splice(idx, 1); |
| 30 this._wrappedListeners.splice(idx, 1); |
| 31 } |
| 32 } |
| 33 }; |
| 34 |
| 35 var MessageEventTarget = function() { |
| 36 WrappedEventTarget.call(this, chrome.runtime.onMessage); |
| 37 }; |
| 38 MessageEventTarget.prototype = { |
| 39 __proto__: WrappedEventTarget.prototype, |
| 40 _wrapListener: function(listener) { |
| 41 return function(message, sender, sendResponse) { |
| 42 listener(message, {tab: sender.tab && new Tab(sender.tab)}, sendResponse
); |
| 43 }; |
| 44 } |
| 45 }; |
| 46 |
| 47 /* API */ |
| 48 |
| 49 ext = { |
| 50 backgroundPage: { |
| 51 sendMessage: function(message, responseCallback) { |
| 52 chrome.runtime.sendMessage(message, responseCallback); |
| 53 }, |
| 54 getDOMWindow: function() { |
| 55 return chrome.extension.getBackgroundPage(); |
| 56 } |
| 57 }, |
| 58 getURL: chrome.extension.getURL, |
| 59 onMessage: new MessageEventTarget(), |
| 60 i18n: chrome.i18n |
| 61 }; |
| 62 })(); |
OLD | NEW |