User:CosmicChronos/global.js

importArticles({   type: 'script',    articles: [        'u:dev:MediaWiki:QuickDiff/code.js',    ] }); (function ($, mw) {   "use strict";    // double-run protection    if (window.quickDiffLoaded) {        return;    }    window.quickDiffLoaded = true;    var diffStylesModule = "mediawiki.action.history.diff";    var i18n;    var modal;    var special = {};    // "Special:Diff/12345" and "Special:ComparePages" link detection    function initSpecialPageStrings {        special.diffDefault = mw.util.getUrl("Special:Diff/");        special.compareDefault = mw.util.getUrl("Special:ComparePages");        var wiki = mw.config.get("wgDBname");        var storageKeyDiff = "QuickDiff-specialdiff_" + wiki;        var storageKeyCompare = "QuickDiff-specialcompare_" + wiki;        try {            special.diff = localStorage.getItem(storageKeyDiff);            special.compare = localStorage.getItem(storageKeyCompare);            // tidy up old storage - can be removed after a while            localStorage.removeItem("QuickDiff-specialdiff"); localStorage.removeItem("QuickDiff-specialcompare"); } catch (ignore) {} if (special.diff && special.compare) { // using stored values - no need for api request return; }       $.getJSON(mw.util.wikiScript("api"), {            action: "parse",            format: "json",            prop: "text",            text: " Special:Diff/ Special:ComparePages ",            disablepp: "" // note: deprecated in MW 1.26, but needed for older versions        }).done(function (data) {            var $parsed = $(data.parse.text["*"]);            special.diff = $parsed.find(".diff > a").attr("href");            special.compare = $parsed.find(".compare > a").attr("href");            try {                localStorage.setItem(storageKeyDiff, special.diff);                localStorage.setItem(storageKeyCompare, special.compare);            } catch (ignore) {}        }); }   function getDiffTitle($diff) { var prevTitle = $diff.find("#mw-diff-otitle1 a").attr("title"); var currTitle = $diff.find("#mw-diff-ntitle1 a").attr("title"); if (prevTitle && prevTitle !== currTitle) { return i18n("differences-multipage", prevTitle, currTitle).plain; }       return i18n("differences", currTitle).plain; }   function loadDiff(url) { modal.show({           loading: true,            title: !modal.visible && i18n("loading").plain        }); // add 'action=render' and 'diffonly' params to save some bytes on each request url.extend({           action: "render",            diffonly: "1"        }); // pass through 'bot' param for rollback links if it's in use on the current page if (mw.util.getParamValue("bot")) { url.extend({bot: "1"}); }       $.when(            $.get(url.getRelativePath),            mw.loader.using(diffStylesModule)        ).always(function (response) {            delete url.query.action;            delete url.query.diffonly;            delete url.query.bot;            var data = {                url: url.toString            };            var $diff;            if (typeof response[0] === "string") {                var $content = $(response[0]);                $diff = $content.filter("table.diff, #mw-rev-deleted-no-diff");                if (!$diff.length) {                    // $content is a complete page - see if a diff can be found                    // needed for diffs from special pages as they ignore action=render URL parameter                    $diff = $content.find("table.diff");                }            }            if ($diff && $diff.length) {                data.content = $diff; data.hook = "quickdiff.ready"; data.title = getDiffTitle($diff); } else { data.content = i18n("error", data.url).escape; }           modal.show(data); });   }    function linkClickHandler(event) {        // ignore clicks with modifier keys to avoid overriding browser features        if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) {            return;        }        var target = event.currentTarget;        var url = new mw.Uri(target.href);        // cross-domain requests not supported        if (url.host !== location.hostname) {            return;        }        // no fragment check is to ensure section links/collapsible trigger links on diff pages are ignored        var hasDiffParam = url.query.diff !== undefined                && url.fragment === undefined;        var isSpecialDiffLink = url.path.indexOf(special.diff) === 0                || url.path.indexOf(special.diffDefault) === 0;        var isSpecialCompareLink = url.path.indexOf(special.compare) === 0                || url.path.indexOf(special.compareDefault) === 0; if (hasDiffParam || isSpecialDiffLink || isSpecialCompareLink) { event.preventDefault; loadDiff(url); }   }    function init { window.dev.i18n.loadMessages("QuickDiff").done(function (i18nInstance) {           i18n = i18nInstance.msg;        }); modal = new mw.libs.QDmodal("quickdiff-modal"); // full screen modal mw.util.addCSS("#quickdiff-modal { height: 100%; width: 100% }"); // diff styles module was renamed in MW 1.28 if (mw.loader.getState("mediawiki.diff.styles")) { diffStylesModule = "mediawiki.diff.styles"; }       // attach to body for compatibility with ajax-loaded content // also, one attached event handler is better than hundreds! $(document.body).on("click.quickdiff", "a[href]", linkClickHandler); initSpecialPageStrings; }   function initDependencies { var devLoadUrl = "https://dev.wikia.com/load.php?mode=articles&only=scripts&articles=MediaWiki:"; var waitFor = [ mw.loader.using(["mediawiki.Uri", "mediawiki.util"]) ];       if (!mw.libs.QDmodal) { waitFor.push($.ajax({ cache: true, dataType: "script", url: devLoadUrl + "QDmodal.js" }));       }        if (!(window.dev && window.dev.i18n)) { waitFor.push($.ajax({ cache: true, dataType: "script", url: devLoadUrl + "I18n-js/code.js" }));       }        $.when.apply($, waitFor).done(init); }   initDependencies; // add open link button to footer mw.hook("quickdiff.ready").add(function (modal) {       var $diffLink = $("")            .attr({ "class": "qdmodal-button", href: modal.data.url, target: "_blank" })           .click(function (event) { // prevent quickdiff intercepting click event.stopPropagation; })           .text(i18n("link").plain);        modal.$footer.append($diffLink);    }); // collect action links (edit, undo, rollback, patrol) and add them to footer mw.hook("quickdiff.ready").add(function (modal) {       // edit/undo links use "mw-rev-head-action" class on Wikia,        // and "mw-diff-edit" or "-undo" class on MW 1.24+        var $buttons = modal.$content.find(".diff-ntitle").find( ".mw-rev-head-action, .mw-diff-edit, .mw-diff-undo, .mw-rollback-link, .patrollink" ).clone;       // remove text nodes (the brackets around each link)        $buttons.contents.filter(function (ignore, element) { return element.nodeType === 3; }).remove;       $buttons.find("a")            .addClass("qdmodal-button")            .attr("target", "_blank");        modal.$footer.append($buttons);    }); }(jQuery, mediaWiki));