Added server address input validation.

This commit is contained in:
Ajay Ramachandran 2020-02-08 21:58:50 -05:00
parent be6cd62fbe
commit 1735cdb45d
3 changed files with 43 additions and 2 deletions

View file

@ -400,9 +400,12 @@
"message": "SponsorBlock Server Address"
},
"customServerAddressDescription": {
"message": "The address SponsorBlock uses to make calls to the server.\nIt must be formatted https://domain with no trailing forward slash (/).\nUnless you have your own server instance, this should not be changed."
"message": "The address SponsorBlock uses to make calls to the server.\nUnless you have your own server instance, this should not be changed."
},
"saveCustomServerAddress": {
"message": "Save"
},
"customAddressError": {
"message": "This address is not in the right form. Make sure you have http:// or https:// at the begining and no slashes or sub-folders at the end."
}
}

View file

@ -41,7 +41,7 @@
<br/>
<br/>
<div option-type="text-change" sync-option="invidiousInstances">
<div option-type="private-text-change" sync-option="invidiousInstances">
<div class="option-button trigger-button">
__MSG_addInvidiousInstance__
</div>

View file

@ -63,6 +63,20 @@ async function init() {
textInput.value = Config.config[textChangeOption];
setButton.addEventListener("click", () => {
// See if anything extra must be done
switch (textChangeOption) {
case "serverAddress":
let result = validateServerAddress(textInput.value);
if (result !== null) {
textInput.value = result;
} else {
return;
}
break;
}
Config.config[textChangeOption] = textInput.value;
});
@ -325,3 +339,27 @@ function activatePrivateTextChange(element: HTMLElement) {
element.querySelector(".option-hidden-section").classList.remove("hidden");
}
/**
* Validates the value used for the database server address.
* Returns null and alerts the user if there is an issue.
*
* @param input Input server address
*/
function validateServerAddress(input: string): string {
// Trim the last slash if needed
if (input.endsWith("/")) {
input = input.substring(0, input.length - 1);
}
// Isn't HTTP protocol or has extra slashes
if ((!input.startsWith("https://") && !input.startsWith("http://"))
|| input.replace("://", "").includes("/")) {
alert(chrome.i18n.getMessage("customAddressError"));
return null;
}
return input;
}