MediaWiki:Gadget-catfix.js

來自維基辭典

注意:喺發布之後,你可能要先略過你嘅瀏覽器快取去睇到更改。

  • Firefox / Safari:㩒住Shift掣再撳重新載入,又或者㩒Ctrl-F5或者Ctrl-R(喺Mac㩒Command-R掣)
  • Google Chrome:Ctrl-Shift-R(喺Mac㩒Command-R掣)
  • Internet Explorer / Edge:㩒住Ctrl掣再撳重新整理,又或者㩒Ctrl-F5
  • Opera:Ctrl-F5
/*
 * dependencies: mediawiki.Title
 */

jQuery(function () {
	'use strict';
	
	var wrapper;
	
	// Apply only to pages containing an element with the id "catfix".
	if (!(wrapper = document.getElementById("catfix")))
		return;
	
	// Apply only to Category: namespace
	if (mw.config.get('wgNamespaceNumber') !== 14)
		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;
	
	// 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 namespaceId = titleobj.getNamespaceId();
				var pageName = titleobj.getNameText();
				
				/*
				 * Continue if the link points to a page in the main or
				 * reconstruction namespaces or is an appendix page
				 * beginning with the language name (i.e. an entry for a
				 * term in an appendix-only language).
				 */
				if (!([0, 1, 114, 118].indexOf(namespaceId) != -1
					|| (namespaceId == 100
						&& pageName.substring(0, langname.length + 1) == langname + "/")))
					return;
				
				// Add the anchor if the link goes to a mainspace or reconstruction page.
				if (namespaceId === 0 || namespaceId === 118)
					this.setAttribute("href", this.getAttribute("href") + anchor);
				
				var textNodeToWrap;
				if (namespaceId === 1 || namespaceId === 114) { // talk, citations
					textNodeToWrap = document.createTextNode(pageName);
					$(this).empty()
						.append(titleobj.getNamespacePrefix())
						.append(textNodeToWrap);
				} else {
					textNodeToWrap = this;
				}
				
				// Insert the wrapper around the link.
				var parent = textNodeToWrap.parentNode;
				var clone = wrapper.cloneNode(false);
				clone.appendChild(textNodeToWrap);
				parent.appendChild(clone);
			} catch (e) {
				console.error(e);
			}
		});
});