mirror of
https://github.com/driftywinds/driftywinds.github.io.git
synced 2025-12-19 11:03:32 +00:00
Better HTTP URL validation
This commit is contained in:
14
js/main.js
14
js/main.js
@@ -1,9 +1,13 @@
|
|||||||
(function () {
|
(function () {
|
||||||
// If no source or source is not a URL
|
// If no source
|
||||||
if (!urlSearchParams.has('source') || !sourceURL.match(urlRegex))
|
if (!urlSearchParams.has('source'))
|
||||||
window.location.replace("index.html");
|
search();
|
||||||
insertAddToAltStoreBanner();
|
// If source is not a valid HTTP URL
|
||||||
})()
|
else if (!isValidHTTPURL(sourceURL)) {
|
||||||
|
alert("Invalid HTTP URL.");
|
||||||
|
search();
|
||||||
|
} else insertAddToAltStoreBanner();
|
||||||
|
})();
|
||||||
|
|
||||||
fetch(sourceURL, {
|
fetch(sourceURL, {
|
||||||
cache: "force-cache"
|
cache: "force-cache"
|
||||||
|
|||||||
40
js/shared.js
40
js/shared.js
@@ -48,18 +48,6 @@ const appHeaderHTML = app => app ? `
|
|||||||
</div>
|
</div>
|
||||||
</div>` : undefined;
|
</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() {
|
function insertAddToAltStoreBanner() {
|
||||||
document.getElementById("top")?.insertAdjacentHTML("afterbegin", `
|
document.getElementById("top")?.insertAdjacentHTML("afterbegin", `
|
||||||
<div class="uibanner">
|
<div class="uibanner">
|
||||||
@@ -96,6 +84,30 @@ function insertNavigationBar(title) {
|
|||||||
setUpBackButton();
|
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) {
|
function setTintColor(color) {
|
||||||
document.querySelector(':root')?.style.setProperty("--accent-color", `#${color}`);
|
document.querySelector(':root')?.style.setProperty("--accent-color", `#${color}`);
|
||||||
}
|
}
|
||||||
@@ -104,6 +116,10 @@ function setUpBackButton() {
|
|||||||
document.getElementById("back")?.addEventListener("click", () => history.back(1));
|
document.getElementById("back")?.addEventListener("click", () => history.back(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function search() {
|
||||||
|
window.location.replace("search.html");
|
||||||
|
}
|
||||||
|
|
||||||
const $ = selector => selector.startsWith("#") && !selector.includes(".") && !selector.includes(" ")
|
const $ = selector => selector.startsWith("#") && !selector.includes(".") && !selector.includes(" ")
|
||||||
? document.getElementById(selector.substring(1))
|
? document.getElementById(selector.substring(1))
|
||||||
: document.querySelectorAll(selector);
|
: document.querySelectorAll(selector);
|
||||||
Reference in New Issue
Block a user