User:P1ayer/CatNavStr.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
addEditButton('btnGetCatnav',{
src : "f/fe/Button_refs.png",
tagOpen : '',
sampleText : '',
tagClose : '',
speedTip : "GetCatnav",
action : GetCatnav
});
function GetCatnav()
{
//參考: User:Liangent/Scripts/CatNav.js
jQuery(function() {
// ns:14 is category. avoid hard-coding?
if (wgNamespaceNumber != 14 || wgArticleId == 0 || (wgAction != 'view' && wgAction != 'purge')) return;
window.CatNav = {};
CatNav.pageName = wgPageName.replace(/_/g, ' ');
CatNav.data = {};
CatNav.walked = {};
CatNav.path = [];
CatNav.pending = 0;
CatNav.categoryPrefix = new RegExp('^' + wgFormattedNamespaces[14] + ':');
CatNav.titlesLimit = ((wgUserGroups.indexOf('sysop') != -1 || wgUserGroups.indexOf('bot') != -1) ? 500 : 50);
CatNav.cllimitLimit = ((wgUserGroups.indexOf('sysop') != -1 || wgUserGroups.indexOf('bot') != -1) ? 5000 : 500);
CatNav.callback = 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([data.requestid], data['query-continue'].categories.clcontinue);
}
CatNav.query(toquery);
if (CatNav.pending == 0) {
CatNav.walk(CatNav.pageName);
}
};
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.
jQuery.getScript(wgScriptPath + '/api.php?action=query&format=json&callback=CatNav.callback&titles='
+ encodeURIComponent(titles) + '&prop=categories&clshow=!hidden&cllimit=' + CatNav.cllimitLimit + '&requestid='
+ encodeURIComponent(titles) + (clcontinue ? ('&clcontinue=' + encodeURIComponent(clcontinue)) : '')
);
}
};
CatNav.walk = function(page) {
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 sb = new StringBuilder();
var isfirst = true;
jQuery.each(CatNav.path, function() {
if (!isfirst) {
//div.prepend(' > ');
sb.append('|');
} else {
isfirst = false;
sb.append("{{Catnav|");
}
// div.prepend(jQuery('<a />').attr(
// 'href', encodeURIComponent(this).replace(/(.*)/, wgArticlePath)
// ).text(this.replace(CatNav.categoryPrefix, '')));
sb.append(this.replace(CatNav.categoryPrefix, ''));
});
if (loop) {
//div.prepend('[...] > ');
sb.append('...|');
}
//div.insertBefore('.printfooter');
sb.append("}}\n");
//
with($('#wpTextbox1'))
{
sb.append(val());
val(sb.toString());
}
};
CatNav.data[CatNav.pageName] = {};
CatNav.query([CatNav.pageName]);
});
}
if (typeof(StringBuilder) == "undefined")
{
//-----------------------------------------
// StringBuilder(JavaScript版)
// 原始出處
// StringBuilder: http://www.codeproject.com/jscript/stringbuilder.asp
// sprintf: http://jan.moesen.nu/code/javascript/sprintf-and-printf-in-javascript/
// 改良版
// http://www.player.idv.tw/prog/index.php?title=StringBuilder.js
function StringBuilder(value)
{
this.strings = new Array("");
this.append(value);
}
// Appends the given value to the end of this instance.
StringBuilder.prototype.append = function (value)
{
if (value)
{
this.strings.push(value);
}
return this;
}
// Appends the given format value to the end of this instance.
StringBuilder.prototype.appendFormat = function ()
{
var arguments = StringBuilder.prototype.appendFormat.arguments;
if (arguments.length > 0)
{
var value = arguments[0];
for (var i = 1; i < arguments.length; ++i)
{
value = value.replace("{"+(i-1)+"}", arguments[i]);
}
this.strings.push(value);
}
return this;
}
// Clears the string buffer
StringBuilder.prototype.clear = function ()
{
this.strings.length = 1;
}
// Converts this instance to a String.
StringBuilder.prototype.toString = function ()
{
return this.strings.join("");
}
//appendPrintf
StringBuilder.prototype.appendPrintf = function ()
{
var arguments = StringBuilder.prototype.appendPrintf.arguments;
if (!arguments || arguments.length < 1 || !RegExp)
{
Error('Error at StringBuilder.prototype.appendPrintf');
return this;
}
var str = arguments[0];
var formats = {
'%': function(val) {return '%';},
'b': function(val) {return parseInt(val, 10).toString(2);},
'c': function(val) {return String.fromCharCode(parseInt(val, 10));},
'd': function(val) {return parseInt(val, 10) ? parseInt(val, 10) : 0;},
'u': function(val) {return Math.abs(val);},
'f': function(val, p) {return (p > -1) ? Math.round(parseFloat(val) * Math.pow(10, p)) / Math.pow(10, p): parseFloat(val);},
'o': function(val) {return parseInt(val, 10).toString(8);},
's': function(val) {return val;},
'x': function(val) {return ('' + parseInt(val, 10).toString(16)).toLowerCase();},
'X': function(val) {return ('' + parseInt(val, 10).toString(16)).toUpperCase();}
};
var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
var a = [], numSubstitutions = 0;
while (a = re.exec(str))
{
var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
var pPrecision = a[5], pType = a[6], rightPart = a[7];
numSubstitutions++;
if (numSubstitutions >= arguments.length)
{
Error('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
return this;
}
var param = arguments[numSubstitutions];
var subst = param;
var formatsFunc = formats[pType];
if (formatsFunc != null)
{
if (pType == 'f')
{
var precision = (pPrecision && pType == 'f') ? parseInt(pPrecision.substring(1)) : -1;
subst = formatsFunc(param, precision);
} else {
subst = formatsFunc(param);
}
}
if (leftpart)
{
this.strings.push(leftpart);
}
if (subst)
{
this.strings.push(subst);
}
str = rightPart;
}
if (str)
{
this.strings.push(str);
}
return this;
}
}