User:Interiot/js/RealTitle.js

This is an old revision of this page, as edited by Interiot (talk | contribs) at 03:44, 21 November 2006. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// The function looks for a banner like that:
// <div id="RealTitleBanner">Div that is hidden
//   <span id="RealTitle">title</span>
// </div>
// An element with id=DisableRealTitle disables the function.
addOnloadHook(function() {
	//try {
		var realTitleBanner = document.getElementById("RealTitleBanner");
		if (realTitleBanner && !document.getElementById("DisableRealTitle")) {
			var realTitle = document.getElementById("RealTitle");
			if (realTitle) {
				realTitle = realTitle.innerHTML.toString();		// TODO: replace this with something like "realText"
				var isPasteable = 0;
				
				// calculate whether the title is pasteable
				var verifyTitle = realTitle.replace(/^ +/, "");		// trim left spaces
				verifyTitle = verifyTitle.replace(/ /g, "_");		// spaces to underscores
				verifyTitle = verifyTitle.charAt(0).toUpperCase() + verifyTitle.substring(1, verifyTitle.length);	// uppercase first character
				alert("1. '" + verifyTitle + "'");

				// if the namespace prefix is there, remove it on our verification copy.  If it isn't there, add it to the original realValue copy.
				if (wgNamespaceNumber != 0) {
					if (wgCanonicalNamespace == verifyTitle.substr(0, wgCanonicalNamespace.length) && verifyTitle.charAt(wgCanonicalNamespace.length) == ":") {
						verifyTitle = verifyTitle.substr(wgCanonicalNamespace.length + 1);
					} else {
						realTitle = wgCanonicalNamespace.replace(/_/g, " ") + ":" + realTitle;
					}
				}
				alert("2a. '" + verifyTitle + "'");
				alert("2b. '" + realTitle + "'");

				// verify whether wgTitle matches
				verifyTitle = verifyTitle.replace(/^_+/, "").replace(/_+$/, "");		// trim left and right spaces
				verifyTitle = verifyTitle.replace(/_/g, " ");		// underscores to spaces
				verifyTitle = verifyTitle.charAt(0).toUpperCase() + verifyTitle.substring(1, verifyTitle.length);	// uppercase first character
				isPasteable = (verifyTitle == wgTitle);
				alert("3. '" + verifyTitle + "' == '" + wgTitle + "' => " + isPasteable);

				var h1 = document.getElementsByTagName("h1")[0];
				if (h1 && isPasteable) {
					h1.innerHTML = realTitle;
					realTitleBanner.style.display = "none";
				}
				document.title = realTitle + " - Wikipedia, the free encyclopedia";
			}
		}
	//} catch (e) {
		/* Something went wrong. */
	//}
});