Difference between revisions of "MediaWiki:Common.js"

From The iPhone Wiki
Jump to: navigation, search
m
Line 9: Line 9:
 
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 ($("[id=archive-page-notice]").length == 0)
+
if ($(".archive-page-notice").length == 0)
 
return;
 
return;
 
 
 
// Is there a TOC?
 
// Is there a TOC?
var toc = $("[id=toc]>ul>li");
+
var toc = $("#toc li");
 
if (toc.length == 0)
 
if (toc.length == 0)
 
return;
 
return;
Line 21: Line 21:
 
 
 
// Decrement the outermost section number for each TOC entry
 
// Decrement the outermost section number for each TOC entry
var sec = $("[class=tocnumber]");
+
var sec = $(".tocnumber");
var len = sec.length;
+
for (var i = 0; i < sec.length; i++) {
var arr;
+
var arr = sec[i].innerText.split(".");
for (var i = 0; i < len; i++) {
 
arr = sec[i].innerText.split(".");
 
 
arr[0]--;
 
arr[0]--;
 
sec[i].innerText = arr.join(".");
 
sec[i].innerText = arr.join(".");
Line 32: Line 30:
   
 
// Removes the "/20##" part of the "Page" link on talk archives
 
// Removes the "/20##" part of the "Page" link on talk archives
// TODO: Work on [[The iPhone Wiki:Community Portal]]'s archives
 
 
function fixTalkArchiveNSLinks() {
 
function fixTalkArchiveNSLinks() {
 
// Are we an archive?
 
// Are we an archive?
var page = $(".firstHeading")[0].innerText;
+
var page = $(".firstHeading")[0].innerHTML;
 
if (page.match(/\/20[0-9]{2}$/) == null)
 
if (page.match(/\/20[0-9]{2}$/) == null)
 
return;
 
return;
 
 
  +
var ns = $("#mw-toolbar .new");
if (page.indexOf("The iPhone Wiki:Community Portal") != -1)
 
return;
 
 
var ns = $("[class=new]");
 
 
if (ns.length == 0) {
 
if (ns.length == 0) {
console.log("Can't find namespace selector thingy!");
+
console.log("Can't find namespace selector button!");
 
return;
 
return;
 
}
 
}
 
 
 
ns[0].className = ns[0].className.replace("new", "");
 
ns[0].className = ns[0].className.replace("new", "");
ns = ns.find("a");
+
ns = ns.find("a")[0];
ns[0].href = ns[0].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 20:10, 27 April 2016

/**
 * The iPhone Wiki MediaWiki:Common.js
 *   - JavaScript to fix common issues on The iPhone Wiki
 * Copyright (c) 2013-2014, 2016 Cole Johnson <coleharrisjohnson@gmail.com>
 * Licensed under CC-BY-SA 4.0 <https://creativecommons.org/licenses/by-sa/4.0/>
 **/

// 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 ($(".archive-page-notice").length == 0)
        return;
    
    // Is there a TOC?
    var toc = $("#toc li");
    if (toc.length == 0)
        return;
    
    // Remove "This is an Archive Page" entry
    toc[0].remove();
    
    // Decrement the outermost section number for each TOC entry
    var sec = $(".tocnumber");
    for (var i = 0; i < sec.length; i++) {
        var arr = sec[i].innerText.split(".");
        arr[0]--;
        sec[i].innerText = arr.join(".");
    }
}

// Removes the "/20##" part of the "Page" link on talk archives
function fixTalkArchiveNSLinks() {
    // Are we an archive?
    var page = $(".firstHeading")[0].innerHTML;
    if (page.match(/\/20[0-9]{2}$/) == null)
        return;
    
    var ns = $("#mw-toolbar .new");
    if (ns.length == 0) {
        console.log("Can't find namespace selector button!");
        return;
    }
    
    ns[0].className = ns[0].className.replace("new", "");
    ns = ns.find("a")[0];
    ns.href = ns.href.replace(
        /w[\/\\]index.php\?title=(.*?)(\/20[0-9]{2}).*$/,
        "wiki/$1");
}

fixTalkArchiveToc();
fixTalkArchiveNSLinks();