Index: chrome/common.js |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/chrome/common.js |
@@ -0,0 +1,79 @@ |
+/* |
+ * This file is part of Adblock Plus <http://adblockplus.org/>, |
+ * Copyright (C) 2006-2013 Eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+(function() { |
+ /* Events */ |
Felix Dahlke
2013/10/24 16:30:34
As in other places, opening braces should go on th
|
+ |
+ WrappedEventTarget = function(target) { |
+ this._listeners = []; |
+ this._wrappedListeners = []; |
+ this._target = target; |
+ }; |
+ WrappedEventTarget.prototype = { |
+ _prepareExtraArguments: function() { |
+ return []; |
+ }, |
+ addListener: function(listener) { |
+ var extaArgs = Array.prototype.slice.call(arguments, 1); |
Felix Dahlke
2013/10/24 16:30:34
s/extaArgs/extraArgs/?
|
+ extaArgs = this._prepareExtraArguments.apply(this, extaArgs); |
+ |
+ var wrappedListener = this._wrapListener(listener); |
+ this._listeners.push(listener); |
+ this._wrappedListeners.push(wrappedListener); |
+ |
+ this._target.addListener.apply(this._target, [wrappedListener].concat(extaArgs)); |
+ }, |
+ removeListener: function(listener) { |
+ var idx = this._listeners.indexOf(listener); |
+ |
+ if (idx != -1) { |
+ this._target.removeListener(this._wrappedListeners[idx]); |
+ |
+ this._listeners.splice(idx, 1); |
+ this._wrappedListeners.splice(idx, 1); |
+ } |
+ } |
+ }; |
+ |
+ var MessageEventTarget = function() { |
+ WrappedEventTarget.call(this, chrome.runtime.onMessage); |
+ }; |
+ MessageEventTarget.prototype = { |
+ __proto__: WrappedEventTarget.prototype, |
+ _wrapListener: function(listener) { |
+ return function(message, sender, sendResponse) { |
+ listener(message, {tab: sender.tab && new Tab(sender.tab)}, sendResponse); |
+ }; |
+ } |
+ }; |
+ |
+ /* API */ |
+ |
+ ext = { |
+ backgroundPage: { |
+ sendMessage: function(message, responseCallback) { |
+ chrome.runtime.sendMessage(message, responseCallback); |
+ }, |
+ getDOMWindow: function() { |
+ return chrome.extension.getBackgroundPage(); |
+ } |
+ }, |
+ getURL: chrome.extension.getURL, |
+ onMessage: new MessageEventTarget(), |
+ i18n: chrome.i18n |
+ }; |
+})(); |