From 20576675ce5373d556fe9db02f9a776768be2681 Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Mon, 16 Jul 2018 22:30:52 +0200 Subject: [PATCH] Fixes for chrome --- js/lib/BrowserDetect.js | 21 +++++++++++++++--- js/lib/Comms.js | 47 +++++++++++++++++++---------------------- 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/js/lib/BrowserDetect.js b/js/lib/BrowserDetect.js index fa3b009..6923854 100644 --- a/js/lib/BrowserDetect.js +++ b/js/lib/BrowserDetect.js @@ -6,10 +6,24 @@ var _bd_isEdge = false; // we'll see if FF try{ // todo: find something that works in firefox but not in edge (or vice-versa) - _bd_isFirefox = true; - _bd_isEdge = false; + var browserinfo = browser.runtime.getBrowserInfo(); + + // we don't need to actually check because only firefox supports that. + // if we're not on firefox, the above call will probably throw an exception anyway. + // if browsers other than firefox start supporting that, well ... we'll also need to actually await for promise + // that getBrowserInfo() returns to resolve. + + // if (Browser.name.toLowerCase().indexOf(firefox) !== -1 || Browser.vendor.toLowerCase().indexOf(mozilla) !== -1) { + _bd_isFirefox = true; + _bd_isEdge = false; + // } + } -catch (e) {}; +catch (e) { + if(Debug.debug) { + console.info("[BrowserDetect] browser.runtime.getBrowserInfo() probably failed. This means we're probably not using firefox.", e) + } +}; if(typeof browser === "undefined"){ // This is a good sign we're in chrome or chromium-based browsers if(chrome){ @@ -17,6 +31,7 @@ if(typeof browser === "undefined"){ // This is a good sign we're in chrome or ch _bd_usebrowser = "chrome"; _bd_isChrome = true; _bd_isEdge = false; + _bd_isFirefox = false; } } diff --git a/js/lib/Comms.js b/js/lib/Comms.js index f24fc24..a856b22 100644 --- a/js/lib/Comms.js +++ b/js/lib/Comms.js @@ -1,10 +1,14 @@ if(Debug.debug){ - console.log("Loading Comms.js") + console.log("Loading Comms.js"); } class CommsClient { - constructor(name){ - this.port = browser.runtime.connect({name: name}); + constructor(name) { + if (BrowserDetect.firefox) { + this.port = browser.runtime.connect({name: name}); + } else if (BrowserDetect.chrome) { + this.port = chrome.runtime.connect({name: name}); + } var ths = this; this.port.onMessage.addListener(m => ths.processReceivedMessage(m)); @@ -158,20 +162,25 @@ class CommsServer { } } - sendToActive(message) { + async _getActiveTab() { + if (BrowserDetect.firefox) { + console.log("we firefox") + return await browser.tabs.query({currentWindow: true, active: true}); + } else { + return await new Promise( (resolve, reject) => { + chrome.tabs.query({currentWindow: true, active: true}, function (res) { + resolve(res); + }); + }); + } + } + + async sendToActive(message) { if(Debug.debug && Debug.comms){ console.log("%c[CommsServer::sendToActive] trying to send a message to active tab. Message:", "background: #dda; color: #11D", message); } - if(BrowserDetect.firefox){ - this._sendToActive_ff(message); - } else if (BrowserDetect.chrome) { - - } - } - - async _sendToActive_ff(message){ - var tabs = await browser.tabs.query({currentWindow: true, active: true}); + var tabs = await this._getActiveTab(); if(Debug.debug && Debug.comms){ console.log("[CommsServer::_sendToActive_ff] currently active tab(s)?", tabs); @@ -186,18 +195,6 @@ class CommsServer { } } - - async queryTabs_chrome(tabInfo){ - return new Promise(function (resolve, reject){ - browser.tabs.query(tabInfo, function(response){ - browser.tabs.query(tabInfo); - // Chrome/js shittiness mitigation — remove this line and an empty array will be returned - var r = response; - resolve(r); - }); - }); - } - onConnect(port){ var ths = this;