From 5998b6c0b2e427fe5afca8540622de3acca77c30 Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Fri, 13 Mar 2020 00:13:00 +0100 Subject: [PATCH] Functions for sending to specific frame/port --- src/ext/lib/comms/CommsServer.js | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/ext/lib/comms/CommsServer.js b/src/ext/lib/comms/CommsServer.js index e2d8b56..3d273a6 100644 --- a/src/ext/lib/comms/CommsServer.js +++ b/src/ext/lib/comms/CommsServer.js @@ -186,13 +186,21 @@ class CommsServer { } } - async sendToContentScripts(message, tab, frame) { + // if port is NOT defined, send to all content scripts of a given frame + // if port is defined, send just to that particular script of a given frame + async sendToFrameContentScripts(message, tab, frame, port) { + if (port !== undefined) { + // note: 'port' is _not_ shadowed here. + this.ports[tab][frame][port].postMessage(message); + return; + } for (const port in this.ports[tab][frame]) { + // note: 'port' is shadowed here! this.ports[tab][frame][port].postMessage(message); } } - async sendToFrame(message, tab, frame) { + async sendToFrame(message, tab, frame, port) { this.logger.log('info', 'comms', `%c[CommsServer::sendToFrame] attempting to send message to tab ${tab}, frame ${frame}`, "background: #dda; color: #11D", message); if (isNaN(tab)) { @@ -210,12 +218,18 @@ class CommsServer { this.logger.log('info', 'comms', `%c[CommsServer::sendToFrame] attempting to send message to tab ${tab}, frame ${frame}`, "background: #dda; color: #11D", message); try { - this.sendToContentScripts(message, tab, frame); + this.sendToFrameContentScripts(message, tab, frame, port); } catch (e) { this.logger.log('error', 'comms', `%c[CommsServer::sendToFrame] Sending message failed. Reason:`, "background: #dda; color: #11D", e); } } + async sendToAllFrames(message, tab, port) { + for (const frame in this.ports[tab]) { + this.sendToFrameContentScripts(message, tab, frame, port); + } + } + async sendToActive(message) { this.logger.log('info', 'comms', "%c[CommsServer::sendToActive] trying to send a message to active tab. Message:", "background: #dda; color: #11D", message); @@ -227,7 +241,7 @@ class CommsServer { } for (const frame in this.ports[tabs[0].id]) { - this.sendToContentScripts(message, tabs[0].id, frame); + this.sendToFrameContentScripts(message, tabs[0].id, frame); } } @@ -286,6 +300,9 @@ class CommsServer { async handleMessage(message, portOrSender, sendResponse) { await this.execCmd(message, portOrSender, sendResponse); + if (message.forwardToSameFramePort) { + this.sendToFrameContentScripts(message, portOrSender.tab.id, portOrSender.frameId, message.port) + } 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);