From 740f2e8f6f84859e171b32e434c3fef093b95ce2 Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Tue, 20 Sep 2022 01:34:59 +0200 Subject: [PATCH] fix forwarding data between server, content script, and popup --- src/ext/UWServer.ts | 2 + src/ext/lib/EventBus.ts | 10 ++-- src/ext/lib/comms/CommsClient.ts | 25 ++++++++-- src/ext/lib/comms/CommsServer.ts | 57 ++++++++++++++++------ src/popup/App.vue | 84 ++++++++++++++++++++++---------- src/popup/popup.js | 2 +- 6 files changed, 130 insertions(+), 50 deletions(-) diff --git a/src/ext/UWServer.ts b/src/ext/UWServer.ts index f11b933..a91c6c4 100644 --- a/src/ext/UWServer.ts +++ b/src/ext/UWServer.ts @@ -84,6 +84,7 @@ export default class UWServer { this.settings = new Settings({logger: this.logger}); await this.settings.init(); + this.eventBus = new EventBus(); for (const action in this.eventBusCommands) { @@ -93,6 +94,7 @@ export default class UWServer { } this.comms = new CommsServer(this); + this.eventBus.setComms(this.comms); browser.tabs.onActivated.addListener((m) => {this.onTabSwitched(m)}); } catch (e) { diff --git a/src/ext/lib/EventBus.ts b/src/ext/lib/EventBus.ts index be37c27..40c5810 100644 --- a/src/ext/lib/EventBus.ts +++ b/src/ext/lib/EventBus.ts @@ -1,4 +1,4 @@ -import CommsClient from './comms/CommsClient'; +import CommsClient, { CommsOrigin } from './comms/CommsClient'; import CommsServer from './comms/CommsServer'; export interface EventBusCommand { @@ -10,7 +10,7 @@ export interface EventBusContext { stopPropagation?: boolean, // Context stuff added by Comms - fromComms?: boolean, + origin?: CommsOrigin, comms?: { sender?: any, port?: any, @@ -86,8 +86,10 @@ export default class EventBus { } } - if (this.comms && !context?.fromComms) { - this.comms.sendMessage({command, config}, context); + // preventing messages from flowing back to their original senders is + // CommsServer's job. EventBus does not have enough data for this decision. + if (this.comms) { + this.comms.sendMessage({command, config, context}, context); } if (context?.stopPropagation) { diff --git a/src/ext/lib/comms/CommsClient.ts b/src/ext/lib/comms/CommsClient.ts index cc173f6..18b3f59 100644 --- a/src/ext/lib/comms/CommsClient.ts +++ b/src/ext/lib/comms/CommsClient.ts @@ -3,7 +3,7 @@ import BrowserDetect from '../../conf/BrowserDetect'; import Logger from '../Logger'; import { browser } from 'webextension-polyfill-ts'; import Settings from '../Settings'; -import EventBus from '../EventBus'; +import EventBus, { EventBusContext } from '../EventBus'; if (process.env.CHANNEL !== 'stable'){ console.info("Loading CommsClient"); @@ -50,7 +50,7 @@ if (process.env.CHANNEL !== 'stable'){ * | (Connect to popup) X POPUP EVENT BUS * | A (accessible within popup) /todo * x eventBus.sendToTunnel() | | - *