From d2c1b2de3eb5e262d57ab562a9326c632adc59fc Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Wed, 15 Jan 2020 21:20:34 +0100 Subject: [PATCH] Get current site more reliably (hopefully) --- src/ext/lib/comms/CommsServer.js | 2 +- src/ext/uw-bg.js | 32 +++++++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/ext/lib/comms/CommsServer.js b/src/ext/lib/comms/CommsServer.js index 32d050d..1b1da0e 100644 --- a/src/ext/lib/comms/CommsServer.js +++ b/src/ext/lib/comms/CommsServer.js @@ -154,7 +154,7 @@ class CommsServer { if (message.cmd === 'get-current-site') { port.postMessage({ cmd: 'set-current-site', - site: this.server.getVideoTab(), + site: await this.server.getVideoTab(), tabHostname: await this.getCurrentTabHostname() }); } diff --git a/src/ext/uw-bg.js b/src/ext/uw-bg.js index 3045bf1..974cfa8 100644 --- a/src/ext/uw-bg.js +++ b/src/ext/uw-bg.js @@ -33,7 +33,8 @@ class UWServer { // logger is the first thing that goes up this.logger = new Logger({ logToFile: false, - logToConsole: false + logToConsole: true, + logAll: true, }); await this.logger.init(); @@ -236,12 +237,33 @@ class UWServer { this.selectedSubitem[menu] = subitem; } - getVideoTab() { + async getCurrentTab() { + if (BrowserDetect.firefox) { + return (await browser.tabs.query({active: true, currentWindow: true}))[0]; + } else if (BrowserDetect.chrome) { + return new Promise((resolve, reject) => chrome.tabs.query({active: true, currentWindow: true}, (x) => resolve(x[0]))); + } + } + + async getVideoTab() { // friendly reminder: if current tab doesn't have a video, // there won't be anything in this.videoTabs[this.currentTabId] - if (this.videoTabs[this.currentTabId]) { + + const ctab = await this.getCurrentTab(); + + console.log('Current tab:', ctab); + + if (!ctab || !ctab.id) { return { - ...this.videoTabs[this.currentTabId], + host: 'INVALID SITE', + frames: [], + } + } + + if (this.videoTabs[ctab.id]) { + return { + ...this.videoTabs[ctab.id], + host: this.extractHostname(ctab.url), selected: this.selectedSubitem }; } @@ -249,7 +271,7 @@ class UWServer { // return something more or less empty if this tab doesn't have // a video registered for it return { - host: this.currentSite, + host: this.extractHostname(ctab.url), frames: [], selected: this.selectedSubitem }