MediaWiki:Uniwiki.js

From EVE University Wiki
Revision as of 22:33, 31 March 2020 by Rayanth (talk | contribs)
Jump to: navigation, search

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.
/* Any JavaScript here will be loaded for users using the UniWiki skin */


//April Fools 4/1/2020 Code =======================================
opts = {
    pageSelector: ".page-User_Rayanth_SeekritStuff",
    AprilFoolEnable: true,
    className: 'april-fools',
    frequency: 0.1,
    expiryThresholdMs: 2000,
    addStyleIntvl: null,
    clearStyleIntvl: null,
    maxFlicks: 18,
    minFlicks: 6,
};

// The "oh dear gods my eyes, turn it off" Button
$(function(){
    var r = $('<input type="checkbox" name="chkAprilFoolEnable" value="3" class="lcs_check lcs_tt1" checked="checked" autocomplete="off" />');
    var AprilFoolText = '<div style="margin-bottom:1em; padding:0em; font-size: 20px; font-weight:bold; text-align:center; border:1px solid #f43e29; background: #060606; width:auto; color: #ffffff;">The UniWiki is being invaded by the Triglavians!!!</div>';
    $(opts.pageSelector +  " .mw-parser-output").prepend(AprilFoolText, 'Triglavian Invasion' , r);
    startUpAprilFools();
});

function startUpAprilFools(){
    opts.addStyleIntvl = setInterval(() => addStyleToRandomWordsInJQueryObject($(opts.pageSelector +  ' .mw-parser-output'), opts), 2000);
    addStyleToRandomWordsInJQueryObject($(opts.pageSelector +  " .mw-parser-output"), opts);
    opts.clearStyleIntvl = setInterval(() => removeExpiredStyles(opts), 2000);
    opts.AprilFoolEnable = true;
}

function shutDownAprilFools(){
    opts.AprilFoolEnable = false;
    clearInterval(opts.addStyleIntvl);
    clearInterval(opts.clearInterval);
    $('span.april-fools').contents().unwrap();
}

function addStyleToText(text, className, ratio) {
    tokens = text.split(' ');
    result = '';
    for (let token of tokens) {
    if (token.trim() === '') {
        continue;
    }
    const shouldAddStyle = ratio > Math.random();
    if (shouldAddStyle) {
        token = `<span class="${className}" data-timestamp="${Date.now()}">${token}</span>`          
    }
    result += `${token} `;
    }
    return result;
}
  
function addStyleToRandomWordsInJQueryObject(jqueryObject, opts) {
    if (opts.AprilFoolEnable === false) { return };
    cur_time = Date.now()
    let objects = [jqueryObject];
    while (objects.length > 0) {
        const object = objects.pop();
        let $object = $(object);
        if (object.nodeType === Node.TEXT_NODE) {
            if (object.textContent.trim() !== ''){
                const result = addStyleToText(object.textContent, opts.className, opts.frequency);
                $object.replaceWith(result);
            }
        }
        else {
            const contents = $object.contents();
            objects = objects.concat(contents.toArray());
        }
    }
    flicker_text(cur_time);
    flicker_images();
}
  
function removeExpiredStyles(opts) {
    const spans = $('span.april-fools').filter((_, s) => {
        const timestamp = s.attributes['data-timestamp'].value;
        return (Date.now() - timestamp) > opts.expiryThresholdMs;
        })
    .contents()
    .unwrap();
}
  
async function flicker_text(cur_time){
    var amount =  Math.round(Math.random() * (opts.maxFlicks-opts.minFlicks) + opts.minFlicks);
    const spans = $('span.april-fools').filter((_, s) => {
        const timestamp = s.attributes['data-timestamp'].value;
        return timestamp > cur_time;
        })

    for(let iter = 0; iter < amount; iter++)
    {
        spans.toggleClass('april-fools');
        await sleep(Math.round(Math.random() * 100));
    }
    spans.toggleClass('april-fools', true);
}

async function flicker_images(){
    if (opts.frequency < Math.random()) { return };
    var amount =  Math.round(Math.random() * (opts.maxFlicks-opts.minFlicks) + opts.minFlicks);
    const trigs = $('.AF2020_trig')
    const origs = $('.AF2020_orig')
    for(let iter = 0; iter < amount; iter++)
    {
        trigs.toggle();
        origs.toggle();
        await sleep(Math.round(Math.random() * 100));
    }
}

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}


//END April Fools 4/1/2020 Code =======================================