User:Bequw/bc-ad.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.


// <nowiki>
// Switches the year-numbering system from BCE/CE to BC/AD
// Now available as a gadget: [[MediaWiki:Gadget-BC-AD.js]]
/* globals addOnloadHook */
"use strict";

function yearFormatSwitch() {
	if (!document.querySelectorAll) return;
	
	// This code supports date labels formatted using the following wikitext. (See {{B.C.E}} and {{C.E.}}.) BCE can be replaced by CE:
	
	// <small class="ce-date">[[Appendix:Glossary#BCE|<span title="Glossary and display preference">BCE</span>]]</small>
	var datetags = document.querySelectorAll('.ce-date');
	for (var i = 0; i < datetags.length; i++) {
		var datetag = datetags[i];
		if (datetag.firstChild && datetag.firstChild.firstChild && datetag.firstChild.firstChild.firstChild) {
			if (datetag.firstChild.firstChild.firstChild.nodeValue === "CE") {
				if (typeof datetag.firstChild.setAttribute === "function") {
					datetag.firstChild.setAttribute("href", "/wiki/Appendix:Glossary#AD");
				}
				datetag.firstChild.firstChild.innerHTML = "AD";
				if (datetag.previousSibling) {
					var patt = /((?:\d{1,4} ?(?:-|–|to) ?)?\d{1,4}) ?$/g; // first if hyphen-minus, then en-dash
					var val = datetag.previousSibling.nodeValue;
					var matches = val.match(patt);
					if (matches !== null) {
						var lastMatch = matches[matches.length - 1];
						datetag.previousSibling.nodeValue = datetag.previousSibling.nodeValue.substr(0, datetag.previousSibling.nodeValue.length - lastMatch.length);
						if(!datetag.nextSibling)
							datetag.parentElement.appendChild(document.createTextNode(" " + lastMatch));
						else
							datetag.nextSibling.nodeValue = " " + lastMatch + datetag.nextSibling.nodeValue;
					}
				}
			}
			else {
				if (typeof datetag.firstChild.setAttribute === "function") {
					datetag.firstChild.setAttribute("href", "/wiki/Appendix:Glossary#BC");
				}
				datetag.firstChild.firstChild.innerHTML = "BC";
			}
		}
	}
	
	// This code supports dates formatted in the following fashion:
	
	// <small class="ce-date2">CE</small>
	
	// The tag <small> can be replaced with <span> or something else.
	var datetags2 = document.querySelectorAll(".ce-date2");
	for (i = 0; i < datetags2.length; ++i ) {
		var datetag2 = datetags2[i];
		var convert = { "BCE": "BC", "CE": "AD" };
		datetag2.innerHTML = convert[datetag2.innerHTML];
	}
}

$(yearFormatSwitch);

// </nowiki>