Jump to content

User:Nihiltres/assesslinks.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.
/**
 * Optional component for metadata script ([[MediaWiki:Gadget-metadata.js]]).
 * This script adds a link to the toolbox. When clicked, the script finds the
 * assessment of all articles linked from the current article and colors the
 * links accordingly.
 * @author Pyrospirit - Created the original script
 * @author Nihiltres - Rewrote it to match his update to the gadget
 */

mw.loader.using("ext.gadget.metadata", function () {

    assessment.links = (function () {
        var assessmentLinksObj = {},
            //internal shortcuts
            al = assessmentLinksObj,
            am = assessment.methods;

        /**
         * Assesses all the links on the page, making an AJAX query for each.
         */
        al.assessLinks = function () {
            var unassessedClass = "assess-wikilink " + am.renderAssessment({rating: "none"}).newClass,
                rawArticlePath = mw.config.get("wgArticlePath").replace("$1", "");

            $("#bodyContent a[href^=\"" + rawArticlePath + "\"]").each(function (i, e) {
                var url = $(e).attr("href").replace(/#.*/, ""); //Bypass section links
                if (url === "" || //empty string as the result of stripping out an internal section link
                        url === mw.config.get("wgPageName").replace(/(.*)/, mw.config.get("wgArticlePath")) || //Link to the current page
                        mw.Title.newFromText(decodeURIComponent(url.replace(rawArticlePath, ""))).namespace !== 0 //Link to non-mainspace page
                        ) {
                    return true; //go back to the loop
                }
                $.ajax({
                    url: url.replace(
                        rawArticlePath,
                        mw.config.get("wgScript") + "?title=Talk:"
                    ) + "&action=raw&section=0",
                    cache: true,
                    async: true,
                    dataType: "text",
                    success: function (responseText) {
                        $(e).addClass("assess-wikilink " + am.renderAssessment(am.getAssessment(responseText)).newClass);
                    },
                    error: function (jqxhr) {
                        if (jqxhr.status === 404) {
                            $(e).addClass(unassessedClass);
                        }
                    }
                });
            });
        };

        /**
         * Adds a link to activate the main assessLinks function.
         */
        al.addToolboxLink = function () {
            mw.util.addPortletLink(
                "p-tb",
                "#",
                "Assess links",
                "t-assess-article-links",
                "Find the assessment of all articles linked and colour the links"
            );
            $("#t-assess-article-links").click(assessment.links.assessLinks);
        };

        /**
         * Runs the part that adds the tab, and anything else necessary.
         * Slightly redundant, but consistent form is nice.
         */
        al.init = function () {
            al.addToolboxLink();
        };

        return assessmentLinksObj;
    }());

    //Initializes the script on page load.
    $(assessment.links.init);
    mw.loader.load("//en.wikipedia.org/w/index.php?title=User:Pyrospirit/metadata/assesslinks.css&action=raw&ctype=text/css", "text/css");

});