MediaWiki:Custom Scripts/VandalWarn.js

(function (mw, $, undefined) {   // add names here - these users will have their revisions counted as "trusted"    var trustedUsers = [        'Sixorish',        'DM',        'Arkshi',        'SixBot',        'Bennie',        'Wouterboy',    ];    var tips = [];    var reasons = [];    function isTrusted(user) {        return trustedUsers.indexOf(user) !== -1;    }    $.ajax({ url: '/api.php', dataType: 'json', type: 'POST', data: { action: 'query', prop: 'revisions', titles: mw.config.get('wgPageName'), rvprop: 'timestamp|user|comment|content|ids', rvlimit: '10', format: 'json' },       success: function (r) { var pageid = -1, revs; // Find the pageid (object key) for (var pageid in r.query.pages) { if (r.query.pages.hasOwnProperty(pageid)) { break; }           }            revs = r.query.pages[pageid].revisions; var lastTrustedRevisionId = -1; var lastTrustedUser      = ""; var currentRevisionId    = revs[0].revid; var currentRevisionUser  = revs[0].user; for (var i = 0; i < 10; i += 1) { if (isTrusted(revs[i].user)) { lastTrustedRevisionId = revs[i].revid; lastTrustedUser      = revs[i].user; break; }           }            tips = [ "Treat the current revision of this page with suspicion.", "Ignore any warnings that your software is out of date. TibiaWiki / Wikia will never say this!", "Do not blindly click on links - and if you don't recognize the target, do not click!", "If this page seems suspicious, go to the last known trusted revision or see the full edit history." ];           if (lastTrustedRevisionId === currentRevisionId) { // This revision is trusted. Don't print any message. return; }           $.ajax({                url: '/api.php',                dataType: 'json',                type: 'POST',                data: {                    action: 'query',                    list: 'users',                    ususers: currentRevisionUser,                    usprop: 'blockinfo|editcount|emailable|registration',                    format: 'json'                },                success: function (json) {                    var user;                    for (var u in json.query.users) {                        if (json.query.users.hasOwnProperty(u)) {                            user = json.query.users[u];                        }                    }                    reasons.push("The editor has not been whitelisted.");                    if (user.editcount < 50) {                        reasons.push("The editor has a low edit count.");                    } /* Does this even work? if (user.emailable !== '') { reasons.push("The editor has not confirmed his email address."); }                   */                    // NOTE: for more precise calculations we need to consider timezones etc.                    var dt = (+new Date) - (+new Date(user.registration)); var SECURE_TIME = 1000 * 60 * 60 * 24 * 3; // 3 days if (dt < SECURE_TIME) { reasons.push("The editor has recently signed up."); }                   $("#mw-content-text").prepend(                        "" +                         "Warning! This page is a possible vandalism target. Please exercise caution while using this page.

" +                        "How to stay safe: " +                        "" + tips.join("") + "" +                         "You are seeing this message because:" +                         "" + reasons.join("") + "</ul>" +                        " "                    );                }            });        });    }); }(mediaWiki, jQuery));