Difference between revisions of "MediaWiki:Common.js"

From The iPhone Wiki
Jump to: navigation, search
(attempting to fix the problem of clicking "Page" while on a talk archive)
m (minor changes)
Line 1: Line 1:
  +
/**
// @name fixTalkArchiveToc
 
  +
* The iPhone Wiki MediaWiki:Common.js
// @version 1.0
 
  +
* - JavaScript to fix common issues on The iPhone Wiki
// @description Removes the "1 This is an Archive Page" element from the TOC on Talk Archives
 
// @copyright (c) 2013, Cole Johnson <coleharrisjohnson@gmail.com>
+
* Copyright (c) 2013 Cole Johnson <coleharrisjohnson@gmail.com>
  +
**/
  +
  +
// Removes the "1 This is an Archive Page" element from the TOC on Talk Archives
 
function fixTalkArchiveToc() {
 
function fixTalkArchiveToc() {
// Is there a "This is an Archive Page" header block?
+
// Is there a "This is an Archive Page" header block?
 
if ($("table[id=archive-page-notice]").length != 0) {
 
if ($("table[id=archive-page-notice]").length != 0) {
 
var toc = $("table[id=toc]"); // Is there a TOC?
 
var toc = $("table[id=toc]"); // Is there a TOC?
if (toc.length != 0) {
+
if (toc.length != 0) {
// relocate to the children of the TOC table
+
// relocate to the children of the TOC table
toc = toc[0].children;
+
toc = toc[0].children;
// MediaWiki is consistent, so don't check length first
+
// MediaWiki is consistent, so no need to check length
if (toc[0].localName == "tbody") {
+
if (toc[0].localName == "tbody") {
toc = toc[0].children; // skip the tbody tag
+
toc = toc[0].children; // skip the tbody tag
  +
}
  +
// Nav to the "ul";
  +
toc = toc[0].children[0].children[1].children; // "ul" -> "{li}"
  +
  +
// remove "This is an Archive Page" entry
  +
toc[0].remove();
  +
  +
// Decrement the outermost section number for each TOC entry
  +
var sec = $("span[class=tocnumber]");
  +
var len = sec.length;
  +
var split;
  +
for (var i = 0; i < len; i++) {
  +
split = sec[i].innerText.split(".");
  +
split[0]--;
  +
sec[i].innerText = split.join(".");
 
}
 
}
  +
}
// Nav to the "ul";
 
toc = toc[0].children[0].children[1].children; // "ul" -> "{li}"
 
 
// remove "This is an Archive Page" entry
 
toc[0].remove();
 
 
// Cleanup
 
fixTocSectionNums();
 
}
 
 
}
 
}
 
}
 
}
   
  +
// Removes the "/20##" part of the "Page" link on talk archives
// @name fixTocSectionNums
 
// @version 1.0
 
// @description Decrements the section number of each TOC entry
 
// @copyright (c) 2013, Cole Johnson <coleharrisjohnson@gmail.com>
 
function fixTocSectionNums() {
 
var sec = $("span[class=tocnumber]");
 
var len = sec.length;
 
var split;
 
for (var i = 0; i < len; i++) {
 
split = sec[i].innerText.split(".");
 
split[0]--;
 
sec[i].innerText = split.join(".");
 
}
 
}
 
 
// @name fixTalkArchiveNSLinks
 
// @version 1.0
 
// @description Removes the "/20##" part of the "Page" link on talk archives
 
// @copyright (c) 2013, Cole Johnson <coleharrisjohnson@gmail.com>
 
 
function fixTalkArchiveNSLinks() {
 
function fixTalkArchiveNSLinks() {
if ($("#firstHeading")[0].innerText.match(/\/20[0-9]{2}$/).length != 0) {
+
if ($("#firstHeading")[0].innerText.match(/\/20[0-9]{2}$/).length != 0) {
var ns = $("#p-namespaces li[class!=selected] a")[0];
+
var ns = $("#p-namespaces li[class!=selected] a")[0];
// MediaWiki is predictable - don't check length
+
// MediaWiki is predictable - don't check length
ns.href = ns.href.replace(
+
ns.href = ns.href.replace(
/w[\/\\]index.php\?title=(.*?)(\/20[0-9]{2}).*$/,
+
/w[\/\\]index.php\?title=(.*?)(\/20[0-9]{2}).*$/,
"wiki/$1");
+
"wiki/$1");
  +
}
}
 
 
}
 
}
   

Revision as of 21:16, 17 August 2013

/**
 * The iPhone Wiki MediaWiki:Common.js
 *   - JavaScript to fix common issues on The iPhone Wiki
 * Copyright (c) 2013 Cole Johnson <coleharrisjohnson@gmail.com>
 **/

// Removes the "1 This is an Archive Page" element from the TOC on Talk Archives
function fixTalkArchiveToc() {
    // Is there a "This is an Archive Page" header block?
    if ($("table[id=archive-page-notice]").length != 0) {
        var toc = $("table[id=toc]"); // Is there a TOC?
        if (toc.length != 0) {
            // relocate to the children of the TOC table
            toc = toc[0].children;
            // MediaWiki is consistent, so no need to check length
            if (toc[0].localName == "tbody") {
                toc = toc[0].children; // skip the tbody tag
            }
            // Nav to the "ul"; 
            toc = toc[0].children[0].children[1].children; // "ul" -> "{li}"
            
            // remove "This is an Archive Page" entry
            toc[0].remove();
            
			// Decrement the outermost section number for each TOC entry
			var sec = $("span[class=tocnumber]");
			var len = sec.length;
			var split;
			for (var i = 0; i < len; i++) {
				split = sec[i].innerText.split(".");
				split[0]--;
				sec[i].innerText = split.join(".");
			}
        }
    }
}

// Removes the "/20##" part of the "Page" link on talk archives
function fixTalkArchiveNSLinks() {
    if ($("#firstHeading")[0].innerText.match(/\/20[0-9]{2}$/).length != 0) {
        var ns = $("#p-namespaces li[class!=selected] a")[0];
        // MediaWiki is predictable - don't check length
        ns.href = ns.href.replace(
            /w[\/\\]index.php\?title=(.*?)(\/20[0-9]{2}).*$/,
            "wiki/$1");
    }
}

fixTalkArchiveToc();
fixTalkArchiveNSLinks();