From 3846d1a9262d9f9fb31510b9f0977ad2b97c2c26 Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Tue, 19 Oct 2021 22:49:13 +0200 Subject: [PATCH] Cropping now also works from in-player UI --- src/csui/PlayerUiPanels/VideoSettings.vue | 16 +++++---- src/csui/ui-libs/ExecAction.js | 20 ++++++++--- src/ext/lib/UWGlobals.js | 43 ++++++++++++++++++++++- 3 files changed, 67 insertions(+), 12 deletions(-) diff --git a/src/csui/PlayerUiPanels/VideoSettings.vue b/src/csui/PlayerUiPanels/VideoSettings.vue index 26657c3..707c0c3 100644 --- a/src/csui/PlayerUiPanels/VideoSettings.vue +++ b/src/csui/PlayerUiPanels/VideoSettings.vue @@ -49,6 +49,7 @@ import CropModePersistence from '../../common/enums/CropModePersistence.enum'; export default { data() { return { + exec: null, scope: 'page', CropModePersistence: CropModePersistence, } @@ -63,7 +64,7 @@ export default { 'cropModePersistence', ], created() { - this.exec = new ExecAction(this.settings); + this.exec = new ExecAction(this.settings, window.location.hostname, window.ultrawidify); }, components: { ShortcutButton, @@ -78,7 +79,13 @@ export default { BrowserDetect.runtime.openOptionsPage(); }, execAction(action) { - this.exec.exec(action, 'page', this.frame); + console.log('execing action:', action, window.ultrawidify); + + try { + this.exec.exec(action, 'page', this.frame, true); + } catch (error) { + console.error('[uw:VideoSettings.vue::execAction] failed to execute action. Error:', error); + } }, parseShortcut(action) { if (! action.scopes.page.shortcut) { @@ -91,16 +98,13 @@ export default { }, changeZoom(nz) { nz = Math.pow(2, nz); - this.$emit('zoom-change', nz); + this.$emit('zoom-change', nz); // this.exec.exec( // {cmd: [{action: 'set-zoom', arg: nz}]}, // 'page', // this.frame // ); }, - testAction() { - window.ultrawidify.videos[0].setAr({type: AspectRatioType.FitWidth}); - } } } diff --git a/src/csui/ui-libs/ExecAction.js b/src/csui/ui-libs/ExecAction.js index a5a0afa..cc8c8d9 100644 --- a/src/csui/ui-libs/ExecAction.js +++ b/src/csui/ui-libs/ExecAction.js @@ -13,9 +13,11 @@ class ExecAction { this.site = site; } - async exec(action, scope, frame) { + + async exec(action, scope, frame, useBus) { + console.log('execing actioN!'); for (var cmd of action.cmd) { - if (scope === 'page') { + if (!scope || scope === 'page') { const message = { forwardToContentScript: true, targetFrame: frame, @@ -24,14 +26,18 @@ class ExecAction { arg: cmd.arg, customArg: cmd.customArg } - Comms.sendMessage(message); + if (useBus) { + window.ultrawidify.bus.sendMessage(message.cmd, message); + } else { + Comms.sendMessage(message); + } } else { // set-ar-persistence sends stuff to content scripts as well (!) // it's important to do that BEFORE the save step if (cmd.action === 'set-ar-persistence') { // even when setting global defaults, we only send message to the current tab in - // order to avoid problems related to + // order to avoid problems related to const message = { forwardToActive: true, targetFrame: frame, @@ -41,7 +47,11 @@ class ExecAction { } // this hopefully delays settings.save() until current crops are saved on the site // and thus avoid any fucky-wuckies - await Comms.sendMessage(message); + if (useBus) { + window.ultrawidify.bus.sendMessage(message.cmd, message); + } else { + await Comms.sendMessage(message); + } } let site = this.site; diff --git a/src/ext/lib/UWGlobals.js b/src/ext/lib/UWGlobals.js index 213fa80..fdab379 100644 --- a/src/ext/lib/UWGlobals.js +++ b/src/ext/lib/UWGlobals.js @@ -1,6 +1,14 @@ export default class UWGlobals { + constructor() { this.videos = []; + this.busSubscriptions = []; + this.actionSubscriptions = {}; + this.bus = { + sendMessage: (action, config) => this.propagateMessages(action, config), + subscribe: this.subscribeToAny, + subscribeToAction: this.subscribeToAction + } } getNewVideoID() { @@ -9,7 +17,7 @@ export default class UWGlobals { while (true) { // 4-digit [a-z0-9] string. Should be unique per page on first try random = (Math.random() * 1679616).toFixed().toString(36); - + if (this.videos.findIndex(x => x.vdid === random) === -1) { return random; } @@ -27,6 +35,39 @@ export default class UWGlobals { return this.videos.find(x => x.vdid === id); } + importSubscriptionsFromCommsHandlers(commands) { + for (const action in commands) { + for (const command of commands[action]) { + this.subscribeToAction(action, command); + } + } + } + + subscribeToAction(action, callback) { + if (!this.actionSubscriptions[action]) { + this.actionSubscriptions[action] = []; + } + + this.actionSubscriptions[action].push(callback); + } + + subscribeToAny(callback) { + this.busSubscriptions.push(callback); + } + + propagateMessages(action, config) { + if (this.busSubscriptions) { + for (const subscription of this.busSubscriptions) { + subscription(action, config); + } + } + if (this.actionSubscriptions && this.actionSubscriptions[action]) { + for (const subscription of this.actionSubscriptions[action]) { + subscription(config); + } + } + } + destroy() { // todo: implement }