mirror of
https://github.com/ajayyy/SponsorBlock.git
synced 2025-12-11 22:17:21 +03:00
Merge pull request #302 from Joe-Dowd/restrict-keybindings
Restrict keybindings
This commit is contained in:
@@ -452,5 +452,14 @@
|
|||||||
},
|
},
|
||||||
"copyDebugInformationComplete": {
|
"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."
|
"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."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -331,16 +331,39 @@ function activateKeybindChange(element: HTMLElement) {
|
|||||||
function keybindKeyPressed(element: HTMLElement, e: KeyboardEvent) {
|
function keybindKeyPressed(element: HTMLElement, e: KeyboardEvent) {
|
||||||
var key = e.key;
|
var key = e.key;
|
||||||
|
|
||||||
let button = element.querySelector(".trigger-button");
|
if (["Shift", "Control", "Meta", "Alt", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", "Tab"].indexOf(key) !== -1) {
|
||||||
|
|
||||||
// cancel setting a keybind
|
// Wait for more
|
||||||
if (key === "Escape") {
|
document.addEventListener("keydown", (e) => keybindKeyPressed(element, e), {once: true});
|
||||||
element.querySelector(".option-hidden-section").classList.add("hidden");
|
} else {
|
||||||
button.classList.remove("disabled");
|
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 ) {
|
||||||
|
closeKeybindOption(element, button);
|
||||||
|
|
||||||
|
alert(chrome.i18n.getMessage("theKey") + " " + key + " " + chrome.i18n.getMessage("keyAlreadyUsedByYouTube"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let option = element.getAttribute("sync-option");
|
// 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) {
|
||||||
|
closeKeybindOption(element, button);
|
||||||
|
|
||||||
|
alert(chrome.i18n.getMessage("theKey") + " " + key + " " + chrome.i18n.getMessage("keyAlreadyUsed"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// cancel setting a keybind
|
||||||
|
if (key === "Escape") {
|
||||||
|
closeKeybindOption(element, button);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Config.config[option] = key;
|
Config.config[option] = key;
|
||||||
|
|
||||||
@@ -352,6 +375,18 @@ function keybindKeyPressed(element: HTMLElement, e: KeyboardEvent) {
|
|||||||
|
|
||||||
button.classList.remove("disabled");
|
button.classList.remove("disabled");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
* Will trigger the textbox to appear to be able to change an option's text.
|
||||||
|
|||||||
Reference in New Issue
Block a user