MediaWiki:Gadget-catfix.js
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Internet Explorer / Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
- Opera:按 Ctrl-F5。
- 此腳本缺少說明文檔。請協助創建。
- 該腳本/樣式清單是小工具
catfix
的一部分(編輯定義)- 描述(編輯):处理如Category:英语词元等特定语言的分类中的词条链接,赋予其该语言特定的样式并将其指向页面内该语言对应的章节。
- 相關-{zh:連結;zh-hans:链接;zh-hant:連結;}-:子頁面列表 • -{zh-hans:链入; zh-hant:連入}- • -{zh-hans:重定向; zh-hant:重新導向}-
/*
* dependencies: mediawiki.Title
*/
jQuery(function () {
'use strict';
var wrapper;
// Apply only to pages in the Category namespace
// containing an element with the id "catfix".
// Set window.disableCatfix to true to prevent this script from running.
if (!(!window.disableCatfix
&& mw.config.get('wgNamespaceNumber') === 14
&& (wrapper = document.getElementById("catfix"))))
return;
// Get the language name and script wrapper.
var langName = wrapper.className.split("CATFIX-")[1];
wrapper = wrapper.getElementsByTagName("*")[0] || document.createElement("span");
var anchor = "";
if (langName && langName.length > 0)
anchor = langName;
function isEntry(namespaceName, pageName) {
// main, Talk, Citations, Reconstruction,
// Appendix if it starts with language name and "/"
return ["", "Talk", "Citations", "Reconstruction"].indexOf(namespaceName) != -1
|| (namespaceName == "Appendix"
&& pageName.slice(0, langName.length + 1) == langName + "/");
}
var formattedNamespaces = mw.config.get("wgFormattedNamespaces");
function wrapNode(node, wrapper) {
var parent = node.parentNode;
wrapper.appendChild(node);
parent.appendChild(wrapper);
}
// Process each link in the category listing.
jQuery("#mw-pages > .mw-content-ltr li > a, #newest-and-oldest-pages tr li > a")
.each(function () {
try {
var titleobj = new mw.Title(this.textContent || this.innerText);
var namespaceName = formattedNamespaces[titleobj.getNamespaceId()];
var pageName = titleobj.getMainText();
if (!isEntry(namespaceName, pageName))
return;
var textNodeToWrap;
// Choose the part of the link text to wrap
// - in mainspace, the whole link
// - in Talk and Citations, the part after the namespace prefix
// - in Reconstruction and Appendix, the part after the
// namespace prefix and language name
// Set window.catfixReconstructedAsterisk to true
// to replace "Reconstruction:langname/" with "*".
if (namespaceName === "") {
textNodeToWrap = this;
// Add the anchor in mainspace, not Reconstruction or Appendix,
// to match linking templates
// ([[Wiktionary:Grease pit/2019/December#Template:catfix shouldn't add an anchor to Reconstruction pages]]).
this.hash = anchor;
} else {
if (["Talk", "Citations"].indexOf(namespaceName) !== -1) {
textNodeToWrap = document.createTextNode(pageName);
$(this).empty()
.append(titleobj.getNamespacePrefix())
.append(textNodeToWrap);
} else if (["Reconstruction", "Appendix"].indexOf(namespaceName) !== -1) {
var split = pageName.split("/", 2);
if (split.length !== 2) {
throw new TypeError("Malformed title: " + pageName);
}
var langPrefix = split[0];
var prefix = window.catfixReconstructedAsterisk
? "" : titleobj.getNamespacePrefix() + langPrefix + "/";
textNodeToWrap = document.createTextNode(
(window.catfixReconstructedAsterisk ? "*" : "")
+ split[1]);
$(this).empty()
.append(prefix)
.append(textNodeToWrap);
}
}
wrapNode(textNodeToWrap, wrapper.cloneNode(false));
} catch (e) {
console.error(e);
}
});
});