nojs-webring/static/redirect.js
2025-12-30 15:59:38 -05:00

65 lines
1.8 KiB
JavaScript

const sites = ["~jigong", "rafhei0"];
function getRandomSite(ignore) {
// get random index from sites
const index = Math.floor(Math.random() * sites.length);
// if random site is referrer, retry
return index === ignore ? getRandomSite(ignore) : sites[index];
}
const validRedirectTypes = ["next", "previous", "random"];
// parse url params
const params = new URLSearchParams(window.location.search);
let referrerUrl = params.get("referrer");
let type = params.get("type") || "random"; // default to random if no type given
if (!validRedirectTypes.includes(type)) {
// default to random is type not valid
type = "random";
}
if (!sites.includes(referrerUrl)) {
// referrer url is incorrect, select random referrer from sites list,
referrerUrl = sites[Math.floor(Math.random() * sites.length)];
// select random site, and redirect to it
window.location.href = sites[Math.floor(Math.random() * sites.length)];
}
// get index of the referrer url
const referrerIndex = sites.indexOf(referrerUrl);
let redirect;
if (type === "random") {
redirect = getRandomSite(referrerIndex);
} else if (type === "next") {
// get index of next website in list
let nextIndex = referrerIndex + 1;
// if index is greater than the length of list, loop back to 0 index
if (nextIndex > sites.length - 1) {
nextIndex = 0;
}
redirect = sites[nextIndex];
} else {
// redirect to previous
let previousIndex = referrerIndex - 1;
if (previousIndex < 0) {
// if previous index is less than 0, loop around to last index
previousIndex = sites.length - 1;
}
redirect = sites[previousIndex];
}
if (redirect[0] !== "~") {
redirect = `~${redirect}`;
}
if (redirect === "~rafhei0") {
redirect = `https://tilde.green/${redirect}/p/0.html`;
window.location.href = redirect;
} else {
redirect = `https://tilde.green/${redirect}/`;
window.location.href = redirect;
}