ବ୍ୟବହାରକାରୀ:Ansumang/metadata/assesslinks.js

ସୂଚନା: ବଦଳଗୁଡ଼ିକ ଦେଖିବା ପାଇଁ ଆପଣଙ୍କୁ ହୁଏତ ନିଜ ବ୍ରାଉଜର କ୍ୟାସ ବାଇପାସ କରିବାକୁ ପଡ଼ିପାରେ ।

  • Firefox / Safari: Reload ଉପରେ କ୍ଲିକ କରିବା ବେଳେ Shift ଧରି କିମ୍ବା Ctrl-F5 ବା Ctrl-R ଦବାନ୍ତୁ (Macରେ ⌘-R)
  • Google Chrome: Ctrl-Shift-R ଦବାନ୍ତୁ (Macରେ ⌘-Shift-R)
  • Internet Explorer / Edge: Refresh ଉପରେ କ୍ଲିକ କଲା ବେଳେ Ctrl ଧରି ବା Ctrl-F5 ଦବାଇ
  • Opera: Ctrl-F5 ଦବାନ୍ତୁ ।
/**
 * Optional component for metadata script ([[User:ansumang/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.
 */

assessment.links = {
    /**
     * Add a link to the toolbox in the sidebar. Clicking the link runs the
     * script.
     */
    addToolboxLink: function addToolboxLink () {
        var desc = 'Find the assessment of all articles linked and color the links';
        addPortletLink('p-tb', 'javascript:assessment.links.assessLinks()',
            'Assess links', 't-assess-article-links', desc);
    },
    /**
     * Find the assessment data for all links on the page, and color each
     * link accordingly. Requests are only sent one at a time.
     */
    assessLinks: function assessLinks () {
        // Script-specific CSS rules
        importStylesheet('User:Pyrospirit/metadata/assesslinks.css');

        this.linkList = document.getElementById('bodyContent').getElementsByTagName('a');
        // Start the first request
        this.assessSingleLink(0);
    },
    /**
     * Assess the link with the given index from the link list.
     * @param {Number} linkIndex - the index of the link element to be assessed
     */
    assessSingleLink: function assessSingleLink (linkIndex) {
        if (linkIndex >= this.linkList.length)
            return;
        var el = this.linkList[linkIndex],
            url = this.getRequestLink(el.href),
            that = this;
        if (url) {
            assessment.ajaxMain(url, function () {
                that.modifyLink.apply(that, arguments);
            }, linkIndex);
        }
        else {
            // Continue to next link
            this.assessSingleLink(linkIndex + 1);
        }
    },
    /**
     * Given the href attribute of a link, finds and returns the talk page URL
     * associated with it. Returns nothing for external or non-mainspace links.
     * @param {String} href - the href attribute of the element to be assessed
     * @return {String} url - the URL containing the assessment data
     */
    getRequestLink: function getRequestLink (href) {
        var localUrl = wgServer + wgArticlePath.replace('$1', ''),
            url;
        if (href.replace(/#.*/, '') != document.location.href
                && RegExp('^' + localUrl).test(href)) {
            if (!/^[a-z]+([_ ]talk)?:[^_ ]/i.test(href.replace(localUrl, ''))) {
                url = href.replace('?', '&').replace('\/wiki\/', '\/w\/index.php?title=Talk:')
                    + '&action=raw&section=0';
            }
            else if (/^Talk:[^_ ]/i.test(href.replace(localUrl, ''))) {
                url = href.replace('?', '&').replace('\/wiki\/', '\/w\/index.php?title=')
                    + '&action=raw&section=0';
            }
            return url;
        }
    },
    /**
     * The callback function for requests that are sent out. When the request
     * is ready, parses the assessment data, colors the link, and starts the
     * next request.
     * @param {Object} request - an AJAX GET request containing assessment data
     * @param {Number} index - the index of the element being assessed
     */
    modifyLink: function modifyLink (request, index) {
        if (request.readyState == 4) {
            var assess = {rating: 'none', pageLink: [null, null], extra: [], activeReview: null};
            // Only do stuff with the request if it has a 200 status code
            if (request.status == 200) {
                assess.rating = assessment.getRating(request.responseText);
            }
            var newClass = assessment.talkAssess(assess).newClass;
            this.linkList[index].className += ' assess-wikilink ' + newClass;

            // Start next request
            this.assessSingleLink(index + 1);
        }
    }
};

// Add the toolbox link when the page loads
addOnloadHook(function () {
    assessment.links.addToolboxLink.call(assessment.links);
});