From da5a3841bd895175a92123c9014a048f710de061 Mon Sep 17 00:00:00 2001 From: Joe-Dowd Date: Fri, 20 Mar 2020 15:35:23 +0000 Subject: [PATCH 1/6] Added restrictions to keybindings. --- src/options.ts | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/options.ts b/src/options.ts index f5ebdb70..6586eddf 100644 --- a/src/options.ts +++ b/src/options.ts @@ -320,6 +320,26 @@ function keybindKeyPressed(element: HTMLElement, e: KeyboardEvent) { var key = e.key; let button = element.querySelector(".trigger-button"); + let option = element.getAttribute("sync-option"); + + // Don't allow keys which are already listened for by youtube + let restrictedKeys = Array.from("1234567890,.jklfcbm/<> -").concat(["Shift"]); + if (restrictedKeys.indexOf(key) !== -1 ) { + element.querySelector(".option-hidden-section").classList.add("hidden"); + button.classList.remove("disabled"); + alert("The key '" + key + "' is already used by youtube. Please select another key."); + return; + } + + // Make sure keybind isn't used by the other listener + // TODO: If other keybindings are going to be added, we need a better way to find the other keys used. + let otherKeybind = (option === "startSponsorKeybind") ? Config.config['submitKeybind'] : Config.config['startSponsorKeybind']; + if (key === otherKeybind) { + element.querySelector(".option-hidden-section").classList.add("hidden"); + button.classList.remove("disabled"); + alert("The key '" + key + "' is bound to another action. Please select another key."); + return; + } // cancel setting a keybind if (key === "Escape") { @@ -327,8 +347,7 @@ function keybindKeyPressed(element: HTMLElement, e: KeyboardEvent) { button.classList.remove("disabled"); return; } - - let option = element.getAttribute("sync-option"); + Config.config[option] = key; From c78e2cd21420537d5110c41d7aa333eb55f6c808 Mon Sep 17 00:00:00 2001 From: Joe-Dowd Date: Fri, 20 Mar 2020 19:39:37 +0000 Subject: [PATCH 2/6] Fixed mod keys when setting keybinding --- src/options.ts | 77 ++++++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 37 deletions(-) diff --git a/src/options.ts b/src/options.ts index 6586eddf..88213ea2 100644 --- a/src/options.ts +++ b/src/options.ts @@ -307,7 +307,7 @@ function activateKeybindChange(element: HTMLElement) { element.querySelector(".option-hidden-section").classList.remove("hidden"); - document.addEventListener("keydown", (e) => keybindKeyPressed(element, e), {once: true}); + document.addEventListener("keydown", (e) => keybindKeyPressed(element, e), {once: true}); } /** @@ -318,46 +318,49 @@ function activateKeybindChange(element: HTMLElement) { */ function keybindKeyPressed(element: HTMLElement, e: KeyboardEvent) { var key = e.key; + if (["Shift", "Control", "Meta", "Alt"].indexOf(key) !== -1) { + document.addEventListener("keydown", (e) => keybindKeyPressed(element, e), {once: true}); + } else { + let button = element.querySelector(".trigger-button"); + let option = element.getAttribute("sync-option"); - let button = element.querySelector(".trigger-button"); - let option = element.getAttribute("sync-option"); + // Don't allow keys which are already listened for by youtube + let restrictedKeys = "1234567890,.jklftcbmJKLFTCBM/<> -"; + if (restrictedKeys.indexOf(key) !== -1 ) { + element.querySelector(".option-hidden-section").classList.add("hidden"); + button.classList.remove("disabled"); + alert("The key " + key + " is already used by youtube. Please select another key."); + return; + } + + // Make sure keybind isn't used by the other listener + // TODO: If other keybindings are going to be added, we need a better way to find the other keys used. + let otherKeybind = (option === "startSponsorKeybind") ? Config.config['submitKeybind'] : Config.config['startSponsorKeybind']; + if (key === otherKeybind) { + element.querySelector(".option-hidden-section").classList.add("hidden"); + button.classList.remove("disabled"); + alert("The key " + key + " is bound to another action. Please select another key."); + return; + } + + // cancel setting a keybind + if (key === "Escape") { + element.querySelector(".option-hidden-section").classList.add("hidden"); + button.classList.remove("disabled"); + return; + } + + + Config.config[option] = key; + + let status = element.querySelector(".option-hidden-section > .keybind-status"); + status.innerText = chrome.i18n.getMessage("keybindDescriptionComplete"); + + let statusKey = element.querySelector(".option-hidden-section > .keybind-status-key"); + statusKey.innerText = key; - // Don't allow keys which are already listened for by youtube - let restrictedKeys = Array.from("1234567890,.jklfcbm/<> -").concat(["Shift"]); - if (restrictedKeys.indexOf(key) !== -1 ) { - element.querySelector(".option-hidden-section").classList.add("hidden"); button.classList.remove("disabled"); - alert("The key '" + key + "' is already used by youtube. Please select another key."); - return; } - - // Make sure keybind isn't used by the other listener - // TODO: If other keybindings are going to be added, we need a better way to find the other keys used. - let otherKeybind = (option === "startSponsorKeybind") ? Config.config['submitKeybind'] : Config.config['startSponsorKeybind']; - if (key === otherKeybind) { - element.querySelector(".option-hidden-section").classList.add("hidden"); - button.classList.remove("disabled"); - alert("The key '" + key + "' is bound to another action. Please select another key."); - return; - } - - // cancel setting a keybind - if (key === "Escape") { - element.querySelector(".option-hidden-section").classList.add("hidden"); - button.classList.remove("disabled"); - return; - } - - - Config.config[option] = key; - - let status = element.querySelector(".option-hidden-section > .keybind-status"); - status.innerText = chrome.i18n.getMessage("keybindDescriptionComplete"); - - let statusKey = element.querySelector(".option-hidden-section > .keybind-status-key"); - statusKey.innerText = key; - - button.classList.remove("disabled"); } /** From c013c7ef0fab04dd8e6c406db718ad82742ea262 Mon Sep 17 00:00:00 2001 From: Joe-Dowd Date: Fri, 20 Mar 2020 19:50:38 +0000 Subject: [PATCH 3/6] added N and i to the list of restricted characters --- src/options.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/options.ts b/src/options.ts index 88213ea2..86252256 100644 --- a/src/options.ts +++ b/src/options.ts @@ -325,7 +325,7 @@ function keybindKeyPressed(element: HTMLElement, e: KeyboardEvent) { let option = element.getAttribute("sync-option"); // Don't allow keys which are already listened for by youtube - let restrictedKeys = "1234567890,.jklftcbmJKLFTCBM/<> -"; + let restrictedKeys = "1234567890,.jklftcibmJKLFTCIBMN/<> -"; if (restrictedKeys.indexOf(key) !== -1 ) { element.querySelector(".option-hidden-section").classList.add("hidden"); button.classList.remove("disabled"); From b614dce91a5117cb4f413fa5737bca38a07a01c5 Mon Sep 17 00:00:00 2001 From: Joe-Dowd Date: Fri, 20 Mar 2020 19:55:29 +0000 Subject: [PATCH 4/6] Added more restricted characters --- src/options.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/options.ts b/src/options.ts index 86252256..f6a3d906 100644 --- a/src/options.ts +++ b/src/options.ts @@ -318,14 +318,14 @@ function activateKeybindChange(element: HTMLElement) { */ function keybindKeyPressed(element: HTMLElement, e: KeyboardEvent) { var key = e.key; - if (["Shift", "Control", "Meta", "Alt"].indexOf(key) !== -1) { + if (["Shift", "Control", "Meta", "Alt", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", "Tab"].indexOf(key) !== -1) { document.addEventListener("keydown", (e) => keybindKeyPressed(element, e), {once: true}); } else { let button = element.querySelector(".trigger-button"); let option = element.getAttribute("sync-option"); // Don't allow keys which are already listened for by youtube - let restrictedKeys = "1234567890,.jklftcibmJKLFTCIBMN/<> -"; + let restrictedKeys = "1234567890,.jklftcibmJKLFTCIBMNP/<> -+"; if (restrictedKeys.indexOf(key) !== -1 ) { element.querySelector(".option-hidden-section").classList.add("hidden"); button.classList.remove("disabled"); From feec5b4e22a4a582c79e134255d4fe305a08131e Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Thu, 26 Mar 2020 12:26:47 -0400 Subject: [PATCH 5/6] Added spacing and separated code into a function --- src/options.ts | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/options.ts b/src/options.ts index f6a3d906..e6b40430 100644 --- a/src/options.ts +++ b/src/options.ts @@ -318,18 +318,23 @@ function activateKeybindChange(element: HTMLElement) { */ function keybindKeyPressed(element: HTMLElement, e: KeyboardEvent) { var key = e.key; + if (["Shift", "Control", "Meta", "Alt", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", "Tab"].indexOf(key) !== -1) { + + // Wait for more document.addEventListener("keydown", (e) => keybindKeyPressed(element, e), {once: true}); } else { - let button = element.querySelector(".trigger-button"); + let button: HTMLElement = element.querySelector(".trigger-button"); let option = element.getAttribute("sync-option"); // Don't allow keys which are already listened for by youtube let restrictedKeys = "1234567890,.jklftcibmJKLFTCIBMNP/<> -+"; if (restrictedKeys.indexOf(key) !== -1 ) { - element.querySelector(".option-hidden-section").classList.add("hidden"); - button.classList.remove("disabled"); + closeKeybindOption(element, button); + + // TODO: This should be localised alert("The key " + key + " is already used by youtube. Please select another key."); + return; } @@ -337,20 +342,21 @@ function keybindKeyPressed(element: HTMLElement, e: KeyboardEvent) { // TODO: If other keybindings are going to be added, we need a better way to find the other keys used. let otherKeybind = (option === "startSponsorKeybind") ? Config.config['submitKeybind'] : Config.config['startSponsorKeybind']; if (key === otherKeybind) { - element.querySelector(".option-hidden-section").classList.add("hidden"); - button.classList.remove("disabled"); + closeKeybindOption(element, button); + + // TODO: This should be localised alert("The key " + key + " is bound to another action. Please select another key."); + return; } // cancel setting a keybind if (key === "Escape") { - element.querySelector(".option-hidden-section").classList.add("hidden"); - button.classList.remove("disabled"); + closeKeybindOption(element, button); + return; } - Config.config[option] = key; let status = element.querySelector(".option-hidden-section > .keybind-status"); @@ -363,6 +369,17 @@ function keybindKeyPressed(element: HTMLElement, e: KeyboardEvent) { } } +/** + * Closes the menu for editing the keybind + * + * @param element + * @param button + */ +function closeKeybindOption(element: HTMLElement, button: HTMLElement) { + element.querySelector(".option-hidden-section").classList.add("hidden"); + button.classList.remove("disabled"); +} + /** * Will trigger the textbox to appear to be able to change an option's text. * From 08d28798c67b37775a11755b9153f4ad7303c908 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Thu, 26 Mar 2020 12:30:06 -0400 Subject: [PATCH 6/6] Localised key errors --- public/_locales/en/messages.json | 9 +++++++++ src/options.ts | 8 ++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/public/_locales/en/messages.json b/public/_locales/en/messages.json index e26d1419..e39a7e74 100644 --- a/public/_locales/en/messages.json +++ b/public/_locales/en/messages.json @@ -452,5 +452,14 @@ }, "copyDebugInformationComplete": { "message": "The debug information has been copied to the clip board. Feel free to remove any information you would rather not share. Save this in a text file or paste into the bug report." + }, + "theKey": { + "message": "The key" + }, + "keyAlreadyUsedByYouTube": { + "message": "is already used by youtube. Please select another key." + }, + "keyAlreadyUsed": { + "message": "is bound to another action. Please select another key." } } diff --git a/src/options.ts b/src/options.ts index 4c55bae7..27d2a1f6 100644 --- a/src/options.ts +++ b/src/options.ts @@ -344,9 +344,7 @@ function keybindKeyPressed(element: HTMLElement, e: KeyboardEvent) { if (restrictedKeys.indexOf(key) !== -1 ) { closeKeybindOption(element, button); - // TODO: This should be localised - alert("The key " + key + " is already used by youtube. Please select another key."); - + alert(chrome.i18n.getMessage("theKey") + " " + key + " " + chrome.i18n.getMessage("keyAlreadyUsedByYouTube")); return; } @@ -356,9 +354,7 @@ function keybindKeyPressed(element: HTMLElement, e: KeyboardEvent) { if (key === otherKeybind) { closeKeybindOption(element, button); - // TODO: This should be localised - alert("The key " + key + " is bound to another action. Please select another key."); - + alert(chrome.i18n.getMessage("theKey") + " " + key + " " + chrome.i18n.getMessage("keyAlreadyUsed")); return; }