From 3aabd298ec63880a51f81c975c2bec2bbbd76f1b Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Sun, 31 Jul 2022 01:12:54 +0200 Subject: [PATCH] fix various fuckies wuckies --- src/ext/UWContent.ts | 2 ++ src/ext/UWServer.ts | 10 ++++-- src/ext/lib/ActionHandler.ts | 2 +- src/ext/lib/EventBus.ts | 43 ++++++++++++-------------- src/ext/lib/comms/CommsClient.ts | 1 - src/ext/lib/comms/CommsServer.ts | 4 +-- src/ext/lib/video-data/PageInfo.ts | 21 +++---------- src/ext/lib/video-data/VideoData.ts | 3 -- src/ext/lib/video-transform/Resizer.ts | 2 +- 9 files changed, 37 insertions(+), 51 deletions(-) diff --git a/src/ext/UWContent.ts b/src/ext/UWContent.ts index 6235bd3..6f44588 100644 --- a/src/ext/UWContent.ts +++ b/src/ext/UWContent.ts @@ -100,6 +100,8 @@ export default class UWContent { } ); this.comms = new CommsClient('content-main-port', this.logger, this.eventBus); + this.eventBus.setComms(this.comms); + this.initPhase2(); } catch (e) { diff --git a/src/ext/UWServer.ts b/src/ext/UWServer.ts index 47da6e8..5948e9a 100644 --- a/src/ext/UWServer.ts +++ b/src/ext/UWServer.ts @@ -75,11 +75,15 @@ 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) { + for (const command of this.eventBusCommands[action]) { + this.eventBus.subscribe(action, command); + } + } + this.comms = new CommsServer(this); - - this.comms.subscribe('emit-logs', () => {}); // we don't need to do anything, this gets forwarded to UI content script as is - browser.tabs.onActivated.addListener((m) => {this.onTabSwitched(m)}); } catch (e) { console.error(`Ultrawidify [server]: failed to start. Reason:`, e); diff --git a/src/ext/lib/ActionHandler.ts b/src/ext/lib/ActionHandler.ts index d6da2fa..cc7d0c4 100644 --- a/src/ext/lib/ActionHandler.ts +++ b/src/ext/lib/ActionHandler.ts @@ -204,7 +204,7 @@ class ActionHandler { for (const command of this.commands) { if (this.isActionMatch(command.shortcut, event, isLatin)) { - this.eventBus.sendGlobal(command.action, command.arguments); + this.eventBus.send(command.action, command.arguments); } } } catch (e) { diff --git a/src/ext/lib/EventBus.ts b/src/ext/lib/EventBus.ts index 5cbe7f5..3af19be 100644 --- a/src/ext/lib/EventBus.ts +++ b/src/ext/lib/EventBus.ts @@ -34,7 +34,7 @@ export default class EventBus { } //#endregion - setComms(comms: CommsClient): void { + setComms(comms: CommsClient) { this.comms = comms; } @@ -78,19 +78,24 @@ export default class EventBus { } send(command: string, config: any, context?: EventBusContext) { - if (!this.commands ||!this.commands[command]) { - // ensure send is not being called for commands that we have no subscriptions for + // execute commands we have subscriptions for + if (this.commands?.[command]) { + for (const eventBusCommand of this.commands[command]) { + eventBusCommand.function(config, context); + } + } + + if (this.comms && !context?.fromComms) { + this.comms.sendMessage({command, config}); + } + + if (context?.stopPropagation) { return; } - for (const eventBusCommand of this.commands[command]) { - eventBusCommand.function(config, context); - - if (eventBusCommand.isGlobal && !context?.stopPropagation) { - this.sendUpstream(command, config, context); - this.sendDownstream(command, config, context); - } - } + // propagate commands across the bus + this.sendUpstream(command, config, context); + this.sendDownstream(command, config, context); } /** @@ -109,17 +114,11 @@ export default class EventBus { } - sendGlobal(command: string, config: any, context?: EventBusContext) { - this.send(command, config); - this.sendUpstream(command, config); - this.sendDownstream(command, config); - } - - sendDownstream(command: string, config: any, context?: EventBusContext, sourceEventBus?: EventBus) { for (const eventBus of this.downstreamBuses) { if (eventBus !== sourceEventBus) { - eventBus.send(command, config); + // prevent eventBus.send from auto-propagating the command + eventBus.send(command, config, {...context, stopPropagation: true}); eventBus.sendDownstream(command, config); } } @@ -127,12 +126,10 @@ export default class EventBus { sendUpstream(command: string, config: any, context?: EventBusContext) { if (this.upstreamBus) { - this.upstreamBus.send(command, config, context); + // prevent eventBus.send from auto-propagating the command + this.upstreamBus.send(command, config, {...context, stopPropagation: true}); this.upstreamBus.sendUpstream(command, config, context); this.upstreamBus.sendDownstream(command, config, context, this); } - if (!this.upstreamBus && this.comms && !context?.fromComms) { - this.comms.sendMessage({command, config}); - } } } diff --git a/src/ext/lib/comms/CommsClient.ts b/src/ext/lib/comms/CommsClient.ts index 08b49ef..97e00a9 100644 --- a/src/ext/lib/comms/CommsClient.ts +++ b/src/ext/lib/comms/CommsClient.ts @@ -68,7 +68,6 @@ class CommsClient { try { this.logger = logger; this.eventBus = eventBus; - this.eventBus.setComms(this); this.port = browser.runtime.connect(null, {name: name}); diff --git a/src/ext/lib/comms/CommsServer.ts b/src/ext/lib/comms/CommsServer.ts index 1aeeef9..ad01071 100644 --- a/src/ext/lib/comms/CommsServer.ts +++ b/src/ext/lib/comms/CommsServer.ts @@ -222,13 +222,13 @@ class CommsServer { async processReceivedMessage(message, port){ this.logger.log('info', 'comms', "[CommsServer.js::processReceivedMessage] Received message from popup/content script!", message, "port", port); - this.eventBus.send(message, {port, fromComms: true}); + this.eventBus.send(message.command, message.config, {comms: {port}, fromComms: true}); } processReceivedMessage_nonpersistent(message, sender){ this.logger.log('info', 'comms', "%c[CommsServer.js::processMessage_nonpersistent] Received message from background script!", "background-color: #11D; color: #aad", message, sender); - this.eventBus.send(message, {sender, fromComms: true}); + this.eventBus.send(message.command, message.config, {comms: {sender}, fromComms: true}); } // chrome shitiness mitigation diff --git a/src/ext/lib/video-data/PageInfo.ts b/src/ext/lib/video-data/PageInfo.ts index f4edade..8faa1bf 100644 --- a/src/ext/lib/video-data/PageInfo.ts +++ b/src/ext/lib/video-data/PageInfo.ts @@ -67,7 +67,7 @@ class PageInfo { actionHandler: any; //#endregion - constructor(comms, settings, logger, extensionMode, readOnly = false){ + constructor(eventBus: EventBus, settings: Settings, logger: Logger, extensionMode, readOnly = false){ this.logger = logger; this.settings = settings; @@ -75,9 +75,8 @@ class PageInfo { this.extensionMode = extensionMode; this.readOnly = readOnly; - - if (comms){ - this.comms = comms; + if (eventBus){ + this.eventBus = eventBus; } try { @@ -88,18 +87,6 @@ class PageInfo { // do nothing. It's ok if there's no special settings for the player element or crop persistence } - // try getting default crop immediately. - // const cropModePersistence = this.settings.getDefaultCropPersistenceMode(window.location.hostname); - - // try { - // if (cropModePersistence === CropModePersistence.Forever) { - // this.defaultCrop = this.settings.active.sites[window.location.hostname].defaultCrop; - // } else if (cropModePersistence === CropModePersistence.CurrentSession) { - // this.defaultCrop = JSON.parse(sessionStorage.getItem('uw-crop-mode-session-persistence')); - // } - // } catch (e) { - // // do nothing. It's ok if there's no special settings for the player element or crop persistence - // } this.currentCrop = this.defaultCrop; this.rescan(RescanReason.PERIODIC); @@ -248,7 +235,7 @@ class PageInfo { // if we're left without videos on the current page, we unregister the page. // if we have videos, we call register. - if (this.comms) { + if (this.eventBus) { // We used to send "register video" requests only on the first load, or if the number of // videos on the page has changed. However, since Chrome Web Store started to require every // extension requiring "broad permissions" to undergo manual review diff --git a/src/ext/lib/video-data/VideoData.ts b/src/ext/lib/video-data/VideoData.ts index 270105b..24e9ad2 100644 --- a/src/ext/lib/video-data/VideoData.ts +++ b/src/ext/lib/video-data/VideoData.ts @@ -65,8 +65,6 @@ class VideoData { } constructor(video, settings, pageInfo){ - (window as any).ultrawidify.addVideo(this); - this.logger = pageInfo.logger; this.arSetupComplete = false; this.video = video; @@ -98,7 +96,6 @@ class VideoData { }}); } - this.setupStageOne(); } diff --git a/src/ext/lib/video-transform/Resizer.ts b/src/ext/lib/video-transform/Resizer.ts index e1a633f..c19f701 100644 --- a/src/ext/lib/video-transform/Resizer.ts +++ b/src/ext/lib/video-transform/Resizer.ts @@ -141,7 +141,7 @@ class Resizer { } replaceCss(oldCssString, newCssString) { - this.eventBus.send('replace-css', {oldCss: oldCssString, newCss: newCssString}); + this.eventBus.send('replace-css', {oldCssString, newCssString}); } prepareCss(css) {