From 854bc05d14b257b7e8b39aa4eb232e10b652e2a3 Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Sat, 26 Oct 2019 02:38:47 +0200 Subject: [PATCH] Pass `set-ar-persistence` between CommsClient and pageInfo --- src/ext/lib/comms/CommsClient.js | 2 ++ src/ext/lib/video-data/PageInfo.js | 5 ++++- src/ext/lib/video-transform/Resizer.js | 20 ++++++++++---------- src/popup/js/ExecAction.js | 25 ++++++++----------------- 4 files changed, 24 insertions(+), 28 deletions(-) diff --git a/src/ext/lib/comms/CommsClient.js b/src/ext/lib/comms/CommsClient.js index 4b3d2e4..cd8c7d9 100644 --- a/src/ext/lib/comms/CommsClient.js +++ b/src/ext/lib/comms/CommsClient.js @@ -92,6 +92,8 @@ class CommsClient { this.pageInfo.setManualTick(message.arg); } else if (message.cmd === 'autoar-tick') { this.pageInfo.tick(); + } else if (message.cmd === 'set-ar-persistence') { + this.pageInfo.setArPersistence(message.arg); } } diff --git a/src/ext/lib/video-data/PageInfo.js b/src/ext/lib/video-data/PageInfo.js index b23ecbe..72c3955 100644 --- a/src/ext/lib/video-data/PageInfo.js +++ b/src/ext/lib/video-data/PageInfo.js @@ -575,6 +575,9 @@ class PageInfo { } setArPersistence(persistenceMode) { + // name of this function is mildly misleading — we don't really _set_ ar persistence. (Ar persistence + // mode is set and saved via popup or keyboard shortcuts, if user defined them) We just save the current + // aspect ratio whenever aspect ratio persistence mode changes. if (persistenceMode === CropModePersistence.CurrentSession) { sessionStorage.setItem('uw-crop-mode-session-persistence', JSON.stringify(this.currentCrop)); } else if (persistenceMode === CropModePersistence.Forever) { @@ -590,7 +593,7 @@ class PageInfo { } } - setDefaultCrop(ar) { + updateCurrentCrop(ar) { this.currentCrop = ar; // This means crop persistance is disabled. If crop persistance is enabled, then settings for current // site MUST exist (crop persistence mode is disabled by default) diff --git a/src/ext/lib/video-transform/Resizer.js b/src/ext/lib/video-transform/Resizer.js index d9c35b6..f0dea5a 100644 --- a/src/ext/lib/video-transform/Resizer.js +++ b/src/ext/lib/video-transform/Resizer.js @@ -134,16 +134,16 @@ class Resizer { const siteSettings = this.settings.active.sites[window.location.host]; // most everything that could go wrong went wrong by this stage, and returns can happen afterwards - // this means here's the optimal place to set or forget aspect ratio - if (this.settings.getDefaultCropPersistenceMode(window.location.host) > CropModePersistance.Disabled) { - if (ar.type === AspectRatio.Automatic || - ar.type === AspectRatio.Reset || - ar.type === AspectRatio.Initial ) { - // reset/undo default - this.conf.pageInfo.setDefaultCrop(undefined); - } else { - this.conf.pageInfo.setDefaultCrop(ar); - } + // this means here's the optimal place to set or forget aspect ratio. Saving of current crop ratio + // is handled in pageInfo.updateCurrentCrop(), which also makes sure to persist aspect ratio if ar + // is set to persist between videos / through current session / until manual reset. + if (ar.type === AspectRatio.Automatic || + ar.type === AspectRatio.Reset || + ar.type === AspectRatio.Initial ) { + // reset/undo default + this.conf.pageInfo.updateCurrentCrop(undefined); + } else { + this.conf.pageInfo.updateCurrentCrop(ar); } if (ar.type === AspectRatio.Automatic || diff --git a/src/popup/js/ExecAction.js b/src/popup/js/ExecAction.js index 130a3fe..03cb516 100644 --- a/src/popup/js/ExecAction.js +++ b/src/popup/js/ExecAction.js @@ -30,23 +30,14 @@ class ExecAction { // set-ar-persistence sends stuff to content scripts as well (!) // it's important to do that BEFORE the save step if (cmd === 'set-ar-persistence') { - let message; - if (scope === 'site') { - message = { - forwardToContentScript: true, - targetFrame: frame, - frame: frame, - cmd: cmd.action, - arg: cmd.arg, - } - } else { - message = { - forwardToAll: true, - targetFrame: frame, - frame: frame, - cmd: cmd.action, - arg: cmd.arg, - } + // even when setting global defaults, we only send message to the current tab in + // order to avoid problems related to + const message = { + forwardToContentScript: true, + targetFrame: frame, + frame: frame, + cmd: cmd.action, + arg: cmd.arg, } // this hopefully delays settings.save() until current crops are saved on the site // and thus avoid any fucky-wuckies