diff options
author | hackademix | 2018-08-22 16:34:16 +0200 |
---|---|---|
committer | hackademix | 2018-08-22 16:34:16 +0200 |
commit | 075a5ad0e0f9b4f9af614194d9c8d21d0ed45184 (patch) | |
tree | cc341040d0515e54ec8466dec8d3e32c381f1457 /src/lib/Messages.js | |
parent | 48c04726b8d2da0023abd0ccef3e2d09511bea47 (diff) | |
download | noscript-075a5ad0e0f9b4f9af614194d9c8d21d0ed45184.tar.gz noscript-075a5ad0e0f9b4f9af614194d9c8d21d0ed45184.tar.xz noscript-075a5ad0e0f9b4f9af614194d9c8d21d0ed45184.zip |
More coherent wrapper around the webex messaging API.
Diffstat (limited to 'src/lib/Messages.js')
-rw-r--r-- | src/lib/Messages.js | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/src/lib/Messages.js b/src/lib/Messages.js index c87a8a8..fc7df6a 100644 --- a/src/lib/Messages.js +++ b/src/lib/Messages.js @@ -3,9 +3,9 @@ let handlers = new Set(); let dispatch = async (msg, sender) => { - let {action} = msg; + let {_messageName} = msg; for (let h of handlers) { - let f = h[action]; + let f = h[_messageName]; if (typeof f === "function") { return await f(msg, sender); } @@ -24,8 +24,17 @@ let originalSize = handlers.size; handlers.delete(handler); if (originalSize === 1 && handlers.size === 0) { - browser.runtime.onMessage.remveListener(dispatch); + browser.runtime.onMessage.removeListener(dispatch); } + }, + async send(name, args = {}, toContent = null) { + args._messageName = name; + if (toContent && "tabId" in toContent) { + let opts; + if ("frameId" in toContent) opts = {frameId: toContent.frameId}; + return await browser.tabs.sendMessage(toContent.tabId, args, opts); + } + return await browser.runtime.sendMessage(args); } } } |