Index: background.js |
diff --git a/background.js b/background.js |
index fa78123054da5bb29352923c3496ec8a049eecde..59e1a13b93b4bdd5a3f92c58c0f8354c1cc18e3a 100644 |
--- a/background.js |
+++ b/background.js |
@@ -337,6 +337,58 @@ |
reinitialized: params.filterlistsReinitialized |
}; |
+ modules.messaging = { |
+ port: { |
+ _eventEmitter: new EventEmitter(), |
Sebastian Noack
2017/01/16 15:00:03
Couldn't you do it like that:
modules.messaging
kzar
2017/01/16 15:16:31
Done.
|
+ on(name, callback) { this._eventEmitter.on(name, callback); }, |
+ off(name, callback) { this._eventEmitter.off(name, callback); }, |
+ _onMessage(event) |
+ { |
+ if (event.data.type != "message") |
+ return; |
+ let message = event.data.payload; |
+ let messageId = event.data.messageId; |
+ let sender = { |
+ page: new ext.Page(event.source) |
+ }; |
+ |
+ function reply(message) |
+ { |
+ event.source.postMessage({ |
+ type: "response", |
+ messageId: messageId, |
+ payload: message |
+ }, "*"); |
+ } |
+ |
+ let port = modules.messaging.port; |
+ let listeners = port._eventEmitter._listeners[message.type]; |
+ if (!listeners) |
+ return; |
+ |
+ for (let listener of listeners) |
+ { |
+ let response = listener(message, sender); |
+ if (response && typeof response.then == "function") |
+ { |
+ response.then( |
+ reply, |
+ reason => { |
+ console.error(reason); |
+ reply(undefined); |
+ } |
+ ); |
+ } |
+ else if (typeof response != "undefined") |
+ { |
+ reply(response); |
+ } |
+ } |
+ } |
+ } |
+ }; |
+ window.addEventListener("message", modules.messaging.port._onMessage); |
+ |
global.Services = { |
vc: { |
compare: function(v1, v2) |