diff --git a/js/lib/Comms.js b/js/lib/Comms.js index 017515b..225e3e1 100644 --- a/js/lib/Comms.js +++ b/js/lib/Comms.js @@ -128,6 +128,10 @@ class CommsClient { return Promise.resolve(true); } + registerTab() { + this.port.postMessage({cmd: "register-tab", url: location.hostname}); + } + registerVideo(){ this.port.postMessage({cmd: "has-video"}); } @@ -155,8 +159,24 @@ class CommsServer { } } - async getCurrentTabUrl() { + async getCurrentTabHostname() { + const activeTab = await this._getActiveTab(); + 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; } sendToAll(message){ @@ -224,7 +244,7 @@ class CommsServer { }); } - processReceivedMessage(message, port){ + async processReceivedMessage(message, port){ if (Debug.debug && Debug.comms) { console.log("[CommsServer.js::processMessage] Received message from background script!", message, "port", port, "\nsettings and server:", this.settings,this.server); } @@ -233,6 +253,25 @@ class CommsServer { port.postMessage({cmd: 'set-current-site', site: this.server.currentSite}); } + if(message.cmd === 'register-tab') { + if(Debug.debug) { // we want to get these messages always when debugging + console.log("[Comms::processReceivedMessage] registering tab with hostname", message.url) + } + + const currentUrl = await this.getCurrentTabHostname(); + if (message.url === currentUrl) { + this.server.url = message.url; + + if(Debug.debug) { // we want to get these messages always when debugging + console.log("[Comms::processReceivedMessage] hostname matches currently active tab. active:", currentUrl, "message:", message.url); + } + } else { + if(Debug.debug) { // we want to get these messages always when debugging + console.log("[Comms::processReceivedMessage] hostnames don't match. active:", currentUrl, "message:", message.url); + } + } + } + if (message.cmd === 'get-config') { if(Debug.debug) { console.log("CommsServer: received get-config. Active settings?", this.settings.active, "\n(settings:", this.settings, ")") diff --git a/js/modules/PageInfo.js b/js/modules/PageInfo.js index f9495ed..c0d30a8 100644 --- a/js/modules/PageInfo.js +++ b/js/modules/PageInfo.js @@ -18,8 +18,11 @@ class PageInfo { } if(this.videos.length > 0){ + console.log("registering video") comms.registerVideo(); } + + comms.registerTab(); } destroy() { @@ -118,7 +121,6 @@ class PageInfo { this.videos = this.videos.filter( vid => vid.destroyed === false); } - scheduleRescan(rescanReason){ if(rescanReason != RescanReason.PERIODIC){ this.rescan(rescanReason);