From b8a6e9e0b3157923b681f0e42b3ec9cb804e5b98 Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Sun, 2 Jun 2019 23:54:32 +0200 Subject: [PATCH] Allow disabling of shortcuts --- src/ext/conf/ActionList.js | 26 +++++++++++++ src/ext/conf/Debug.js | 20 +++++----- src/ext/lib/ActionHandler.js | 47 +++++++++++++++-------- src/ext/lib/Settings.js | 24 ++++++++++++ src/ext/lib/comms/CommsClient.js | 2 + src/ext/lib/video-data/PageInfo.js | 4 ++ src/popup/js/ExecAction.js | 2 +- src/popup/panels/DefaultSettingsPanel.vue | 13 +++++++ src/popup/panels/VideoPanel.vue | 15 +++++++- 9 files changed, 125 insertions(+), 28 deletions(-) diff --git a/src/ext/conf/ActionList.js b/src/ext/conf/ActionList.js index b73d1c0..6457888 100644 --- a/src/ext/conf/ActionList.js +++ b/src/ext/conf/ActionList.js @@ -180,6 +180,32 @@ var ActionList = { global: true, site: true, } + }, + 'set-keyboard': { + name: 'Keyboard shortcuts', + args: [{ + name: 'Enable', + arg: ExtensionMode.Enabled, + },{ + name: 'On whitelisted only', + arg: ExtensionMode.Whitelist, + scopes: { + global: true, + } + },{ + name: 'Default', + arg: ExtensionMode.Default, + scopes: { + page: true, + } + },{ + name: 'Disable', + arg: ExtensionMode.Disabled + }], + scopes: { + global: true, + site: true, + } } }; diff --git a/src/ext/conf/Debug.js b/src/ext/conf/Debug.js index 00739d9..3cb16db 100644 --- a/src/ext/conf/Debug.js +++ b/src/ext/conf/Debug.js @@ -7,10 +7,10 @@ var Debug = { init: true, debug: true, // debug: false, - // keyboard: true, - debugResizer: true, - debugArDetect: true, - scaler: true, + keyboard: true, + // debugResizer: true, + // debugArDetect: true, + // scaler: true, // debugStorage: false, // debugStorage: true, // comms: false, @@ -18,15 +18,15 @@ var Debug = { // showArDetectCanvas: true, // flushStoredSettings: true, // flushStoredSettings: false, - playerDetectDebug: true, - periodic: true, - videoRescan: true, + // playerDetectDebug: true, + // periodic: true, + // videoRescan: true, // mousemove: true, - arDetect: { + // arDetect: { // edgeDetect: true - }, + // }, canvas: { - debugDetection: true + // debugDetection: true }, debugCanvas: { // enabled: true, diff --git a/src/ext/lib/ActionHandler.js b/src/ext/lib/ActionHandler.js index f6e6583..44cb9bd 100644 --- a/src/ext/lib/ActionHandler.js +++ b/src/ext/lib/ActionHandler.js @@ -1,5 +1,6 @@ import Debug from '../conf/Debug'; import PlayerData from './video-data/PlayerData'; +import ExtensionMode from '../../common/enums/extension-mode.enum'; class ActionHandler { @@ -8,6 +9,7 @@ class ActionHandler { this.settings = pageInfo.settings; this.inputs = ['input', 'select', 'button', 'textarea']; + this.keyboardLocalDisabled = false; } init() { @@ -119,6 +121,15 @@ class ActionHandler { } + setKeyboardLocal(state) { + if (state === ExtensionMode.Enabled) { + this.keyboardLocalDisabled = false; + } else if (state === ExtensionMode.Disabled) { + this.keyboardLocalDisabled = true; + } + // don't do shit on invalid value of state + } + preventAction() { var activeElement = document.activeElement; @@ -130,6 +141,8 @@ class ActionHandler { "\nis tag one of defined inputs? (yes->prevent):", this.inputs.indexOf(activeElement.tagName.toLocaleLowerCase()) !== -1, "\nis role = textbox? (yes -> prevent):", activeElement.getAttribute("role") === "textbox", "\nis type === 'text'? (yes -> prevent):", activeElement.getAttribute("type") === "text", + "\nis keyboard local disabled? (yes -> prevent):", this.keyboardLocalDisabled, + "\nis keyboard enabled in settings? (no -> prevent)", this.settings.keyboardShortcutsEnabled(window.location.hostname), "\nwill the action be prevented? (yes -> prevent)", this.preventAction(), "\n-----------------{ extra debug info }-------------------", "\ntag name? (lowercase):", activeElement.tagName, activeElement.tagName.toLocaleLowerCase(), @@ -146,6 +159,13 @@ class ActionHandler { // if (PlayerData.isFullScreen()) { // return false; // } + + if (this.keyboardLocalDisabled) { + return true; + } + if (!this.settings.keyboardShortcutsEnabled(window.location.hostname)) { + return true; + } if (this.inputs.indexOf(activeElement.tagName.toLocaleLowerCase()) !== -1) { return true; } @@ -193,27 +213,22 @@ class ActionHandler { if (videoData) { videoData.panHandler(event, true); } + } else if (cmd.action === 'set-keyboard') { + this.setKeyboardLocal(cmd.arg); } - } else if (action.scope === 'site') { + } else { + let site = action.scope === 'site' ? window.location.host : '@global'; + if (cmd.action === "set-stretch") { - this.settings.active.sites[window.location.host].stretch = cmd.arg; + this.settings.active.sites[site].stretch = cmd.arg; } else if (cmd.action === "set-alignment") { - this.settings.active.sites[window.location.host].videoAlignment = cmd.arg; + this.settings.active.sites[site].videoAlignment = cmd.arg; } else if (cmd.action === "set-extension-mode") { - this.settings.active.sites[window.location.host].status = cmd.arg; + this.settings.active.sites[site].status = cmd.arg; } else if (cmd.action === "set-autoar-mode") { - this.settings.active.sites[window.location.host].arStatus = cmd.arg; - } - this.settings.save(); - } else if (action.scope === 'global') { - if (cmd.action === "set-stretch") { - this.settings.active.stretch.initialMode = cmd.arg; - } else if (cmd.action === "set-alignment") { - this.settings.active.miscSettings.videoAlignment = cmd.arg; - } else if (cmd.action === "set-extension-mode") { - this.settings.active.sites['@global'] = cmd.arg; - } else if (cmd.action === "set-autoar-mode") { - this.settings.active.arDetect.mode.arStatus = cmd.arg; + this.settings.active.sites[site].arStatus = cmd.arg; + } else if (cmd.action === 'set-keyboard') { + this.settings.active.sites[site].keyboardShortcutsEnabled = cmd.arg; } this.settings.save(); } diff --git a/src/ext/lib/Settings.js b/src/ext/lib/Settings.js index 171a31c..9bbc95a 100644 --- a/src/ext/lib/Settings.js +++ b/src/ext/lib/Settings.js @@ -297,6 +297,30 @@ class Settings { } } + keyboardShortcutsEnabled(site) { + if (!site) { + site = window.location.hostname; + } + if (!site) { + return false; + } + + try { + if (!this.active.sites[site] + || this.active.sites[site].keyboardShortcutsEnabled === undefined + || this.active.sites[site].keyboardShortcutsEnabled === ExtensionMode.Default) { + return this.keyboardShortcutsEnabled('@global'); + } else { + return this.active.sites[site].keyboardShortcutsEnabled === ExtensionMode.Enabled; + } + } catch (e) { + if (Debug.debug) { + console.error("[Settings.js::keyboardDisabled] something went wrong:", e); + } + return false; + } + } + extensionEnabled(){ return this.active.sites['@global'] !== ExtensionMode.Disabled } diff --git a/src/ext/lib/comms/CommsClient.js b/src/ext/lib/comms/CommsClient.js index 28185dd..9493d92 100644 --- a/src/ext/lib/comms/CommsClient.js +++ b/src/ext/lib/comms/CommsClient.js @@ -71,6 +71,8 @@ class CommsClient { this.pageInfo.restoreAr(); } else if (message.cmd === "set-stretch") { this.pageInfo.setStretchMode(message.arg, message.playing); + } else if (message.cmd === 'set-keyboard') { + this.pageInfo.setKeyboardShortcutsEnabled(message.arg) } else if (message.cmd === "autoar-start") { if (message.enabled !== false) { this.pageInfo.initArDetection(message.playing); diff --git a/src/ext/lib/video-data/PageInfo.js b/src/ext/lib/video-data/PageInfo.js index 65978c9..a361ecd 100644 --- a/src/ext/lib/video-data/PageInfo.js +++ b/src/ext/lib/video-data/PageInfo.js @@ -479,6 +479,10 @@ class PageInfo { requestCurrentZoom() { this.comms.announceZoom(this.currentZoomScale); } + + setKeyboardShortcutsEnabled(state) { + this.actionHandler.setKeybordLocal(state); + } } export default PageInfo; diff --git a/src/popup/js/ExecAction.js b/src/popup/js/ExecAction.js index 696030c..422cb7c 100644 --- a/src/popup/js/ExecAction.js +++ b/src/popup/js/ExecAction.js @@ -46,7 +46,7 @@ class ExecAction { this.settings.active.sites[site].mode = cmd.arg; } else if (cmd.action === "set-autoar-mode") { this.settings.active.sites[site].autoar = cmd.arg; - } + } else if (cmd.action === 'set-keyboard') { this.settings.active.sites[site].keyboardShortcutsEnabled = cmd.arg; } this.settings.save(); diff --git a/src/popup/panels/DefaultSettingsPanel.vue b/src/popup/panels/DefaultSettingsPanel.vue index 598759a..eb2c1a2 100644 --- a/src/popup/panels/DefaultSettingsPanel.vue +++ b/src/popup/panels/DefaultSettingsPanel.vue @@ -57,6 +57,19 @@ + +
+
Enable/disable keyboard shortcuts
+
+ +
diff --git a/src/popup/panels/VideoPanel.vue b/src/popup/panels/VideoPanel.vue index 78180d5..ccf8042 100644 --- a/src/popup/panels/VideoPanel.vue +++ b/src/popup/panels/VideoPanel.vue @@ -65,8 +65,21 @@ + + -
Video alignment: