User:Interiot/js/RealTitle.js: Difference between revisions

Content deleted Content added
merge from version posted to MediaWiki:Common.js
m Remove legacy globals per phab:T72470 (via WP:JWB)
 
(16 intermediate revisions by one other user not shown)
Line 1:
/** "Technical restrictions" title fix *****************************************
//============================================================
*
// "Technical restrictions" title fix
* Description:
//============================================================
//******* * written byMaintainers: [[User:Interiot]], ********[[User:Mets501]]
*/
// For pages that have something like Template:Lowercase, replace the title, but only if it is cut-and-pasteable as a valid wikilink.
// (for instance [[iPod]]'s title is updated. But [[C#]] is not an equivalent wikilink, so [[C Sharp]] doesn't have its main title changed)
// For pages that have something like Template:Lowercase, replace the title, but only if it is cut-and-pasteable as a valid wikilink.
//
// (for instance [[iPod]]'s title is updated. <nowiki>But [[C#]] is not an equivalent wikilink, so [[C Sharp]] doesn't have its main title changed)</nowiki>
// The function looks for a banner like this:
//
// <div id="RealTitleBanner"> <!-- div that gets hidden -->
// The function looks for a banner like this: <nowiki>
// <span id="RealTitle">title</span>
// <div id="RealTitleBanner"> <!-- div that gets hidden -->
// </div>
// <span id="RealTitle">title</span>
// An element with id=DisableRealTitle disables the function.
// </div>
addOnloadHook(function() {
// </nowiki>An element with id=DisableRealTitle disables the function.
try {
var disableRealTitle = 0; // users can disable this by making this true from their monobook.js
var realTitleBanner = document.getElementById("RealTitleBanner");
if (mw.config.get('wgIsArticle')) { // don't display the RealTitle when editing, since it is apparently inconsistent (doesn't show when editing sections, doesn't show when not previewing)
if (realTitleBanner && !document.getElementById("DisableRealTitle")) {
addOnloadHook(function() {
var realTitle = document.getElementById("RealTitle");
try {
if (realTitle) {
var realTitleBanner = document.getElementById("RealTitleBanner");
var isPasteable = 0;
if (realTitleBanner && !document.getElementById("DisableRealTitle")) {
var containsHTML = /</.test(realTitle.innerHTML);
var realTitle = document.getElementById("RealTitle");
 
if (realTitle) {
realTitle = pickUpText(realTitle); // TODO: replace this with something like "realText"
var realTitleHTML = realTitle.innerHTML;
realTitleText = pickUpText(realTitle);
// calculate whether the title is pasteable
var verifyTitle = realTitle.replace(/^ +/, ""); // trim left spaces
var isPasteable = 0;
verifyTitle = verifyTitle.charAt(0).toUpperCase() + verifyTitle.substring(1, verifyTitle.length); // uppercase first character
//var containsHTML = /</.test(realTitleHTML); // contains ANY HTML
 
var containsTooMuchHTML = /</.test( realTitleHTML.replace(/<\/?(sub|sup|small|big)>/gi, "") ); // contains HTML that will be ignored when cut-n-pasted as a wikilink
// if the namespace prefix is there, remove it on our verification copy. If it isn't there, add it to the original realValue copy.
// calculate whether the title is pasteable
if (wgNamespaceNumber != 0) {
var verifyTitle = realTitleText.replace(/^ +/, ""); // trim left spaces
if (wgCanonicalNamespace == verifyTitle.substr(0, wgCanonicalNamespace.length).replace(/ /g, "_") && verifyTitle.charAt(wgCanonicalNamespace.length) == ":") {
verifyTitle = verifyTitle.substrcharAt(wgCanonicalNamespace0).lengthtoUpperCase() + verifyTitle.substring(1, verifyTitle.length); // uppercase first character
} else {
// if the namespace prefix is there, remove it on our verification copy. If it isn't there, add it to the original realValue copy.
realTitle = wgCanonicalNamespace.replace(/_/g, " ") + ":" + realTitle;
if (mw.config.get('wgNamespaceNumber') != 0) {
}
if (mw.config.get('wgCanonicalNamespace') == verifyTitle.substr(0, mw.config.get('wgCanonicalNamespace').length).replace(/ /g, "_") && verifyTitle.charAt(mw.config.get('wgCanonicalNamespace').length) == ":") {
}
verifyTitle = verifyTitle.substr(mw.config.get('wgCanonicalNamespace').length + 1);
 
} else {
// verify whether wgTitle matches
verifyTitle realTitleText = verifyTitlemw.config.get('wgCanonicalNamespace').replace(/^ +_/g, " ").replace(/ +$/, ":"); // trim+ left and right spacesrealTitleText;
verifyTitle realTitleHTML = verifyTitlemw.config.get('wgCanonicalNamespace').replace(/_/g, " "); // underscores+ to":" spaces+ realTitleHTML;
}
verifyTitle = verifyTitle.charAt(0).toUpperCase() + verifyTitle.substring(1, verifyTitle.length); // uppercase first character
}
isPasteable = (verifyTitle == wgTitle);
 
// verify whether wgTitle matches
var h1 = document.getElementsByTagName("h1")[0];
verifyTitle = verifyTitle.replace(/^ +/, "").replace(/ +$/, ""); // trim left and right spaces
if (h1 && isPasteable) {
verifyTitle = verifyTitle.replace(/_/g, " "); // underscores to spaces
h1.innerHTML = realTitle;
verifyTitle = verifyTitle.charAt(0).toUpperCase() + verifyTitle.substring(1, verifyTitle.length); // uppercase first character
if (!containsHTML)
isPasteable = (verifyTitle == mw.config.get('wgTitle'));
realTitleBanner.style.display = "none";
}
var h1 = document.getElementsByTagName("h1")[0];
document.title = realTitle + " - Wikipedia, the free encyclopedia";
if (h1 && isPasteable) {
}
h1.innerHTML = containsTooMuchHTML ? realTitleText : realTitleHTML;
}
if (!containsTooMuchHTML)
} catch (e) {
realTitleBanner.style.display = "none";
/* Something went wrong. */
}
}
document.title = realTitleText + " - Wikipedia, the free encyclopedia";
});
}
 
}
 
} catch (e) {
// similar to innerHTML, but only returns the text portions of the insides, excludes HTML
/* Something went wrong. */
function pickUpText(aParentElement) {
}
var str = "";
});
 
}
function pickUpTextInternal(aElement) {
var child = aElement.firstChild;
while (child) {
if (child.nodeType == 1) // ELEMENT_NODE
pickUpTextInternal(child);
else if (child.nodeType == 3) // TEXT_NODE
str += child.nodeValue;
 
child = child.nextSibling;
}
}
 
pickUpTextInternal(aParentElement);
 
return str;
}
// ^^^^ end "technical restrictions" title fix ^^^^
//============================================================