Remove paginfo and command declarations out of comms

This commit is contained in:
Tamius Han 2020-03-08 17:13:44 +01:00
parent c26744b9d9
commit 92c4ba0c69
3 changed files with 107 additions and 109 deletions

View File

@ -2,7 +2,7 @@ import Debug from '../../conf/Debug';
import BrowserDetect from '../../conf/BrowserDetect'; import BrowserDetect from '../../conf/BrowserDetect';
class CommsClient { class CommsClient {
constructor(name, settings, logger) { constructor(name, logger, commands) {
this.logger = logger; this.logger = logger;
if (BrowserDetect.firefox) { if (BrowserDetect.firefox) {
@ -24,46 +24,15 @@ class CommsClient {
} }
); );
var ths = this; this._listener = m => this.processReceivedMessage(m);
this._listener = m => ths.processReceivedMessage(m);
this.port.onMessage.addListener(this._listener); this.port.onMessage.addListener(this._listener);
this.settings = settings;
this.pageInfo = undefined;
this.commsId = (Math.random() * 20).toFixed(0); this.commsId = (Math.random() * 20).toFixed(0);
this.commands = { this.commands = 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)],
};
} }
destroy() { destroy() {
this.pageInfo = null;
this.settings = null;
if (!BrowserDetect.edge) { // edge is a very special browser made by outright morons. if (!BrowserDetect.edge) { // edge is a very special browser made by outright morons.
this.port.onMessage.removeListener(this._listener); 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){ processReceivedMessage(message){
this.logger.log('info', 'comms', `[CommsClient.js::processMessage] <${this.commsId}> Received message from background script!`, 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]) { if (this.commands[message.cmd]) {
for (const c of this.commands[message.cmd]) { for (const c of this.commands[message.cmd]) {
c(message); c(message);
@ -165,13 +111,7 @@ class CommsClient {
registerVideo(){ registerVideo(){
this.logger.log('info', 'comms', `[CommsClient::registerVideo] <${this.commsId}>`, "Registering video for current page."); this.logger.log('info', 'comms', `[CommsClient::registerVideo] <${this.commsId}>`, "Registering video for current page.");
if (this.pageInfo) { this.port.postMessage({cmd: "has-video"});
if (this.pageInfo.hasVideo()) {
this.port.postMessage({cmd: "has-video"});
}
} else {
// this.port.postMessage({cmd: "has-video"});
}
} }
sendPerformanceUpdate(message){ sendPerformanceUpdate(message){

View File

@ -7,49 +7,49 @@ import LoggerUi from '../csui/LoggerUi';
class UwUi { class UwUi {
constructor() {
this.loggerUiInitiated = false;
this.playerUiInitiated = false;
}
async init() { async init() {
// initialize vuejs // initialize vuejs
try { Vue.prototype.$browser = global.browser;
Vue.prototype.$browser = global.browser; Vue.use(Vuex);
Vue.use(Vuex); this.vuexStore = new Vuex.Store({
this.vuexStore = new Vuex.Store({ plugins: [VuexWebExtensions({
plugins: [VuexWebExtensions({ persistentStates: [
persistentStates: [ 'uwLog',
'uwLog', 'showLogger',
'showLogger', ],
], })],
})], state: {
state: { uwLog: '',
uwLog: '', showLogger: false,
showLogger: false, },
mutations: {
'uw-set-log'(state, payload) {
state['uwLog'] = payload;
}, },
mutations: { 'uw-show-logger'(state) {
'uw-set-log'(state, payload) { state['showLogger'] = true;
state['uwLog'] = payload;
},
'uw-show-logger'(state) {
state['showLogger'] = true;
},
'uw-hide-logger'(state) {
state['showLogger'] = false;
}
}, },
actions: { 'uw-hide-logger'(state) {
'uw-set-log' ({commit}, payload) { state['showLogger'] = false;
commit('uw-set-log', payload);
},
'uw-show-logger'({commit}) {
commit('uw-show-logger');
},
'uw-hide-logger'({commit}) {
commit('uw-hide-logger');
}
} }
}); },
console.log("successfully vued") actions: {
} catch (e) { 'uw-set-log' ({commit}, payload) {
console.error("wasnt vued,", e); commit('uw-set-log', payload);
} },
'uw-show-logger'({commit}) {
commit('uw-show-logger');
},
'uw-hide-logger'({commit}) {
commit('uw-hide-logger');
}
}
});
// setup logger // setup logger
try { try {
@ -109,9 +109,23 @@ class UwUi {
console.error("logger initialization failed"); 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() { async initLoggerUi() {
@ -155,6 +169,23 @@ class UwUi {
} }
} }
console.log("init ui") // leave a mark, so this script won't get executed more than once on a given page
var uwui = new UwUi(); const markerId = 'ultrawidify-marker-5aeaf521-7afe-447f-9a17-3428f62d0970';
uwui.init();
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");
}

View File

@ -24,6 +24,34 @@ class UW {
this.actionHandler = undefined; this.actionHandler = undefined;
this.logger = undefined; this.logger = undefined;
this.uiInitiated = false; 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() { reloadSettings() {
@ -106,7 +134,7 @@ class UW {
await this.settings.init(); 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 // če smo razširitev onemogočili v nastavitvah, ne naredimo ničesar
// If extension is soft-disabled, don't do shit // If extension is soft-disabled, don't do shit
@ -127,7 +155,6 @@ class UW {
try { try {
this.pageInfo = new PageInfo(this.comms, this.settings, this.logger, extensionMode, isSiteDisabled); this.pageInfo = new PageInfo(this.comms, this.settings, this.logger, extensionMode, isSiteDisabled);
this.logger.log('info', 'debug', "[uw.js::setup] pageInfo initialized."); 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."); this.logger.log('info', 'debug', "[uw.js::setup] will try to initate ActionHandler.");