diff --git a/src/ext/lib/comms/CommsClient.js b/src/ext/lib/comms/CommsClient.js index 142c2c0..bd194ad 100644 --- a/src/ext/lib/comms/CommsClient.js +++ b/src/ext/lib/comms/CommsClient.js @@ -2,7 +2,7 @@ import Debug from '../../conf/Debug'; import BrowserDetect from '../../conf/BrowserDetect'; class CommsClient { - constructor(name, settings, logger) { + constructor(name, logger, commands) { this.logger = logger; if (BrowserDetect.firefox) { @@ -24,46 +24,15 @@ class CommsClient { } ); - var ths = this; - this._listener = m => ths.processReceivedMessage(m); + this._listener = m => this.processReceivedMessage(m); this.port.onMessage.addListener(this._listener); - this.settings = settings; - this.pageInfo = undefined; this.commsId = (Math.random() * 20).toFixed(0); - this.commands = { - 'get-current-zoom': [() => this.pageInfo.requestCurrentZoom()], - 'set-ar': [(message) => this.pageInfo.setAr({type: message.arg, ratio: message.customArg}, message.playing)], - 'set-alignment': [(message) => { - this.pageInfo.setVideoAlignment(message.arg, message.playing); - this.pageInfo.restoreAr(); - }], - 'set-stretch': [(message) => this.pageInfo.setStretchMode(message.arg, message.playing, message.customArg)], - 'set-keyboard': [(message) => this.pageInfo.setKeyboardShortcutsEnabled(message.arg)], - 'autoar-start': [(message) => { - if (message.enabled !== false) { - this.pageInfo.initArDetection(message.playing); - this.pageInfo.startArDetection(message.playing); - } else { - this.pageInfo.stopArDetection(message.playing); - } - }], - 'pause-processing': [(message) => this.pageInfo.pauseProcessing(message.playing)], - 'resume-processing': [(message) => this.pageInfo.resumeProcessing(message.autoArStatus, message.playing)], - 'set-zoom': [(message) => this.pageInfo.setZoom(message.arg, true, message.playing)], - 'change-zoom': [(message) => this.pageInfo.zoomStep(message.arg, message.playing)], - 'mark-player': [(message) => this.pageInfo.markPlayer(message.name, message.color)], - 'unmark-player': [() => this.pageInfo.unmarkPlayer()], - 'autoar-set-manual-tick': [(message) => this.pageInfo.setManualTick(message.arg)], - 'autoar-tick': [() => this.pageInfo.tick()], - 'set-ar-persistence': [() => this.pageInfo.setArPersistence(message.arg)], - }; + this.commands = commands; } 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); } @@ -77,32 +46,9 @@ class CommsClient { } } - setPageInfo(pageInfo){ - - this.pageInfo = pageInfo; - - this.logger.log('info', 'debug', `[CommsClient::setPageInfo] <${this.commsId}>`, "setting pageinfo"); - - 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){ this.logger.log('info', 'comms', `[CommsClient.js::processMessage] <${this.commsId}> Received message from background script!`, message); - if (!this.pageInfo || !this.settings.active) { - this.logger.log('info', 'comms', `[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 (this.commands[message.cmd]) { for (const c of this.commands[message.cmd]) { c(message); @@ -165,13 +111,7 @@ class CommsClient { registerVideo(){ this.logger.log('info', 'comms', `[CommsClient::registerVideo] <${this.commsId}>`, "Registering video for current page."); - if (this.pageInfo) { - if (this.pageInfo.hasVideo()) { - this.port.postMessage({cmd: "has-video"}); - } - } else { - // this.port.postMessage({cmd: "has-video"}); - } + this.port.postMessage({cmd: "has-video"}); } sendPerformanceUpdate(message){ diff --git a/src/ext/uw-ui.js b/src/ext/uw-ui.js index c7a78b2..ca840a3 100644 --- a/src/ext/uw-ui.js +++ b/src/ext/uw-ui.js @@ -7,49 +7,49 @@ import LoggerUi from '../csui/LoggerUi'; class UwUi { + constructor() { + this.loggerUiInitiated = false; + this.playerUiInitiated = false; + } + async init() { // initialize vuejs - try { - Vue.prototype.$browser = global.browser; - Vue.use(Vuex); - this.vuexStore = new Vuex.Store({ - plugins: [VuexWebExtensions({ - persistentStates: [ - 'uwLog', - 'showLogger', - ], - })], - state: { - uwLog: '', - showLogger: false, + Vue.prototype.$browser = global.browser; + Vue.use(Vuex); + this.vuexStore = new Vuex.Store({ + plugins: [VuexWebExtensions({ + persistentStates: [ + 'uwLog', + 'showLogger', + ], + })], + state: { + uwLog: '', + showLogger: false, + }, + mutations: { + 'uw-set-log'(state, payload) { + state['uwLog'] = payload; }, - mutations: { - 'uw-set-log'(state, payload) { - state['uwLog'] = payload; - }, - 'uw-show-logger'(state) { - state['showLogger'] = true; - }, - 'uw-hide-logger'(state) { - state['showLogger'] = false; - } + 'uw-show-logger'(state) { + state['showLogger'] = true; }, - actions: { - 'uw-set-log' ({commit}, payload) { - commit('uw-set-log', payload); - }, - 'uw-show-logger'({commit}) { - commit('uw-show-logger'); - }, - 'uw-hide-logger'({commit}) { - commit('uw-hide-logger'); - } + 'uw-hide-logger'(state) { + state['showLogger'] = false; } - }); - console.log("successfully vued") - } catch (e) { - console.error("wasnt vued,", e); - } + }, + actions: { + 'uw-set-log' ({commit}, payload) { + commit('uw-set-log', payload); + }, + 'uw-show-logger'({commit}) { + commit('uw-show-logger'); + }, + 'uw-hide-logger'({commit}) { + commit('uw-hide-logger'); + } + } + }); // setup logger try { @@ -109,9 +109,23 @@ class UwUi { console.error("logger initialization failed"); } + // we also need to know settings (there's UI-related things in the settings — or rather, there will be UI-related things + // in settings once in-player UI is implemented + // If comms exist, we need to destroy it + if (this.comms) { + this.comms.destroy(); + } + if (!this.settings) { + this.settings = new Settings({ + onSettingsChanged: () => this.reloadSettings(), + logger: this.logger + }); + await this.settings.init(); + } + + this.comms = new CommsClient('content-ui-port', this.settings, this.logger); + - await this.initLoggerUi(); - this.showLogger(); } async initLoggerUi() { @@ -155,6 +169,23 @@ class UwUi { } } -console.log("init ui") -var uwui = new UwUi(); -uwui.init(); +// leave a mark, so this script won't get executed more than once on a given page +const markerId = 'ultrawidify-marker-5aeaf521-7afe-447f-9a17-3428f62d0970'; + +console.log("will init ui") + + +if (! document.getElementById(markerId)) { + console.log("init hasn't happened before") + const markerDiv = document.createElement('div'); + markerDiv.setAttribute("style", "display: none"); + markerDiv.setAttribute('id', markerId); + + document.body.appendChild(markerDiv); + + var uwui = new UwUi(); + uwui.init(); +} else { + console.info("UI has already been initiated once, so we aren't doing it again"); +} + diff --git a/src/ext/uw.js b/src/ext/uw.js index 7e978e3..f1ca0bb 100644 --- a/src/ext/uw.js +++ b/src/ext/uw.js @@ -24,6 +24,34 @@ class UW { this.actionHandler = undefined; this.logger = undefined; this.uiInitiated = false; + + this.commsHandlers = { + 'get-current-zoom': [() => this.pageInfo.requestCurrentZoom()], + 'set-ar': [(message) => this.pageInfo.setAr({type: message.arg, ratio: message.customArg}, message.playing)], + 'set-alignment': [(message) => { + this.pageInfo.setVideoAlignment(message.arg, message.playing); + this.pageInfo.restoreAr(); + }], + 'set-stretch': [(message) => this.pageInfo.setStretchMode(message.arg, message.playing, message.customArg)], + 'set-keyboard': [(message) => this.pageInfo.setKeyboardShortcutsEnabled(message.arg)], + 'autoar-start': [(message) => { + if (message.enabled !== false) { + this.pageInfo.initArDetection(message.playing); + this.pageInfo.startArDetection(message.playing); + } else { + this.pageInfo.stopArDetection(message.playing); + } + }], + 'pause-processing': [(message) => this.pageInfo.pauseProcessing(message.playing)], + 'resume-processing': [(message) => this.pageInfo.resumeProcessing(message.autoArStatus, message.playing)], + 'set-zoom': [(message) => this.pageInfo.setZoom(message.arg, true, message.playing)], + 'change-zoom': [(message) => this.pageInfo.zoomStep(message.arg, message.playing)], + 'mark-player': [(message) => this.pageInfo.markPlayer(message.name, message.color)], + 'unmark-player': [() => this.pageInfo.unmarkPlayer()], + 'autoar-set-manual-tick': [(message) => this.pageInfo.setManualTick(message.arg)], + 'autoar-tick': [() => this.pageInfo.tick()], + 'set-ar-persistence': [() => this.pageInfo.setArPersistence(message.arg)], + } } reloadSettings() { @@ -106,7 +134,7 @@ class UW { await this.settings.init(); } - this.comms = new CommsClient('content-ui-port', this.settings, this.logger); + this.comms = new CommsClient('content-ui-port', this.logger, this.commsHandlers); // če smo razširitev onemogočili v nastavitvah, ne naredimo ničesar // If extension is soft-disabled, don't do shit @@ -127,7 +155,6 @@ class UW { try { this.pageInfo = new PageInfo(this.comms, this.settings, this.logger, extensionMode, isSiteDisabled); this.logger.log('info', 'debug', "[uw.js::setup] pageInfo initialized."); - this.comms.setPageInfo(this.pageInfo); this.logger.log('info', 'debug', "[uw.js::setup] will try to initate ActionHandler.");