datawebring

A webring for data people who write
git clone https://git.eamoncaddigan.net/datawebring.git
Log | Files | Refs | README

commit 882cdf4c00cee6f4ce1a112b70a125d06f59fe80
parent ed9e8edbf9678714cb2ac23067cc0978dcc558f2
Author: Randy Au <randy.au@gmail.com>
Date:   Fri, 20 Sep 2024 17:15:58 -0400

Add daily shuffling to next/prev
Diffstat:
Monionring-widget.js | 30++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+), 0 deletions(-)

diff --git a/onionring-widget.js b/onionring-widget.js @@ -9,6 +9,7 @@ var tag = document.getElementById(ringID); //find the widget on the page thisSite = window.location.href; //get the url of the site we're currently on thisIndex = null; +shuffleWithSeed(sites, getDailySeed()) // go through the site list to see if this site is on it and find its position for (i = 0; i < sites.length; i++) { @@ -18,6 +19,35 @@ for (i = 0; i < sites.length; i++) { } } +// We want the site array to be shuffled once every day, but be consistent for that day +function getDailySeed() { + const today = new Date(); + today.setHours(0, 0, 0, 0); // Set time to midnight + return today.getTime(); // Use timestamp as seed +} + +function shuffleWithSeed(array, seed) { + // Simple seeded PRNG (not cryptographically secure) + function mulberry32(a) { + return function() { + let t = a += 0x6D2B79F5; + t = Math.imul(t ^ t >>> 15, t | 1); + t ^= t + Math.imul(t ^ t >>> 7, t | 61); + return ((t ^ t >>> 14) >>> 0) / 4294967296; + } + } + + const rng = mulberry32(seed); + + // Fisher-Yates shuffle algorithm + for (let i = array.length - 1; i > 0; i--) { + const j = Math.floor(rng() * (i + 1)); + [array[i], array[j]] = [array[j], array[i]]; + } + + return array; +} + function randomSite() { otherSites = sites.slice(); //create a copy of the sites list otherSites.splice(thisIndex, 1); //remove the current site so we don't just land on it again