Jump to content

User:MC10/UsefulTabs.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// A few useful tabs
// Author: MC10
// Linkback: [[User:MC10/UsefulTabs.js]]

(function($, undefined) { // Wrap with jQuery
    // Purge
    $(function() {
        if (mw.config.get("wgNamespaceNumber") === -1) // No special pages
            return;

        var url = mw.config.get("wgServer") + mw.config.get("wgScript") + "?title=" + mw.config.get("wgPageName") + "&action=purge";
        mw.util.addPortletLink('p-cactions', url, 'Purge', 'ca-purge', 'Purge server cache for this page', '0');
    });

    // Special:PrefixIndex
    $(function() {
        if (mw.config.get("wgTitle").indexOf("/") !== -1 || mw.config.get("wgAction") === "history") // No subpages or history
            return;

        if (mw.config.get("wgNamespaceNumber") === 2 || mw.config.get("wgNamespaceNumber") === 3) {
            var username = encodeURIComponent(mw.config.get("wgTitle")),
                checkurl = mw.config.get("wgServer") + mw.config.get("wgScriptPath") + "/index.php?title=Special:PrefixIndex&prefix=" + username + "&namespace=2" ;

            mw.util.addPortletLink("p-cactions", checkurl, "Subpages", "ca-contrib", "Subpages of user");
        }
    });

    // Edit count
    $(function() {
        if (mw.config.get("wgTitle").indexOf("/") !== -1 || mw.config.get("wgAction") === "history") // No subpages or history
            return;

        if (mw.config.get("wgNamespaceNumber") === 2 || mw.config.get("wgNamespaceNumber") === 3) {
            var username = encodeURIComponent(mw.config.get("wgTitle")),
                checkurl = "//toolserver.org/~soxred93/pcount/index.php?name=" + username + "&lang=en&wiki=wikipedia";

            mw.util.addPortletLink("p-cactions", checkurl, "Edits", "ca-contrib", "Analysis of edits by user");
        }
    });

    // Quick edit count
    $(function() {
        var quickEditCount = {
            retrieveData: function(user) {
                $.ajax({
                    url: mw.config.get("wgServer") + mw.config.get("wgScriptPath") + "/api.php?action=query&format=json&list=users&usprop=editcount&ususers=" + user,
                    dataType: "json",
                    success: function(data) {
                        var info = data.query.users[0];
                        var editcount = info.editcount;
                        var name = info.name;
                        $("#contentSub").html($("#contentSub").html() + "<br /><strong>" + name + " has " + editcount + " total edits.</strong>");
                    }
                });
            },

            editCount: function() { 
                var subpagename = mw.config.get("wgTitle").split("/")[1],
                    countuser;
                if ((mw.config.get("wgNamespaceNumber") === 2 || mw.config.get("wgNamespaceNumber") === 3) && !subpagename) { 
                    countuser = mw.config.get("wgTitle").split(":")[0];
                } else if (mw.config.get("wgNamespaceNumber") === -1 && mw.config.get("wgTitle").indexOf("Contributions") !== -1) {
                    if (!document.getElementById("contentSub"))
                        return;
                    countuser = mw.config.get("wgTitle").slice(14);
                }
                this.retrieveData(countuser);
            }
        }

        var subpagename = mw.config.get("wgTitle").split("/")[1];

        if (((mw.config.get("wgNamespaceNumber") === 2 || mw.config.get("wgNamespaceNumber") === 3)
            && subpagename === undefined && mw.config.get("wgAction") !== "history")
            || mw.config.get("wgCanonicalSpecialPageName") === "Contributions") { 
            var portletLink = mw.util.addPortletLink("p-cactions", "#", "Count", "ca-editcount", "Quick edit count of user");
            $(portletLink).click(function(e) {
                e.preventDefault();
                quickEditCount.editCount();
            });
        }
    });
}(jQuery)); // End wrap with jQuery

// [[Category:Wikipedia scripts]]