User:Jeeputer/defconIndicator.js

// Fork of User:Enterprisey/watchlist-notice.js to show defcon level

// icon on top of the page, next to the notification icon

mw.loader.using('mediawiki.util', function() {

var showDefcon = function(icon, level) {

$('#pt-notifications-notice').after(

$('

  • ')

    .append(

    $('')

    .css({

    'font-size': '80%'

    })

    .text('defcon:')

    .attr({

    'title': 'Vandalism Level'

    })

    )

    .append(

    $('')

    .css({

    'padding': '0.25em 0.45em 0.2em',

    'cursor': 'pointer'

    })

    .attr('src',

    'https://upload.wikimedia.org/wikipedia/commons/' +

    icon)

    .click(updateDefcon)

    )

    .css({

    'opacity': '1',

    'transition': 'opacity 0.5s'

    })

    .attr('id', 'defcon-indicator')

    );

    };

    var changeIcon = function(icon, level) {

    $('#defcon-indicator img').attr(

    'src',

    'https://upload.wikimedia.org/wikipedia/commons/' +

    icon,

    'title', 'Vandalism level: ' + level + '; Click to update!'

    );

    $('#defcon-indicator').css('opacity', '1');

    };

    var updateDefcon = function() {

    $('#defcon-indicator').css('opacity', '0');

    $.getJSON(

    mw.util.wikiScript('api'), {

    format: 'json',

    action: 'parse',

    page: 'User:EnterpriseyBot/defcon',

    prop: 'wikitext'

    }).done(function(data) {

    var colors = {

    '1': '6/6d/Ledred.png',

    '2': '8/80/Ledorange.png',

    '3': '0/0a/Ledyellow.png',

    '4': '5/58/Ledgreen.png',

    '5': '7/71/Ledblue.png'

    };

    var wikitext = data.parse.wikitext['*'];

    var level = wikitext.match(

    /\|\s*level\s*\=\s*([0-9])/)[1];

    if (level) {

    var icon = colors[level];

    if (!$('#defcon-indicator').length) {

    showDefcon(icon, level);

    } else {

    changeIcon(icon, level);

    }

    } else {

    mw.notify($('Looks like the wikitext at the ' +

    'defcon page ' +

    'has been changed so the script cannot find the level.'

    ), {

    type: 'error',

    autoHide: 'false',

    tag: 'defcon-error'

    });

    }

    });

    };

    $(document).ready(function() {

    updateDefcon();

    window.setInterval(updateDefcon, 120000);

    });

    });