Difference between revisions of "MediaWiki:Gadget-Resettify/Runtime.js"

From Nookipedia, the Animal Crossing wiki
(Adjust p selector (only select paragraphs with content))
(Replacing images with Resetti)
Line 1: Line 1:
function getRandomInt(max, exclude) {
+
function getRandomInt(max, exclude = -1) {
 
     let randomNum = Math.floor(Math.random() * max);
 
     let randomNum = Math.floor(Math.random() * max);
 
     while (randomNum === exclude) {
 
     while (randomNum === exclude) {
Line 51: Line 51:
 
         "UNDERSTAND?!",
 
         "UNDERSTAND?!",
 
         "AAAAAAAAH, FORGET IT!"
 
         "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'
 
     ];
 
     ];
  
Line 63: Line 71:
  
 
     // For each paragraph...
 
     // For each paragraph...
     let textBeginLastIndex = 100;
+
     let textBeginLastIndex = -1;
     let textEndLastIndex = 100;
+
     let textEndLastIndex = -1;
 
     paragraphs.forEach((paragraph, index) => {
 
     paragraphs.forEach((paragraph, index) => {
 
         let textElBegin = document.createElement('span');
 
         let textElBegin = document.createElement('span');
Line 94: Line 102:
 
     });
 
     });
 
      
 
      
 +
    // Hide Resetti text inside of tables cause it's a mess
 
     document.querySelectorAll('table .nookipedia-resetti-text').forEach((paragraph) => {
 
     document.querySelectorAll('table .nookipedia-resetti-text').forEach((paragraph) => {
 
     paragraph.style.display = 'none';
 
     paragraph.style.display = 'none';
 
     });
 
     });
 +
 +
    // Replace all images with Resetti
 +
    document.querySelectorAll('.mw-parser-output img').forEach((img) => {
 +
        img.src = resettiImages[getRandomInt(resettiImages.length)];
 +
        img.srcset = '';
 +
        img.alt = "Resetti the angry mole";
 +
        img.style.maxWidth = 150;
 +
      });
 
}
 
}
  

Revision as of 03:19, April 1, 2024

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').forEach((paragraph) => {
    	paragraph.style.display = 'none';
    });

    // Replace all images with Resetti
    document.querySelectorAll('.mw-parser-output img').forEach((img) => {
        img.src = resettiImages[getRandomInt(resettiImages.length)];
        img.srcset = '';
        img.alt = "Resetti the angry mole";
        img.style.maxWidth = 150;
      });
}

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

showResetti();