Implemented v1
This commit is contained in:
85
uwuify.js
Normal file
85
uwuify.js
Normal file
@ -0,0 +1,85 @@
|
||||
const uwuify = {
|
||||
// The prefix used in console messages
|
||||
CONSOLE_PREFIX: '[uwuify.js] ',
|
||||
// The range of time to wait between uwuification
|
||||
INTERVAL_MIN: 10000,
|
||||
INTERVAL_MAX: 60000,
|
||||
// Which characters to uwuify
|
||||
TARGET_REGEX: /[LRlr]+/g,
|
||||
// All the #text objects to modify
|
||||
TARGET_ELEMENTS: [],
|
||||
|
||||
// Gets all #text elements on the page
|
||||
getTexts: function(object) {
|
||||
let res = [];
|
||||
|
||||
for (let i = 0; i < object.childNodes.length; i++) {
|
||||
if (object.childNodes[i].nodeName == '#text') {
|
||||
res.push(object.childNodes[i]);
|
||||
} else {
|
||||
res = res.concat(uwuify.getTexts(object.childNodes[i]));
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
},
|
||||
|
||||
// Generates a random integer between min and max
|
||||
getRandomInt: function (min, max) {
|
||||
const minCeiled = Math.ceil(min);
|
||||
const maxFloored = Math.floor(max);
|
||||
return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled); // The maximum is exclusive and the minimum is inclusive
|
||||
},
|
||||
|
||||
// Returns true if a string is able to be uwued
|
||||
isUwuable: function(string) {
|
||||
let res = Boolean(uwuify.TARGET_REGEX.exec(string));
|
||||
uwuify.TARGET_REGEX.lastIndex = 0
|
||||
return res;
|
||||
},
|
||||
|
||||
// Uwuify a random target
|
||||
uwuify: function() {
|
||||
let targetIdx = uwuify.getRandomInt(0, uwuify.TARGET_ELEMENTS.length);
|
||||
|
||||
let shifts = 0;
|
||||
let element = uwuify.TARGET_ELEMENTS[targetIdx]
|
||||
let text = element.textContent;
|
||||
while (!uwuify.isUwuable(text) && shifts < uwuify.TARGET_ELEMENTS.length) {
|
||||
shifts++;
|
||||
element = uwuify.TARGET_ELEMENTS[(targetIdx+shifts)%uwuify.TARGET_ELEMENTS.length];
|
||||
text = element.textContent;
|
||||
}
|
||||
|
||||
if (!uwuify.isUwuable(text)) {
|
||||
console.info(`${uwuify.CONSOLE_PREFIX} No more text to uwuify.`);
|
||||
// Return from the function, preventing the next setTimeout().
|
||||
return
|
||||
}
|
||||
|
||||
let matches = text.matchAll(uwuify.TARGET_REGEX).toArray();
|
||||
let matchIdx = uwuify.getRandomInt(0, matches.length);
|
||||
let match = matches[matchIdx];
|
||||
|
||||
for (let chrOff = 0; chrOff < match[0].length; chrOff++) {
|
||||
let chrIdx = match.index + chrOff;
|
||||
let isUpper = text[chrIdx] == text[chrIdx].toUpperCase();
|
||||
element.textContent = text.slice(0, chrIdx) + (isUpper ? 'W' : 'w') + text.slice(chrIdx+1);1
|
||||
}
|
||||
|
||||
setTimeout(uwuify.uwuify, uwuify.getRandomInt(uwuify.INTERVAL_MIN, uwuify.INTERVAL_MAX));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
window.addEventListener('load', () => {
|
||||
console.debug(`${uwuify.CONSOLE_PREFIX} Document loaded.`);
|
||||
let elements = uwuify.getTexts(document.body);
|
||||
for (let i = 0; i < elements.length; i++) {
|
||||
if (uwuify.isUwuable(elements[i].textContent)) {
|
||||
uwuify.TARGET_ELEMENTS.push(elements[i]);
|
||||
}
|
||||
}
|
||||
setTimeout(uwuify.uwuify, uwuify.getRandomInt(uwuify.INTERVAL_MIN, uwuify.INTERVAL_MAX));
|
||||
});
|
||||
Reference in New Issue
Block a user