Better HTTP URL validation

This commit is contained in:
foxster-mp4
2023-04-30 11:01:55 -07:00
parent e617f82523
commit a94b34225d
2 changed files with 37 additions and 17 deletions

View File

@@ -1,9 +1,13 @@
(function () {
// If no source or source is not a URL
if (!urlSearchParams.has('source') || !sourceURL.match(urlRegex))
window.location.replace("index.html");
insertAddToAltStoreBanner();
})()
// If no source
if (!urlSearchParams.has('source'))
search();
// If source is not a valid HTTP URL
else if (!isValidHTTPURL(sourceURL)) {
alert("Invalid HTTP URL.");
search();
} else insertAddToAltStoreBanner();
})();
fetch(sourceURL, {
cache: "force-cache"

View File

@@ -48,18 +48,6 @@ const appHeaderHTML = app => app ? `
</div>
</div>` : undefined;
function formatString(string) {
if (!string) return undefined;
// URLs
const urlArray = string.match(urlRegex);
const urlSet = [...new Set(urlArray)]; // Converting to set to remove duplicates
urlSet.forEach(url => string = string.replaceAll(url, `<a href="${url}">${url}</a>`));
// New lines
return string.replaceAll("\n", "<br>");
}
function insertAddToAltStoreBanner() {
document.getElementById("top")?.insertAdjacentHTML("afterbegin", `
<div class="uibanner">
@@ -96,6 +84,30 @@ function insertNavigationBar(title) {
setUpBackButton();
}
// https://stackoverflow.com/a/43467144/19227228
function isValidHTTPURL(string) {
var url;
try {
url = new URL(string);
} catch (error) {
console.error("An error occurred.", error);
return false;
}
return url.protocol == "http:" || url.protocol == "https:";
}
function formatString(string) {
if (!string) return undefined;
// URLs
const urlArray = string.match(urlRegex);
const urlSet = [...new Set(urlArray)]; // Converting to set to remove duplicates
urlSet.forEach(url => string = string.replaceAll(url, `<a href="${url}">${url}</a>`));
// New lines
return string.replaceAll("\n", "<br>");
}
function setTintColor(color) {
document.querySelector(':root')?.style.setProperty("--accent-color", `#${color}`);
}
@@ -104,6 +116,10 @@ function setUpBackButton() {
document.getElementById("back")?.addEventListener("click", () => history.back(1));
}
function search() {
window.location.replace("search.html");
}
const $ = selector => selector.startsWith("#") && !selector.includes(".") && !selector.includes(" ")
? document.getElementById(selector.substring(1))
: document.querySelectorAll(selector);