diff --git a/src/ext/lib/comms/Comms.js b/src/ext/lib/comms/Comms.js new file mode 100644 index 0000000..4db282a --- /dev/null +++ b/src/ext/lib/comms/Comms.js @@ -0,0 +1,33 @@ +import Debug from '../../conf/Debug'; +import BrowserDetect from '../../conf/BrowserDetect'; + +class Comms { + static async sendMessage(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); + } + }); + } + } + +} + +export default Comms; diff --git a/src/ext/lib/comms/CommsClient.js b/src/ext/lib/comms/CommsClient.js new file mode 100644 index 0000000..2a81a53 --- /dev/null +++ b/src/ext/lib/comms/CommsClient.js @@ -0,0 +1,165 @@ +import Debug from '../../conf/Debug'; +import BrowserDetect from '../../conf/BrowserDetect'; + +class CommsClient { + constructor(name, settings) { + if (BrowserDetect.firefox) { + this.port = browser.runtime.connect({name: name}); + } else if (BrowserDetect.chrome) { + this.port = chrome.runtime.connect({name: name}); + } else if (BrowserDetect.edge) { + this.port = browser.runtime.connect({name: name}) + } + + var ths = this; + this._listener = m => ths.processReceivedMessage(m); + this.port.onMessage.addListener(this._listener); + + this.settings = settings; + this.pageInfo = undefined; + this.commsId = (Math.random() * 20).toFixed(0); + } + + destroy() { + this.pageInfo = null; + this.settings = null; + if (!BrowserDetect.edge) { // edge is a very special browser made by outright morons. + this.port.onMessage.removeListener(this._listener); + } + } + + setPageInfo(pageInfo){ + + this.pageInfo = pageInfo; + + if(Debug.debug) { + console.log(`[CommsClient::setPageInfo] <${this.commsId}>`, "SETTING PAGEINFO —", this.pageInfo, this) + } + + var ths = this; + this._listener = m => ths.processReceivedMessage(m); + if (!BrowserDetect.edge) { + this.port.onMessage.removeListener(this._listener); + } + this.port.onMessage.addListener(this._listener); + + } + + processReceivedMessage(message){ + if(Debug.debug && Debug.comms){ + console.log(`[CommsClient.js::processMessage] <${this.commsId}> Received message from background script!`, message); + } + + if (!this.pageInfo || !this.settings.active) { + if(Debug.debug && Debug.comms){ + console.log(`[CommsClient.js::processMessage] <${this.commsId}> this.pageInfo (or settings) not defined. Extension is probably disabled for this site.\npageInfo:`, this.pageInfo, + "\nsettings.active:", this.settings.active, + "\nnobj:", this + ); + } + return; + } + + if (message.cmd === 'get-current-zoom') { + this.pageInfo.requestCurrentZoom(); + } + + if (message.cmd === "set-ar") { + this.pageInfo.setAr(message.arg, message.playing); + } else if (message.cmd === 'set-alignment') { + this.pageInfo.setVideoFloat(message.arg, message.playing); + this.pageInfo.restoreAr(); + } else if (message.cmd === "set-stretch") { + this.pageInfo.setStretchMode(message.arg, message.playing); + } else if (message.cmd === "autoar-start") { + if (message.enabled !== false) { + this.pageInfo.initArDetection(message.playing); + this.pageInfo.startArDetection(message.playing); + } else { + this.pageInfo.stopArDetection(message.playing); + } + } else if (message.cmd === "pause-processing") { + this.pageInfo.pauseProcessing(message.playing); + } else if (message.cmd === "resume-processing") { + // todo: autoArStatus + this.pageInfo.resumeProcessing(message.autoArStatus, message.playing); + } else if (message.cmd === 'set-zoom') { + this.pageInfo.setZoom(message.zoom, true, message.playing); + } else if (message.cmd === 'mark-player') { + this.pageInfo.markPlayer(message.name, message.color); + } else if (message.cmd === 'unmark-player') { + this.pageInfo.unmarkPlayer(); + } + } + + 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: #aad"); + } + var response = await this.sendMessage_nonpersistent({cmd: 'get-config'}); + if(Debug.debug){ + console.log("%c[CommsClient::requestSettings] received settings response!", "background: #11D; color: #aad", response); + } + + if(! response || response.extensionConf){ + return Promise.resolve(false); + } + + this.settings.active = JSON.parse(response.extensionConf); + return Promise.resolve(true); + } + + registerVideo(){ + if (Debug.debug && Debug.comms) { + console.log(`[CommsClient::registerVideo] <${this.commsId}>`, "Registering video for current page."); + } + if (this.pageInfo.videos.length) { + this.port.postMessage({cmd: "has-video"}); + } + } + + unregisterVideo(){ + if (Debug.debug && Debug.comms) { + console.log(`[CommsClient::unregisterVideo] <${this.commsId}>`, "Unregistering video for current page."); + } + this.port.postMessage({cmd: "noVideo"}); // ayymd + } + + announceZoom(scale){ + this.port.postMessage({cmd: "announce-zoom", zoom: scale}); + this.registerVideo() + } + +} + +export default CommsClient; diff --git a/src/ext/lib/Comms.js b/src/ext/lib/comms/CommsServer.js similarity index 62% rename from src/ext/lib/Comms.js rename to src/ext/lib/comms/CommsServer.js index 19dc433..196e6ed 100644 --- a/src/ext/lib/Comms.js +++ b/src/ext/lib/comms/CommsServer.js @@ -1,167 +1,5 @@ -if(Debug.debug){ - console.log("Loading Comms.js"); -} - -class CommsClient { - constructor(name, settings) { - if (window.currentBrowser.firefox) { - this.port = browser.runtime.connect({name: name}); - } else if (window.currentBrowser.chrome) { - this.port = chrome.runtime.connect({name: name}); - } else if (window.currentBrowser.edge) { - this.port = browser.runtime.connect({name: name}) - } - - var ths = this; - this._listener = m => ths.processReceivedMessage(m); - this.port.onMessage.addListener(this._listener); - - this.settings = settings; - this.pageInfo = undefined; - this.commsId = (Math.random() * 20).toFixed(0); - } - - destroy() { - this.pageInfo = null; - this.settings = null; - if (!window.currentBrowser.edge) { // edge is a very special browser made by outright morons. - this.port.onMessage.removeListener(this._listener); - } - } - - setPageInfo(pageInfo){ - - this.pageInfo = pageInfo; - - if(Debug.debug) { - console.log(`[CommsClient::setPageInfo] <${this.commsId}>`, "SETTING PAGEINFO —", this.pageInfo, this) - } - - var ths = this; - this._listener = m => ths.processReceivedMessage(m); - if (!window.currentBrowser.edge) { - this.port.onMessage.removeListener(this._listener); - } - this.port.onMessage.addListener(this._listener); - - } - - processReceivedMessage(message){ - if(Debug.debug && Debug.comms){ - console.log(`[CommsClient.js::processMessage] <${this.commsId}> Received message from background script!`, message); - } - - if (!this.pageInfo || !this.settings.active) { - if(Debug.debug && Debug.comms){ - console.log(`[CommsClient.js::processMessage] <${this.commsId}> this.pageInfo (or settings) not defined. Extension is probably disabled for this site.\npageInfo:`, this.pageInfo, - "\nsettings.active:", this.settings.active, - "\nnobj:", this - ); - } - return; - } - - if (message.cmd === 'get-current-zoom') { - this.pageInfo.requestCurrentZoom(); - } - - if (message.cmd === "set-ar") { - this.pageInfo.setAr(message.arg, message.playing); - } else if (message.cmd === 'set-alignment') { - this.pageInfo.setVideoFloat(message.arg, message.playing); - this.pageInfo.restoreAr(); - } else if (message.cmd === "set-stretch") { - this.pageInfo.setStretchMode(message.arg, message.playing); - } else if (message.cmd === "autoar-start") { - if (message.enabled !== false) { - this.pageInfo.initArDetection(message.playing); - this.pageInfo.startArDetection(message.playing); - } else { - this.pageInfo.stopArDetection(message.playing); - } - } else if (message.cmd === "pause-processing") { - this.pageInfo.pauseProcessing(message.playing); - } else if (message.cmd === "resume-processing") { - // todo: autoArStatus - this.pageInfo.resumeProcessing(message.autoArStatus, message.playing); - } else if (message.cmd === 'set-zoom') { - this.pageInfo.setZoom(message.zoom, true, message.playing); - } else if (message.cmd === 'mark-player') { - this.pageInfo.markPlayer(message.name, message.color); - } else if (message.cmd === 'unmark-player') { - this.pageInfo.unmarkPlayer(); - } - } - - async sleep(n){ - return new Promise( (resolve, reject) => setTimeout(resolve, n) ); - } - - async sendMessage_nonpersistent(message){ - if(window.currentBrowser.firefox){ - return browser.runtime.sendMessage(message) - } else { - return new Promise((resolve, reject) => { - try{ - if(window.currentBrowser.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: #aad"); - } - var response = await this.sendMessage_nonpersistent({cmd: 'get-config'}); - if(Debug.debug){ - console.log("%c[CommsClient::requestSettings] received settings response!", "background: #11D; color: #aad", response); - } - - if(! response || response.extensionConf){ - return Promise.resolve(false); - } - - this.settings.active = JSON.parse(response.extensionConf); - return Promise.resolve(true); - } - - registerVideo(){ - if (Debug.debug && Debug.comms) { - console.log(`[CommsClient::registerVideo] <${this.commsId}>`, "Registering video for current page."); - } - if (this.pageInfo.videos.length) { - this.port.postMessage({cmd: "has-video"}); - } - } - - unregisterVideo(){ - if (Debug.debug && Debug.comms) { - console.log(`[CommsClient::unregisterVideo] <${this.commsId}>`, "Unregistering video for current page."); - } - this.port.postMessage({cmd: "noVideo"}); // ayymd - } - - announceZoom(scale){ - this.port.postMessage({cmd: "announce-zoom", zoom: scale}); - this.registerVideo() - } - -} +import Debug from '../../conf/Debug'; +import BrowserDetect from '../../conf/Debug'; class CommsServer { constructor(server) { @@ -171,7 +9,7 @@ class CommsServer { var ths = this; - if (window.currentBrowser.firefox) { + if (BrowserDetect.firefox) { browser.runtime.onConnect.addListener(p => ths.onConnect(p)); browser.runtime.onMessage.addListener(m => ths.processReceivedMessage_nonpersistent_ff(m)); } else { @@ -209,7 +47,7 @@ class CommsServer { } async _getActiveTab() { - if (window.currentBrowser.firefox) { + if (BrowserDetect.firefox) { return await browser.tabs.query({currentWindow: true, active: true}); } else { return await new Promise( (resolve, reject) => { @@ -438,31 +276,4 @@ class CommsServer { } } -class Comms { - static async sendMessage(message){ - if(window.currentBrowser.firefox){ - return browser.runtime.sendMessage(message) - } else { - return new Promise((resolve, reject) => { - try{ - if(window.currentBrowser.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); - } - }); - } - } - -} +export default CommsServer; diff --git a/src/ext/modules/ActionHandler.js b/src/ext/modules/ActionHandler.js index d99ec62..24329c8 100644 --- a/src/ext/modules/ActionHandler.js +++ b/src/ext/modules/ActionHandler.js @@ -1,6 +1,4 @@ -if (Debug.debug) { - console.log("Loading: ActionHandler.js"); -} +import Debug from '../conf/Debug'; class ActionHandler { @@ -207,4 +205,6 @@ class ActionHandler { this.execAction(this.mouseMoveActions, event, undefined, videoData) } -} \ No newline at end of file +} + +export default ActionHandler; diff --git a/src/ext/uw-bg.js b/src/ext/uw-bg.js index 2f48664..2649734 100644 --- a/src/ext/uw-bg.js +++ b/src/ext/uw-bg.js @@ -1,3 +1,9 @@ +import Debug from './conf/Debug.js'; +import BrowserDetect from './conf/BrowserDetect'; +import CommsServer from './lib/comms/CommsServer'; +import Settings from './lib/Settings'; + + var BgVars = { arIsActive: true, hasVideos: false, @@ -30,9 +36,9 @@ class UWServer { this.comms = new CommsServer(this); var ths = this; - if(window.currentBrowser.firefox) { + if(BrowserDetect.firefox) { browser.tabs.onActivated.addListener(function(m) {ths.onTabSwitched(m)}); - } else if (window.currentBrowser.chrome) { + } else if (BrowserDetect.chrome) { chrome.tabs.onActivated.addListener(function(m) {ths.onTabSwitched(m)}); } @@ -92,9 +98,9 @@ class UWServer { this.currentTabId = activeInfo.tabId; // just for readability var tab; - if (window.currentBrowser.firefox) { + if (BrowserDetect.firefox) { var tab = await browser.tabs.get(this.currentTabId); - } else if (window.currentBrowser.chrome) { + } else if (BrowserDetect.chrome) { var tab = await this._promisifyTabsGet(chrome, this.currentTabId); } @@ -118,9 +124,9 @@ class UWServer { // does "garbage collection" on frames let frames; - if (window.currentBrowser.firefox) { + if (BrowserDetect.firefox) { frames = await browser.webNavigation.getAllFrames({tabId: this.currentTabId}); - } else if (window.currentBrowser.chrome) { + } else if (BrowserDetect.chrome) { frames = await new Promise( (resolve, reject) => { chrome.webNavigation.getAllFrames({tabId: this.currentTabId}, (data) => resolve(data) ); }); diff --git a/src/manifest.json b/src/manifest.json index cafd6ec..af6abd5 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -56,15 +56,6 @@ "background": { "scripts": [ - "ext/conf/Debug.js", - "ext/conf/BrowserDetect.js", - "ext/conf/ExtensionConf.js", - "ext/lib/Comms.js", - "ext/lib/ObjectCopy.js", - - "ext/lib/Settings.js", - "ext/modules/ActionHandler.js", - "ext/uw-bg.js" ] }, diff --git a/webpack.config.js b/webpack.config.js index 6f08b4c..cfe93fc 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -11,6 +11,7 @@ const config = { mode: process.env.NODE_ENV, context: __dirname + '/src', entry: { + 'ext/uw': './ext/uw.js', 'ext/uw-bg': './ext/uw-bg.js', 'popup/popup': './popup/popup.js', 'options/options': './options/options.js', @@ -72,7 +73,6 @@ const config = { { from: 'icons', to: 'icons', ignore: ['icon.xcf'] }, { from: 'popup/popup.html', to: 'popup/popup.html', transform: transformHtml }, { from: 'options/options.html', to: 'options/options.html', transform: transformHtml }, - { from: 'manifest.json', to: 'manifest.json',