From dd319551bc77207c17889441d2bad0495f686afd Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Sat, 26 May 2018 23:08:49 +0200 Subject: [PATCH] Comms minimally functional again, with one ugly workaround. --- js/conf/Debug.js | 1 + js/lib/BrowserDetect.js | 10 +- js/lib/Comms.js | 196 ++++++++++++++++++++++++++++++++++++++-- js/lib/PlayerData.js | 5 +- js/modules/PageInfo.js | 9 +- js/uw-bg.js | 17 +++- js/uw.js | 24 +++-- manifest.json | 11 +-- 8 files changed, 236 insertions(+), 37 deletions(-) diff --git a/js/conf/Debug.js b/js/conf/Debug.js index 45ff6fe..8fca609 100644 --- a/js/conf/Debug.js +++ b/js/conf/Debug.js @@ -8,6 +8,7 @@ Debug = { debugResizer: true, debugArDetect: true, debugStorage: true, + comms: true, // showArDetectCanvas: true, flushStoredSettings: false, playerDetectDebug: true, diff --git a/js/lib/BrowserDetect.js b/js/lib/BrowserDetect.js index 8a5922e..fa3b009 100644 --- a/js/lib/BrowserDetect.js +++ b/js/lib/BrowserDetect.js @@ -2,12 +2,10 @@ var _bd_usebrowser = "firefox"; var _bd_isFirefox = false; var _bd_isChrome = false; -var _bd_isEdge = true; // we'll see if FF +var _bd_isEdge = false; // we'll see if FF try{ - // this will fail in Edge - - var promise = browser.runtime.getBrowserInfo(); + // todo: find something that works in firefox but not in edge (or vice-versa) _bd_isFirefox = true; _bd_isEdge = false; } @@ -28,3 +26,7 @@ var BrowserDetect = { chrome: _bd_isChrome, edge: _bd_isEdge } + +if(Debug.debug){ + console.log("BrowserDetect loaded! Here's BrowserDetect object:", BrowserDetect) +} \ No newline at end of file diff --git a/js/lib/Comms.js b/js/lib/Comms.js index a9ee265..01db77c 100644 --- a/js/lib/Comms.js +++ b/js/lib/Comms.js @@ -1,3 +1,179 @@ +class CommsClient { + constructor(name){ + this.port = browser.runtime.connect({name: name}); + + var ths = this; + this.port.onMessage.addListener(m => ths.processReceivedMessage(m)); + this.hasSettings = false; + } + + setPageInfo(pageInfo){ + this.pageInfo = pageInfo; + } + + processReceivedMessage(message){ + if(Debug.debug && Debug.comms){ + console.log("[CommsClient.js::processMessage] Received message from background script!", message); + } + + if(message.cmd === "set-ar"){ + this.pageInfo.setAr(message.ar); + } else if (message.cmd === "has-videos") { + + } else if (message.cmd === "set-config") { + this.hasSettings = true; + ExtensionConf = message.conf; + } else if (message.cmd === "set-stretch") { + + } else if (message.cmd === "autoar-enable") { + if (message.enabled !== false) { + this.pageInfo.initArDetection(); + this.pageInfo.startArDetection(); + } else { + this.pageInfo.stopArDetection(); + } + } else if (message.cmd === "pause-processing") { + this.pageInfo.pauseProcessing(); + } else if (message.cmd === "resume-processing") { + // todo: autoArStatus + this.pageInfo.resumeProcessing(message.autoArStatus); + } + } + + async waitForSettings(){ + var t = this; + return new Promise( async (resolve, reject) => { + while(true){ + await t.sleep(100); + if(this.hasSettings){ + resolve(); + break; + } + } + }); + } + + async sleep(n){ + return new Promise( (resolve, reject) => setTimeout(resolve, n) ); + } + + async sendMessage_nonpersistent(message){ + if(BrowserDetect.firefox){ + return browser.runtime.sendMessage(message) + } else { + return new Promise((resolve, reject) => { + try{ + if(BrowserDetect.edge){ + browser.runtime.sendMessage(message, function(response){ + var r = response; + resolve(r); + }); + } else { + chrome.runtime.sendMessage(message, function(response){ + // Chrome/js shittiness mitigation — remove this line and an empty array will be returned + var r = response; + resolve(r); + }); + } + } + catch(e){ + reject(e); + } + }); + } + } + + async requestSettings(){ + if(Debug.debug){ + console.log("%c[CommsClient::requestSettings] sending request for congif!", "background: #11D; color: #DDA"); + } + var response = await this.sendMessage_nonpersistent({cmd: 'get-config'}); + if(Debug.debug){ + console.log("%c[CommsClient::requestSettings] received settings response!", "background: #11D; color: #DDA", response); + } + + if(! response || response.extensionConf){ + return Promise.resolve(false); + } + + ExtensionConf = JSON.parse(response.extensionConf); + return Promise.resolve(true); + } + + async requestSettings_fallback(){ + this.port.postMessage({cmd: "get-config"}); + } + + registerVideo(){ + this.port.postMessage({cmd: "has-video"}); + } + + unregisterVideo(){ + this.port.postMessage({cmd: "noVideo"}); // ayymd + } +} + +class CommsServer { + constructor(server) { + this.server = server; + this.ports = []; + + var ths = this; + + + if (BrowserDetect.firefox) { + browser.runtime.onConnect.addListener(p => ths.onConnect(p)); + browser.runtime.onMessage.addListener(m => ths.processReceivedMessage_nonpersistent_ff(m)); + } else { + chrome.runtime.onConnect.addListener(p => ths.onConnect(p)); + chrome.runtime.onMessage.addListener((msg, sender, callback) => ths.processReceivedMessage_nonpersistent_chrome(m, sender, callback)); + } + } + + onConnect(port){ + console.log("on connect!") + var tabId = port.sender.tab.id; + var ths = this; + this.ports[tabId] = port; + this.ports[tabId].onMessage.addListener( (m,p) => ths.processReceivedMessage(m, p)); + } + + processReceivedMessage(message, port){ + if (Debug.debug && Debug.comms) { + console.log("[CommsServer.js::processMessage] Received message from background script!", message, "port", port); + } + + if (message.cmd === 'get-config') { + port.postMessage({cmd: "set-config", conf: ExtensionConf}) + } + } + + processReceivedMessage_nonpersistent_ff(message, sender){ + if (Debug.debug && Debug.comms) { + console.log("%c[CommsServer.js::processMessage_nonpersistent_ff] Received message from background script!", "background-color: #11D; color: #DDA", message, sender); + } + + if (message.cmd === 'get-config') { + var ret = {extensionConf: JSON.stringify(ExtensionConf)}; + if (Debug.debug && Debug.comms) { + console.log("%c[CommsServer.js::processMessage_nonpersistent_ff] Returning this:", "background-color: #11D; color: #DDA", ret); + } + Promise.resolve(ret); + } + } + + processReceivedMessage_nonpersistent_chrome(message, sender, sendResponse){ + if (Debug.debug && Debug.comms) { + console.log("[CommsServer.js::processMessage_nonpersistent_chrome] Received message from background script!", message); + } + + if(message.cmd === 'get-config') { + sendResponse({extensionConf: JSON.stringify(ExtensionConf)}); + // return true; + } + } +} + var _com_chrome_tabquery_wrapper = async function(tabInfo){ return new Promise(function (resolve, reject){ browser.tabs.query(tabInfo, function(response){ @@ -172,13 +348,13 @@ var _com_sendToMainFrame = async function(message, tabId){ return response; } -var Comms = { - getActiveTab: _com_getActiveTab, - sendToBackgroundScript: _com_sendMessageRuntime, - queryTabs: _com_queryTabs, - sendMessage: _com_sendMessage, - sendMessageRuntime: _com_sendMessageRuntime, - sendToEach: _com_sendToEachFrame, - sendToAll: _com_sendToAllFrames, - sendToMain: _com_sendToMainFrame, -} +// var Comms = { +// getActiveTab: _com_getActiveTab, +// sendToBackgroundScript: _com_sendMessageRuntime, +// queryTabs: _com_queryTabs, +// sendMessage: _com_sendMessage, +// sendMessageRuntime: _com_sendMessageRuntime, +// sendToEach: _com_sendToEachFrame, +// sendToAll: _com_sendToAllFrames, +// sendToMain: _com_sendToMainFrame, +// } diff --git a/js/lib/PlayerData.js b/js/lib/PlayerData.js index 1206995..85b6874 100644 --- a/js/lib/PlayerData.js +++ b/js/lib/PlayerData.js @@ -218,15 +218,12 @@ class PlayerData { } checkPlayerSizeChange(){ - - // console.log("Player:", this.dimensions, "Node:", this.element) - if(Debug.debug){ if(this.element == undefined) console.log("[PlayerDetect] player size changed. reason: player element undefined"); if(this.dimensions.fullscreen){ - if(! this.isFullScreen()){ + if(! PlayerData.isFullScreen()){ console.log("[PlayerDetect] player size changed. reason: exited fullscreen"); } } diff --git a/js/modules/PageInfo.js b/js/modules/PageInfo.js index c1fa4ef..1f59fff 100644 --- a/js/modules/PageInfo.js +++ b/js/modules/PageInfo.js @@ -2,7 +2,7 @@ if(Debug.debug) console.log("Loading: PageInfo.js"); class PageInfo { - constructor(){ + constructor(comms){ this.keybinds = new Keybinds(this); this.keybinds.setup(); this.hasVideos = false; @@ -13,6 +13,13 @@ class PageInfo { this.rescan(RescanReason.PERIODIC); this.scheduleUrlCheck(); + + if(comms){ + this.comms = comms; + if(this.videos.length > 0){ + comms.registerVideo(); + } + } } rescan(rescanReason){ diff --git a/js/uw-bg.js b/js/uw-bg.js index e949d8d..06829c2 100644 --- a/js/uw-bg.js +++ b/js/uw-bg.js @@ -4,6 +4,21 @@ var BgVars = { currentSite: "" } +class UWServer { + constructor() { + this.ports = []; + + this.setup(); + } + + async setup() { + await Settings.init(); + this.comms = new CommsServer(this); + } +} + +var server = new UWServer(); + function extractHostname(url){ // extract hostname if (url.indexOf("://") > -1) { //find & remove protocol (http, ftp, etc.) and get hostname @@ -23,7 +38,7 @@ async function main(){ if(Debug.debug) console.log("[uw-bg::main] setting up background script"); - await Settings.init(); + Keybinds.keybinds = await Keybinds.fetch(); diff --git a/js/uw.js b/js/uw.js index 643ede1..35cd68c 100644 --- a/js/uw.js +++ b/js/uw.js @@ -13,15 +13,26 @@ if(Debug.debug){ } +var pageInfo; +var comms; async function init(){ if(Debug.debug) console.log("[uw::main] loading configuration ..."); + comms = new CommsClient('content-client-port'); + // load settings // var isSlave = true; // await Settings.init(isSlave); - await Settings.init(); + var settingsLoaded = await comms.requestSettings(); + if(!settingsLoaded){ + console.log("[uw::main] failed to get settings (settingsLoaded=",settingsLoaded,") Waiting for settings the old fashioned way"); + comms.requestSettings_fallback(); + await comms.waitForSettings(); + console.log("[uw::main] settings loaded."); + } + // await Settings.init(); // za sporočilca poslušamo v vsakem primeru, tudi če je razširitev na spletnem mestu onemogočena // we listen for messages in any case, even if extension is disabled on current site. @@ -42,22 +53,15 @@ async function init(){ console.log("[uw::main] configuration should be loaded now"); - // setup the extension - setup(); -} - -var pageInfo; - -async function setup(){ - pageInfo = new PageInfo(); + comms.setPageInfo(pageInfo); if(Debug.debug){ console.log("[uw.js::setup] pageInfo initialized. Here's the object:", pageInfo); } - } + // comms // function receiveMessage(message, sender, sendResponse) { // if(Debug.debug) diff --git a/manifest.json b/manifest.json index f58c726..79748d8 100644 --- a/manifest.json +++ b/manifest.json @@ -13,14 +13,13 @@ "content_scripts": [{ "matches": ["*://*/*"], "js": [ - "js/dep/chrome/chrome-extension-async.js", - + "js/conf/Debug.js", + "js/run/GlobalVars.js", "js/lib/BrowserDetect.js", "js/lib/StorageManager.js", "js/lib/Comms.js", - "js/conf/Debug.js", "js/conf/ExtensionConf.js", "js/conf/Settings.js", "js/conf/SitesConf.js", @@ -50,14 +49,12 @@ "background": { "scripts": [ - "js/dep/jquery-3.1.1.js", - "js/dep/chrome/chrome-extension-async.js", - + "js/conf/Debug.js", + "js/lib/BrowserDetect.js", "js/lib/StorageManager.js", "js/lib/Comms.js", - "js/conf/Debug.js", "js/conf/ExtensionConf.js", "js/conf/Settings.js", "js/conf/SitesConf.js",