From a0388689cf9497a063ee2f0111f893f0666d7eb4 Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Sun, 21 Aug 2022 22:46:06 +0200 Subject: [PATCH] Continue slowly working on unified event bus --- src/common/js/ChromeShittinessMitigations.ts | 14 +- src/ext/UWServer.ts | 51 +++++- src/ext/lib/EventBus.ts | 13 +- src/ext/lib/comms/CommsClient.ts | 30 ++- src/ext/lib/comms/CommsPopupClient.ts | 0 src/ext/lib/comms/CommsServer.ts | 181 +++++++------------ src/popup/App.vue | 6 +- 7 files changed, 152 insertions(+), 143 deletions(-) create mode 100644 src/ext/lib/comms/CommsPopupClient.ts diff --git a/src/common/js/ChromeShittinessMitigations.ts b/src/common/js/ChromeShittinessMitigations.ts index a1d9ca9..2082e4b 100644 --- a/src/common/js/ChromeShittinessMitigations.ts +++ b/src/common/js/ChromeShittinessMitigations.ts @@ -2,16 +2,16 @@ * For some reason, Chrome really doesn't like when chrome.runtime * methods are wrapped inside a ES6 proxy object. If 'port' is a * ES6 Proxy of a Port object that `chrome.runtime.connect()` creates, - * then Chrome will do bullshits like `port.sendMessage` and + * then Chrome will do bullshits like `port.sendMessage` and * `port.onMessage.addListener` crashing your Vue3 UI with bullshits * excuses, e.g. - * + * * | TypeError: Illegal invocation. Function must be called on * | an object of type Port - * + * * which is some grade A bullshit because Firefox can handle that just * fine. - * + * * There's two ways how I could handle this: * * Find out how to get the original object from the proxy Vue3 * creates, which would take time and ruin my xmass holiday, or @@ -19,8 +19,8 @@ * the very real possibility that there's prolly a reason Chrome * does things in its own very special(tm) way, as if it had one * extra chromosome over Firefox. - * - * Easy chhoice, really. + * + * Easy choice, really. */ export class ChromeShittinessMitigations { static port = null; @@ -30,4 +30,4 @@ export class ChromeShittinessMitigations { } } -export default ChromeShittinessMitigations; \ No newline at end of file +export default ChromeShittinessMitigations; diff --git a/src/ext/UWServer.ts b/src/ext/UWServer.ts index 5948e9a..f11b933 100644 --- a/src/ext/UWServer.ts +++ b/src/ext/UWServer.ts @@ -44,12 +44,22 @@ export default class UWServer { }], 'replace-css': [{ function: (message, context) => this.replaceCss(message.oldCssString, message.newCssString, context.comms.sender) + }], + 'get-current-site': [{ + function: (message, context) => this.getCurrentSite() }] }; private gcTimeout: any; uiLoggerInitialized: boolean = false; + + //#region getters + get activeTab() { + return browser.tabs.query({currentWindow: true, active: true}); + } + //#endregion + constructor() { this.setup(); } @@ -227,10 +237,26 @@ export default class UWServer { this.selectedSubitem[menu] = subitem; } + async getCurrentSite() { + this.eventBus.send( + 'set-current-site', + { + site: await this.getVideoTab(), + tabHostname: await this.getCurrentTabHostname(), + }, + { + comms: { + forwardTo: 'popup' + } + } + ) + } + async getCurrentTab() { return (await browser.tabs.query({active: true, currentWindow: true}))[0]; } + async getVideoTab() { // friendly reminder: if current tab doesn't have a video, // there won't be anything in this.videoTabs[this.currentTabId] @@ -279,8 +305,27 @@ export default class UWServer { } } - // chrome shitiness mitigation - sendUnmarkPlayer(message) { - this.comms.sendUnmarkPlayer(message); + async getCurrentTabHostname() { + const activeTab = await this.activeTab; + + if (!activeTab || activeTab.length < 1) { + this.logger.log('warn', 'comms', 'There is no active tab for some reason. activeTab:', activeTab); + } + + const url = activeTab[0].url; + + var hostname; + + if (url.indexOf("://") > -1) { //find & remove protocol (http, ftp, etc.) and get hostname + hostname = url.split('/')[2]; + } + else { + hostname = url.split('/')[0]; + } + + hostname = hostname.split(':')[0]; //find & remove port number + hostname = hostname.split('?')[0]; //find & remove "?" + + return hostname; } } diff --git a/src/ext/lib/EventBus.ts b/src/ext/lib/EventBus.ts index 3af19be..be37c27 100644 --- a/src/ext/lib/EventBus.ts +++ b/src/ext/lib/EventBus.ts @@ -14,7 +14,8 @@ export interface EventBusContext { comms?: { sender?: any, port?: any, - forwardTo?: 'all' | 'active' | 'contentScript' | 'sameOrigin', + frame?: any, + forwardTo?: 'all' | 'active' | 'contentScript' | 'server' | 'sameOrigin' | 'popup', } } @@ -23,7 +24,7 @@ export default class EventBus { private commands: { [x: string]: EventBusCommand[]} = {}; private downstreamBuses: EventBus[] = []; private upstreamBus?: EventBus; - private comms?: CommsClient; + private comms?: CommsClient | CommsServer; //#region lifecycle destroy() { @@ -34,7 +35,7 @@ export default class EventBus { } //#endregion - setComms(comms: CommsClient) { + setComms(comms: CommsClient | CommsServer) { this.comms = comms; } @@ -86,7 +87,7 @@ export default class EventBus { } if (this.comms && !context?.fromComms) { - this.comms.sendMessage({command, config}); + this.comms.sendMessage({command, config}, context); } if (context?.stopPropagation) { @@ -114,7 +115,7 @@ export default class EventBus { } - sendDownstream(command: string, config: any, context?: EventBusContext, sourceEventBus?: EventBus) { + private sendDownstream(command: string, config: any, context?: EventBusContext, sourceEventBus?: EventBus) { for (const eventBus of this.downstreamBuses) { if (eventBus !== sourceEventBus) { // prevent eventBus.send from auto-propagating the command @@ -124,7 +125,7 @@ export default class EventBus { } } - sendUpstream(command: string, config: any, context?: EventBusContext) { + private sendUpstream(command: string, config: any, context?: EventBusContext) { if (this.upstreamBus) { // prevent eventBus.send from auto-propagating the command this.upstreamBus.send(command, config, {...context, stopPropagation: true}); diff --git a/src/ext/lib/comms/CommsClient.ts b/src/ext/lib/comms/CommsClient.ts index 97e00a9..cc173f6 100644 --- a/src/ext/lib/comms/CommsClient.ts +++ b/src/ext/lib/comms/CommsClient.ts @@ -25,22 +25,34 @@ if (process.env.CHANNEL !== 'stable'){ * * EventBus is started first. Other components (including commsClient) follow later. * + * Messages that pass through CommsServer need to define context object with + * context.comms.forwardTo field defined, with one of the following values: + * + * - all : all content scripts of ALL TABS + * - active : all content scripts in CURRENT TAB + * - contentScript : specific content script (requires other EventBusContext fields!) + * - backgroundScript : background script (considered default behaviour) + * - sameOrigin : ??? + * - popup : extension popup + * + * + * * * fig 0. ULTRAWIDIFY COMMUNICATION MAP * * CS EVENT BUS * (accessible within tab scripts) - * | NOT EVENT BUS - * PageInfo x (accessible within popup) + * | BG EVENT BUS + * PageInfo x (accessible within background page) * x | * : : x UWServer * x CommsClient <---------------x CommsServer x - * | (Connect to popup) - * | - * x eventBus.sendToTunnel() - *