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

Content deleted Content added
+improved comment
m Remove legacy globals per phab:T72470 (via WP:JWB)
 
(22 intermediate revisions by one other user not shown)
Line 1:
/** "Technical restrictions" title fix *****************************************
// The function looks for a banner like that:
*
// <div id="RealTitleBanner">Div that is hidden
* Description:
// <span id="RealTitle">title</span>
* Maintainers: [[User:Interiot]], [[User:Mets501]]
// </div>
*/
// An element with id=DisableRealTitle disables the function.
//
// TheFor <h1>pages isthat changedhave ONLYsomething IFlike theTemplate:Lowercase, proposedreplace the title, isbut aonly if it is copycut-nand-pasteable versionas ofa thevalid underlying titlewikilink.
// (for instance [[JiPod]]'s title is updated. <nowiki>But [[C#]] is not aan usefulequivalent wikilink, so on [[JC Sharp]], thedoesn't <h1>have titleits ismain nottitle changed)</nowiki>
//
addOnloadHook(function() {
// The function looks for a banner like this: <nowiki>
try {
// <div id="RealTitleBanner"> <!-- div that gets hidden -->
var realTitleBanner = document.getElementById("RealTitleBanner");
// <span id="RealTitle">title</span>
if (realTitleBanner && !document.getElementById("DisableRealTitle")) {
// </div>
var realTitle = document.getElementById("RealTitle");
// </nowiki>An element with id=DisableRealTitle disables the function.
if (realTitle) {
var disableRealTitle = 0; // users can disable this by making this true from their monobook.js
realTitle = realTitle.innerHTML; // TODO: replace this with something like "realText"
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)
var isPasteable = 0;
addOnloadHook(function() {
try {
// calculate whether the title is pasteable
var realTitleBanner = document.getElementById("RealTitleBanner");
var verifyTitle = realTitle.replace(/^ +/, ""); // trim left spaces
if (realTitleBanner && !document.getElementById("DisableRealTitle")) {
verifyTitle = verifyTitle.charAt(0).toUpperCase() + verifyTitle.substring(1, verifyTitle.length); // uppercase first character
var realTitle = document.getElementById("RealTitle");
 
if (realTitle) {
// if the namespace prefix is there, remove it on our verification copy. If it isn't there, add it to the original realValue copy.
var realTitleHTML = realTitle.innerHTML;
if (wgNamespaceNumber != 0) {
realTitleText = pickUpText(realTitle);
if (wgCanonicalNamespace == verifyTitle.substr(0, wgCanonicalNamespace.length).replace(/ /g, "_") && verifyTitle.charAt(wgCanonicalNamespace.length) == ":") {
verifyTitle = verifyTitle.substr(wgCanonicalNamespace.length + 1);
}var isPasteable else= {0;
//var containsHTML = /</.test(realTitleHTML); // contains ANY HTML
realTitle = wgCanonicalNamespace.replace(/_/g, " ") + ":" + realTitle;
var containsTooMuchHTML = /</.test( realTitleHTML.replace(/<\/?(sub|sup|small|big)>/gi, "") ); // contains HTML that will be ignored when cut-n-pasted as a wikilink
}
// calculate whether the title is pasteable
}
var verifyTitle = realTitleText.replace(/^ +/, ""); // trim left spaces
 
verifyTitle = verifyTitle.charAt(0).toUpperCase() + verifyTitle.substring(1, verifyTitle.length); // uppercase first character
// verify whether wgTitle matches
verifyTitle = verifyTitle.replace(/^ +/, "").replace(/ +$/, ""); // trim left and right spaces
// if the namespace prefix is there, remove it on our verification copy. If it isn't there, add it to the original realValue copy.
verifyTitle = verifyTitle.replace(/_/g, " "); // underscores to spaces
if (mw.config.get('wgNamespaceNumber') != 0) {
verifyTitle = verifyTitle.charAt(0).toUpperCase() + verifyTitle.substring(1, verifyTitle.length); // uppercase first character
if (mw.config.get('wgCanonicalNamespace') == verifyTitle.substr(0, mw.config.get('wgCanonicalNamespace').length).replace(/ /g, "_") && verifyTitle.charAt(mw.config.get('wgCanonicalNamespace').length) == ":") {
isPasteable = (verifyTitle == wgTitle);
verifyTitle = verifyTitle.substr(mw.config.get('wgCanonicalNamespace').length + 1);
 
} else {
var h1 = document.getElementsByTagName("h1")[0];
realTitleText = mw.config.get('wgCanonicalNamespace').replace(/_/g, " ") + ":" + realTitleText;
if (h1 && isPasteable) {
realTitleHTML = mw.config.get('wgCanonicalNamespace').replace(/_/g, " ") + ":" + realTitleHTML;
h1.innerHTML = realTitle;
}
realTitleBanner.style.display = "none";
}
document.title = realTitle + " - Wikipedia, the free encyclopedia";
// verify whether wgTitle matches
}
verifyTitle = verifyTitle.replace(/^ +/, "").replace(/ +$/, ""); // trim left and right spaces
}
verifyTitle = verifyTitle.replace(/_/g, " "); // underscores to spaces
} catch (e) {
verifyTitle = verifyTitle.charAt(0).toUpperCase() + verifyTitle.substring(1, verifyTitle.length); // uppercase first character
/* Something went wrong. */
isPasteable = (verifyTitle == mw.config.get('wgTitle'));
}
});
var h1 = document.getElementsByTagName("h1")[0];
if (h1 && isPasteable) {
h1.innerHTML = containsTooMuchHTML ? realTitleText : realTitleHTML;
if (!containsTooMuchHTML)
realTitleBanner.style.display = "none";
}
document.title = realTitleText + " - Wikipedia, the free encyclopedia";
}
}
} catch (e) {
/* Something went wrong. */
}
});
}