Участник:Сидик из ПТУ/WhoDied.js

Материал из Википедии — свободной энциклопедии
Перейти к навигации Перейти к поиску
Страница персонального оформления. У этого JS-кода есть связанный CSS-файл: Участник:Сидик из ПТУ/WhoDied.css.
После сохранения очистите кэш браузера.
/*  This script will classify as many links as possible and assign CSS classes
    accordingly.  See the complete script documentation for usage and more details.

    Key acknowledgement goes to User:Anomie whose script (User:Anomie/linkclassifier.js)
    was used as an initial template structure to get up an running quickly with MW api. Go 
    check it out if you're interested in a lot more ways to classify and color various MW links.
*/

var AQFetcher={

    articleClasses: { 'A':[] },
 
    callback:function(r, sts, xhr){
        if(!r || !r.query) {
            if(typeof(window.console)=='undefined' || typeof(window.console.error)!='function')
                mw.log.error(new Error('Bad response'));
            window.console.error("Bad response", r);
            return;
        }
        if(r['query-continue']){
            var cc=this.rawdata;
            for(var k in r['query-continue']){
                for(var k2 in r['query-continue'][k]){
                    cc[k2]=r['query-continue'][k][k2];
                }
            }
            $.ajax({
                url:mw.util.wikiScript('api'),
                dataType:'json',
                type:'POST',
                data:cc,
                rawdata:cc,
                success:arguments.callee,
                error:function(xhr,textStatus,errorThrown){
                    mw.log.error(new Error('AJAX error: '+textStatus+' '+errorThrown));
                }
            });
        }
        r=r.query;

        var a=document.getElementById('wikiPreview');
        if(!a) a=document.getElementById('bodyContent');
        if(!a) {mw.log.error(new Error('Huh? No body content?')); return; }
        a=a.getElementsByTagName('A');
        if(a.length==0) return;

        // Handy shortcut
        var AQF = AQFetcher;

        if(r.pages) for(var i in r.pages){
            if(typeof(r.pages[i].categories)!='undefined'){
                
                // If the corresponding Talk: page has appropriately-named categories we can
                // match each article page to it's classification

                unClassified = true;
                iCat = 0;
                while (unClassified && iCat < r.pages[i].categories.length) {
                   if (r.pages[i].categories[iCat].title!='undefined') {
                      if (r.pages[i].categories[iCat].title.indexOf('Умершие') >= 0) {
                         for(var ri in r.redirects){
                            if (r.redirects[ri].to == r.pages[i].title) {
                               AQF.articleClasses['A'].push(r.redirects[ri].from);
                               AQF.articleClasses['A'].push(r.pages[i].title);
                               unClassified = false;
                               break;
                            }
                         }
                         if (unClassified) {
                            AQF.articleClasses['A'].push(r.pages[i].title);
                            unClassified = false;
                         }
                      }
                   }
                   iCat+=1;
                }
            }
        }

        Array.prototype.forEach.call(a, function(a){
            if(typeof(a.wikipage)=='undefined') return;
            if (AQF.articleClasses['A'].indexOf(a.wikipage) >= 0) {
                $(a).addClass('AQ-A');
            } else {
                //$(a).addClass('AQ-Unknown');  // Having some trouble with this at the moment.
            } 
        });
    },

    /* Extract the name of the WP article from the URL */
    getPageName:function(url){
        var m=url.match(/\/wiki\/([^?#]+)/);
        if(!m) m=url.match(/\/w\/index.php\?(?:.*&)?title=([^&#]+)/);
        if(!m) return '';
        var t=decodeURIComponent(m[1]).replace(/_/g,' ');
        if(t.substr(0,6)=='Image:') t='File:'+t.substr(6);
        if(t.substr(0,11)=='Image talk:') t='File talk:'+t.substr(6);
        if(t.substr(0,8)=='Special:') t='';
        return t;
    },

    classifyChildren:function(node){
        mw.loader.using(['mediawiki.util','mediawiki.user'], function(){
            var a=node.getElementsByTagName('A');
            if(a.length==0) return;
            var self=AQFetcher.getPageName(location.href);
            a=Array.prototype.map.call(a, function(a){
                a.wikipage='';
                if(/(^|\s)(external|extiw)(\s|$)/.test(a.className)) return '';
                a.wikipage=AQFetcher.getPageName(a.href);
                if(a.wikipage==self) a.wikipage='';
                if(a.wikipage.indexOf(':')>0) a.wikipage=''; //filter out anything not in main namespace
                a.origwikipage=a.wikipage;
                return a.wikipage; 
            }).sort().filter(function(e,i,a){
                return e!=='' && (i==0 || a[i-1]!==e) && e!=='Обсуждение:';
            });
            // a is now a sorted array of links with wikipage attributes holding the title of talk pages

            function processLinks(limit){
                while(a.length>0){
                    var q={
                        format:'json',
                        action:'query',
                        redirects:1,
                        titles:a.splice(0,limit).join('|'),
                        prop:'categories', // Only interested in Talk page categories
                        cllimit:'max',
                        rawcontinue:1
                    };
                    $.ajax({
                        url:mw.util.wikiScript('api'),
                        dataType:'json',
                        type:'POST',
                        data:q,
                        rawdata:q,
                        success:AQFetcher.callback,
                        error:function(xhr,textStatus,errorThrown){
                            mw.log.error(new Error('AJAX error: '+textStatus+' '+errorThrown));
                        }
                    });
                }
            }

            if(a.length<=100){
                // Not worth querying the API to see if the user has apihighlimits
                processLinks(50);
            } else {
                // Note mw.user.getRights queries the API
                mw.user.getRights(function(rights){
                    processLinks( (rights.indexOf('apihighlimits')>=0) ? 500 : 50 );
                });
            }  
        });
    },
 
    onLoad:function(){
        if (mw.config.get( 'wgCanonicalNamespace' ) != "") {
            console.log("Browsing in " + mw.config.get( 'wgCanonicalNamespace' )
               + " namespace so article link styles will not be applied by quality.");
            return;
        }

        // Figure out where the article content is...
        var node=document.getElementById('wikiPreview');
        if(!node) node=document.getElementById('bodyContent');

        if(node) AQFetcher.classifyChildren(node);
    } 
};

$(document).ready(AQFetcher.onLoad);