From c26744b9d9b9cf1c7d88b7744f08388fd9994c90 Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Sun, 8 Mar 2020 16:49:20 +0100 Subject: [PATCH] handleMessage _always_ tries to execute command, even if it has forwarding flags set. Command is executed before forwarding. --- src/ext/lib/comms/CommsServer.js | 27 ++++++++++++++++++--------- src/ext/uw-bg.js | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/ext/lib/comms/CommsServer.js b/src/ext/lib/comms/CommsServer.js index adb5486..fc4197e 100644 --- a/src/ext/lib/comms/CommsServer.js +++ b/src/ext/lib/comms/CommsServer.js @@ -133,6 +133,15 @@ class CommsServer { } } + subscribe(command, callback) { + console.log("subscribing to command:", command, "with callback", callback) + if (!this.commands[command]) { + this.commands[command] = [callback]; + } else { + this.commands[command].push(callback); + } + } + async getCurrentTabHostname() { const activeTab = await this._getActiveTab(); @@ -243,35 +252,35 @@ class CommsServer { } - execCmd(message, portOrSender, sendResponse) { + async execCmd(message, portOrSender, sendResponse) { + console.log("got a command to exec:", message.cmd, message, this.commands[message.cmd], this.commands) this.logger.log( 'info', 'comms', '[CommsServer.js::execCmd] Received message', message, ". Port/sender:", portOrSender, "sendResponse:", sendResponse, "\nThere is ", this.commands[message.cmd]?.length ?? 0, " command(s) for action", message.cmd ); - for (const c of this.commands[message.cmd]) { - c(message, portOrSender, sendResponse); + if (this.commands[message.cmd]) { + for (const c of this.commands[message.cmd]) { + await c(message, portOrSender, sendResponse); + } } } - handleMessage(message, portOrSender, sendResponse) { + async handleMessage(message, portOrSender, sendResponse) { + await this.execCmd(message, portOrSender, sendResponse); + if (message.forwardToContentScript) { this.logger.log('info', 'comms', "[CommsServer.js::processReceivedMessage] Message has 'forward to content script' flag set. Forwarding message as is. Message:", message); this.sendToFrame(message, message.targetTab, message.targetFrame); - return; } if (message.forwardToAll) { this.logger.log('info', 'comms', "[CommsServer.js::processReceivedMessage] Message has 'forward to all' flag set. Forwarding message as is. Message:", message); this.sendToAll(message); - return; } if (message.forwardToActive) { this.logger.log('info', 'comms', "[CommsServer.js::processReceivedMessage] Message has 'forward to active' flag set. Forwarding message as is. Message:", message); this.sendToActive(message); - return; } - - this.execCmd(message, portOrSender, sendResponse); } async processReceivedMessage(message, port){ diff --git a/src/ext/uw-bg.js b/src/ext/uw-bg.js index 42cd0ce..562534d 100644 --- a/src/ext/uw-bg.js +++ b/src/ext/uw-bg.js @@ -49,6 +49,7 @@ class UWServer { this.settings = new Settings({logger: this.logger}); await this.settings.init(); this.comms = new CommsServer(this); + this.comms.subscribe('show-logger', async () => await this.initUi()); var ths = this; if(BrowserDetect.firefox) { @@ -198,6 +199,26 @@ class UWServer { this.selectedSubitem[menu] = subitem; } + async initUi() { + try { + if (BrowserDetect.firefox) { + console.log("") + browser.tabs.executeScript({ + file: '/ext/uw-ui.js', + allFrames: true, + }); + } else if (BrowserDetect.chrome) { + chrome.tabs.executeScript({ + file: '/ext/uw-ui.js', + allFrames: true, + }); + } + } catch (e) { + console.error("UI initialization failed. Reason:", e); + this.logger.log('ERROR', 'uwbg', 'UI initialization failed. Reason:', e); + } + } + async getCurrentTab() { if (BrowserDetect.firefox) { return (await browser.tabs.query({active: true, currentWindow: true}))[0];