User:Liangent/Scripts/CatNav.js

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
jQuery(function() {
    // ns:14 is category. avoid hard-coding?
    if (mw.config.get('wgNamespaceNumber') != 14 || mw.config.get('wgArticleId') == 0 || (mw.config.get('wgAction') != 'view' && mw.config.get('wgAction') != 'purge')) return;
    var CatNav = {};
    CatNav.depthLimit = 8;
    CatNav.pageName = mw.config.get('wgPageName').replace(/_/g, ' ');
    CatNav.data = {};
    CatNav.walked = {};
    CatNav.path = [];
    CatNav.pending = 0;
    CatNav.categoryPrefix = new RegExp('^' + mw.config.get('wgFormattedNamespaces')[14] + ':');
    CatNav.titlesLimit = ((mw.config.get('wgUserGroups').indexOf('sysop') != -1 || mw.config.get('wgUserGroups').indexOf('bot') != -1) ? 500 : 50);
    CatNav.cllimitLimit = ((mw.config.get('wgUserGroups').indexOf('sysop') != -1 || mw.config.get('wgUserGroups').indexOf('bot') != -1) ? 5000 : 500);
    CatNav.query = function(pages, clcontinue) {
        if (pages.length > CatNav.titlesLimit) {
            CatNav.query(pages.slice(0, CatNav.titlesLimit));
            CatNav.query(pages.slice(CatNav.titlesLimit));
        } else if (pages.length != 0) {
            var titles = pages.join('|');
            CatNav.pending++;
            // since this will be repeated on every category page, i'd better filter hidden categories out
            // rather than know which are hidden and hide them.
            var data = {
                action: 'query',
                format: 'json',
                titles: titles,
                prop: 'categories',
                clshow: '!hidden',
                cllimit: CatNav.cllimitLimit
            };
            if (clcontinue) {
                data.clcontinue = clcontinue;
            }
            jQuery.post(mw.config.get('wgScriptPath') + '/api.php', data, function(data) {
                CatNav.pending--;
                var toquery = [];
                for (var pageid in data.query.pages) {
                    var page = data.query.pages[pageid];
                    var pagename = page.title;
                    jQuery.each(page.categories ? page.categories : [], function() {
                        CatNav.data[pagename][this.title] = true;
                        if (!CatNav.data[this.title]) {
                            CatNav.data[this.title] = {};
                            toquery.push(this.title);
                        }
                    });
                }
                if (data['query-continue']) {
                    CatNav.query([titles], data['query-continue'].categories.clcontinue);
                }
                CatNav.query(toquery);
                if (CatNav.pending == 0) {
                    CatNav.walk(CatNav.pageName);
                }
            }, 'json');
        }
    };
    CatNav.walk = function(page) {
        if (CatNav.path.length == CatNav.depthLimit) return;
        if (CatNav.walked[page]) {
            CatNav.path.push(page);
            CatNav.display(true);
            CatNav.path.pop();
        } else {
            CatNav.walked[page] = true;
            CatNav.path.push(page);
            var output = true;
            for (var parent in CatNav.data[page]) {
                output = false;
                CatNav.walk(parent);
            }
            if (output) {
                CatNav.display();
            }
            CatNav.path.pop();
            CatNav.walked[page] = false;
        }
    };
    CatNav.display = function(loop) {
        var div = jQuery('<div />').addClass('catnav');
        var isfirst = true;
        jQuery.each(CatNav.path, function() {
            if (!isfirst) {
                div.prepend('&nbsp;&gt;&nbsp;');
            } else {
                isfirst = false;
            }
            div.prepend(jQuery('<a />').attr(
                'href', encodeURIComponent(this).replace(/(.*)/, mw.config.get('wgArticlePath'))
            ).text(this.replace(CatNav.categoryPrefix, '')));
        });
        if (loop) {
            div.prepend('[...]&nbsp;&gt;&nbsp;');
        }
        div.insertBefore('.printfooter');
    };
    CatNav.data[CatNav.pageName] = {};
    CatNav.query([CatNav.pageName]);
});