diff options
Diffstat (limited to 'static')
-rw-r--r-- | static/redirect.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/static/redirect.js b/static/redirect.js new file mode 100644 index 0000000..338e98b --- /dev/null +++ b/static/redirect.js @@ -0,0 +1,51 @@ +function getRandomSite(ignore) { + // get random index from sites + var index = Math.floor(Math.random() * sites.length); + // if random site is referrer, retry + return (index === ignore) ? generateRandom(ignore) : sites[index]; +} + +var validRedirectTypes = ['next', 'previous', 'random'] + +// parse url params +var params = new URLSearchParams(window.location.search); +var referrerUrl = params.get('referrer') +var 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 +var referrerIndex = sites.indexOf(referrerUrl); + +if (type === 'random') { + var redirect = getRandomSite(referrerIndex) +} else if (type === 'next') { + // get index of next website in list + var nextIndex = referrerIndex + 1 + console.log(nextIndex) + + // if index is greater than the length of list, loop back to 0 index + if (nextIndex > sites.length - 1) { + nextIndex = 0 + } + + var redirect = sites[nextIndex] +} else { // redirect to previous + var previousIndex = referrerIndex - 1 + if (previousIndex < 0) { // if previous index is less than 0, loop around to last index + previousIndex = sites.length - 1 + } + + var redirect = sites[previousIndex] +} + +window.location.href = redirect
\ No newline at end of file |