MediaWiki:Gadget-Resettify/Runtime.js

From Nookipedia, the Animal Crossing wiki

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
function getRandomInt(max, exclude = -1) {
    let randomNum = Math.floor(Math.random() * max);
    while (randomNum === exclude) {
        randomNum = Math.floor(Math.random() * max);
    }

    return randomNum;
}

function resettify() {
    let resettiImg = document.querySelector('#nookipedia-resetti');
    if (resettiImg) {
        resettiImg.style.display = 'none';
    }

    // Set up text that appears in first paragraph: 
    const resettiInitialText = 'GRAAAH! YOU AGAIN? Well, listen here...';
    const initialTextEl = document.createElement('span');
    initialTextEl.classList.add('nookipedia-resetti-text');
    initialTextEl.innerText = resettiInitialText + ' ';
    initialTextEl.style.fontWeight = '600';
    initialTextEl.style.fontSize = '120%';

    // Array of text for paragraph start:
    const resettiTextBegin = [
        "YOU'RE STILL HERE?!",
        "KEEP LISTENING, PAL!",
        "NOW PAY ATTENTION, FOR REAL!",
        "ARE YOU STILL LISTENING?",
        "AAAAAAARGH!",
        "HAAH...  HAAAH...HAAAAAAH...",
        "I'M NOT DONE TALKING TO YOU!",
        "WATCH IT, PUNK!",
        "I TALK AND I TALK, BUT I GET NOWHERE!",
        "YOU GONNA MAKE ME KEEP GOING?",
        "I'M GETTING REAL TIRED OF EXPLAINING..."
    ];

    // Array of text for paragraph end:
    const resettiTextEnd = [
        "GRAAAH!",
        "WHAT DON'T YOU GET?!",
        "IS THAT CLEAR?!",
        "I HOPE YOU GOT THAT!",
        "NOW SCRAM!",
        "BRAIN...HURTS!",
        "IT'S DRIVIN' ME NUTS!",
        "Hey...STOP EYEING THAT RESET BUTTON!",
        "DON'T FORGET IT!",
        "YOU HEAR ME?!",
        "UNDERSTAND?!",
        "AAAAAAAAH, FORGET IT!"
    ];

    const resettiImages = [
        'https://dodo.ac/np/images/thumb/4/43/Resetti_PG.png/320px-Resetti_PG.png',
        'https://dodo.ac/np/images/a/a1/Resetti_WW_2.png',
        'https://dodo.ac/np/images/thumb/1/14/Resetti_CF.png/281px-Resetti_CF.png',
        'https://dodo.ac/np/images/thumb/6/63/Resetti_NL.png/261px-Resetti_NL.png',
        'https://dodo.ac/np/images/b/b9/WWGold_Resetti_amiibo.png'
    ];

    // Set all text to uppercase:
    const body = document.querySelector('body');
    if (body) {
        body.style.textTransform = 'uppercase';
    }

    // Grab all paragraph tags in content:
    const paragraphs = document.querySelectorAll('.mw-parser-output p:has(*)');

    // For each paragraph...
    let textBeginLastIndex = -1;
    let textEndLastIndex = -1;
    paragraphs.forEach((paragraph, index) => {
        let textElBegin = document.createElement('span');
        textElBegin.classList.add('nookipedia-resetti-text');
        textElBegin.style.fontWeight = '600';
        textElBegin.style.fontSize = '120%';
        let textElEnd = document.createElement('span');
        textElEnd.classList.add('nookipedia-resetti-text');
        textElEnd.style.fontWeight = '600';
        textElEnd.style.fontSize = '120%';

        if (index === 0) {
            paragraph.prepend(initialTextEl);
            let textEndRandomNum = getRandomInt(resettiTextEnd.length, textEndLastIndex);
            textEndLastIndex = textEndRandomNum;
            textElEnd.innerText = ' ' + resettiTextEnd[textEndRandomNum];
            paragraph.append(textElEnd);
        } else {
            let textBeginRandomNum = getRandomInt(resettiTextBegin.length, textEndLastIndex);
            textBeginLastIndex = textBeginRandomNum;
            textElBegin.innerText = resettiTextBegin[textBeginRandomNum] + ' ';
            paragraph.prepend(textElBegin);

            let textEndRandomNum = getRandomInt(resettiTextEnd.length, textEndLastIndex);
            textEndLastIndex = textEndRandomNum;
            textElEnd.innerText = ' ' + resettiTextEnd[textEndRandomNum];
            paragraph.append(textElEnd);
        }
    });
    
    // Hide Resetti text inside of tables cause it's a mess
    document.querySelectorAll('table .nookipedia-resetti-text, div[style*="grid"] .nookipedia-resetti-text').forEach((paragraph) => {
    	paragraph.style.display = 'none';
    });

    // Replace all images with Resetti
    let lastImgIndex = -1;
    document.querySelectorAll('#content img[src*="dodo.ac/"]').forEach((img) => {
    	let imgRandomNum = getRandomInt(resettiImages.length, lastImgIndex);
        lastImgIndex = imgRandomNum;
        img.src = resettiImages[imgRandomNum];
        img.srcset = '';
        img.alt = "Resetti the angry mole";
        img.style.maxWidth = '150';
        img.style.height = 'auto';
        img.classList.add('nookipedia-resettified-img');
      });
}

function showResetti() {
    const resetti = document.createElement('img');
    resetti.id = 'nookipedia-resetti';
    resetti.width = '80';
    resetti.style.position = 'fixed';
    resetti.style.bottom = '-10px';
    resetti.style.zIndex = '999999999';
    resetti.style.left = Math.floor(Math.random() * (75 - 25) + 25) + '%';
    resetti.style.cursor = 'pointer';
    resetti.src = 'https://dodo.ac/np/images/e/e3/Mr._Resetti_Yell_LINE_Animated_Sticker.png';
    document.getElementsByTagName('body')[0].appendChild(resetti);
    resetti.addEventListener("click", resettify);
}

showResetti();