Skip to content

Instantly share code, notes, and snippets.

@Denperidge
Last active May 6, 2021 20:28
Show Gist options
  • Save Denperidge/37a3996818e3e4dd9c92749340a50f1b to your computer and use it in GitHub Desktop.
Save Denperidge/37a3996818e3e4dd9c92749340a50f1b to your computer and use it in GitHub Desktop.
Randomizer for your website. I don't recommend using this.
<script>
function rng(min, max) {
// Inclusive rng function from W3Schools https://www.w3schools.com/js/js_random.asp
return Math.floor(Math.random() * (max - min + 1) ) + min;
}
var destructibleTextElements =
jQuery("span,p,h2,h4,h5,a,label")
.filter(function() {
// Visibility check through offsetparent from from https://stackoverflow.com/a/21696585
if (this.childElementCount == 0 && this.offsetParent != null && parseInt(getComputedStyle(this).height) > 8) return true;
else return false;
});
function randomTextCapitalization(index, elem) {
if (elem.innerText != "" && !rng(0,1)) {
var oldText = elem.innerText;
var newText = "";
for (var i = 0; i < oldText.length; i++) {
var letter = oldText[i];
if(!rng(0, 2)) letter = letter.toUpperCase();
else letter = letter.toLowerCase();
newText += letter;
}
elem.innerText = newText;
}
}
var text = "";
var timeout = 500;
var a = destructibleTextElements[rng(0, destructibleTextElements.length - 1)];
function appendA() {
// https://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
a.innerText += String.fromCharCode(rng(65, 90));
// 1 in 5 chance of different a
if (!rng(0,4)) {
a = destructibleTextElements[rng(0, destructibleTextElements.length - 1)];
}
timeout -= rng(0,20);
if (timeout < 1) timeout = 750;
setTimeout(appendA, timeout);
}
function AttemptAutoplay() {
setTimeout(function() {
jQuery("audio").each(function(index, elem) {
elem.volume = 0.05;
elem.play().then(x => { console.log("Succesfully started audio"); }).catch(err => { console.log("Failed to start audio, retrying...."); AttemptAutoplay(); });
});
}, rng(500, 3000));
}
AttemptAutoplay();
appendA();
destructibleTextElements.each(randomTextCapitalization);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment