MediaWiki:Gadget-SimplifyRefNotesTag.js
外观
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5。
window.simplifyRefNotesTag = function() {
var i, j;
/**
* 本功能将数字按顺序转为英文字母(如 1->a,26->z,27->aa)
* @param {number} n - 需转换的数字(非负整数)
* @returns {string|null} 转换后的英文字母组合,输入非数字则返回null
*/
function num2alp(n) {
var chk = parseInt(n);
if (isNaN(chk)) return null;
var digit = new Array();
var result = "";
while (chk > 0) {
digit.unshift(chk % 26);
chk = Math.floor(chk / 26);
}
for (var j = digit.length; j-- > 0;) {
if (digit[j] <= 0) {
if (j > 0) {
digit[j] += 26;
digit[j - 1] -= 1;
} else {
break;
}
}
result = String.fromCharCode("a".charCodeAt(0) + (digit[j] - 1)) + result;
}
return result;
} // 功能结束
// 开始处理各类上标简化
var sups = document.getElementsByTagName("sup");
var sup_textnode;
var temp, num, alp;
// 从后往前遍历所有sup标签(避免DOM操作影响遍历顺序)
for (i = sups.length; i-- > 0;) {
// 筛选符合条件的引用上标:class为reference、id以cite_ref开头、结构为sup>a>文本
if (sups[i].className == "reference" && ("" + sups[i].id).indexOf("cite_ref") == 0)
if (sups[i].childNodes.length == 1)
if (("" + sups[i].childNodes[0].tagName).toLowerCase() == "a")
if (sups[i].childNodes[0].childNodes.length == 1)
if (("" + sups[i].childNodes[0].childNodes[0].nodeName) == "#text")
// 文本以[开头或首个字符为数字,且包含“参”或“注”字样(匹配简繁体)
if (sups[i].childNodes[0].childNodes[0].nodeValue.indexOf("[") == 0 || !isNaN(sups[i].childNodes[0].childNodes[0].nodeValue.charAt(0)))
if ((sups[i].childNodes[0].childNodes[0].nodeValue + sups[i].group_name).match(/[參参註注]/g)) {
sup_textnode = sups[i].childNodes[0].childNodes[0];
// 处理“参”类型引用(父容器id为refTag-cite_ref-sup)
if (sups[i].parentNode.id == "refTag-cite_ref-sup") {
// 分割并移除“参 ”字样(匹配简繁体)
temp = sup_textnode.nodeValue.split(/[參参] /g);
sup_textnode.nodeValue = temp.join("");
}
// 处理“注”类型引用(父容器id为noteTag-cite_ref-sup)
else if (sups[i].parentNode.id == "noteTag-cite_ref-sup") {
// 分割“注 ”字样(匹配简繁体)
temp = sup_textnode.nodeValue.split(/[註注] /g);
// 提取最后一段文本中的数字部分
for (j = temp[temp.length - 1].length; j-- > 0;) {
if (!isNaN(temp[temp.length - 1].charAt(j))) break;
}
num = parseInt(temp[temp.length - 1].substring(0, j + 1));
// 将数字转为英文字母,拼接剩余部分
temp[temp.length - 1] = num2alp(num) + temp[temp.length - 1].substring(j + 1);
sup_textnode.nodeValue = temp.join("");
}
}
}
// 上标简化处理结束
// 将备注列表项目用英文字母排序(type="a")
if (document.getElementById("references-NoteFoot")) {
var ol = document.getElementById("references-NoteFoot").getElementsByTagName("ol");
for (i = ol.length; i-- > 0;) {
if (ol[i].className == "references") ol[i].type = "a";
}
}
// 函数自销毁,避免重复执行
window.simplifyRefNotesTag = function() {};
};
window.load_Merge_Simplify = function() {
// 若存在mergeRefBracket函数则执行(外部依赖函数)
if (("" + (typeof window.mergeRefBracket)).toLowerCase() == "function") window.mergeRefBracket();
// 执行上标简化函数
if (("" + (typeof window.simplifyRefNotesTag)).toLowerCase() == "function") window.simplifyRefNotesTag();
// 函数自销毁,避免重复执行
window.load_Merge_Simplify = function() {};
};
// 页面DOM加载完成后执行初始化函数(jQuery语法)
$(function() {
window.load_Merge_Simplify();
});