Difference between revisions of "MediaWiki:Common.js"

Jump to: navigation, search

Difference between revisions of "MediaWiki:Common.js"

(Created page with "Any JavaScript here will be loaded for all users on every page load.: // By User:Pile0nades. // Makes the diff page display as a single column, instead of two. addOnload...")
 
Line 2: Line 2:
 
// By User:Pile0nades.
 
// By User:Pile0nades.
 
// Makes the diff page display as a single column, instead of two.
 
// Makes the diff page display as a single column, instead of two.
 +
$j(function() {
 +
addOnloadHook(function() {
 +
    // ==UserScript==
 +
    // @name          Wikipedia single column diffs
 +
    // @namespace      http://mywebsite.com/myscripts
 +
    // @description    Make Wikipedia diff pages read like patch-format diffs, only one column needed
 +
    // @include        http://en.wikipedia.org/w/index.php?*&diff=*
 +
    // ==/UserScript==
  
addOnloadHook(function(){
 
// ==UserScript==
 
// @name          Wikipedia single column diffs
 
// @namespace      http://mywebsite.com/myscripts
 
// @description    Make Wikipedia diff pages read like patch-format diffs, only one column needed
 
// @include        http://en.wikipedia.org/w/index.php?*&diff=*
 
// ==/UserScript==
 
  
  
 +
    // remove the header for the previous revision, and add the next/prev links to the second header
 +
    var prevHeader = get("//table[@class='diff'][1]/tbody/tr[1]/td[@class='diff-otitle']");
 +
    var nextHeader = get("//table[@class='diff'][1]/tbody/tr[1]/td[@class='diff-ntitle']");
 +
    var oldedit = get("//table[@class='diff'][1]/tbody/tr[1]/td[@class='diff-otitle']/a[@id='differences-prevlink']");
 +
    var newedit = get("//table[@class='diff'][1]/tbody/tr[1]/td[@class='diff-ntitle']/a[@id='differences-nextlink']");
 +
    for (var i = 0; i < prevHeader.snapshotLength; i++) {
 +
        if (oldedit.snapshotLength != 0) {
 +
            nextHeader.snapshotItem(i).appendChild(oldedit.snapshotItem(0));
 +
        }
 +
        if (oldedit.snapshotLength != 0 && newedit.snapshotLength != 0) {
 +
            nextHeader.snapshotItem(i).appendChild(document.createTextNode(" | "));
 +
        }
 +
        if (newedit.snapshotLength != 0) {
 +
            nextHeader.snapshotItem(i).appendChild(newedit.snapshotItem(0));
 +
        }
 +
        remove(prevHeader.snapshotItem(i));
 +
    }
  
// remove the header for the previous revision, and add the next/prev links to the second header
 
var prevHeader = get("//table[@class='diff'][1]/tbody/tr[1]/td[@class='diff-otitle']");
 
var nextHeader = get("//table[@class='diff'][1]/tbody/tr[1]/td[@class='diff-ntitle']");
 
var oldedit = get("//table[@class='diff'][1]/tbody/tr[1]/td[@class='diff-otitle']/a[@id='differences-prevlink']");
 
var newedit = get("//table[@class='diff'][1]/tbody/tr[1]/td[@class='diff-ntitle']/a[@id='differences-nextlink']");
 
for(var i = 0; i < prevHeader.snapshotLength; i++) {
 
  if(oldedit.snapshotLength != 0) {
 
    nextHeader.snapshotItem(i).appendChild(oldedit.snapshotItem(0));
 
  }
 
  if(oldedit.snapshotLength != 0 && newedit.snapshotLength != 0) {
 
    nextHeader.snapshotItem(i).appendChild(document.createTextNode(" | "));
 
  }
 
  if(newedit.snapshotLength != 0) {
 
    nextHeader.snapshotItem(i).appendChild(newedit.snapshotItem(0));
 
  }
 
  remove(prevHeader.snapshotItem(i));
 
}
 
  
  
 +
    // clone rows where the line changed but wasn't added/deleted, and delete where appropriate
 +
    var changedLines = get("//table[@class='diff'][1]/tbody/tr/td[@class='diff-addedline' and position()=4]/../td[@class='diff-deletedline' and position()=2]")
 +
    for (var i = 0; i < changedLines.snapshotLength; i++) {
 +
        var cell = changedLines.snapshotItem(i);
  
// clone rows where the line changed but wasn't added/deleted, and delete where appropriate
+
        cell.parentNode.parentNode.insertBefore(
var changedLines = get("//table[@class='diff'][1]/tbody/tr/td[@class='diff-addedline' and position()=4]/../td[@class='diff-deletedline' and position()=2]")
+
            cell.parentNode.cloneNode(true),
for(var i = 0; i < changedLines.snapshotLength; i++) {
+
            cell.parentNode
  var cell = changedLines.snapshotItem(i);
+
        );
  
  cell.parentNode.parentNode.insertBefore(
+
        remove(cell.previousSibling.previousSibling);
    cell.parentNode.cloneNode(true),
+
        remove(cell);
    cell.parentNode
+
    }
  );
 
  
  remove(cell.previousSibling.previousSibling);
 
  remove(cell);
 
}
 
  
  
 +
    // delete blank cells in the first column to move the added lines into the first column
 +
    var blankcells = get("//table[@class='diff'][1]/tbody/tr/td[1]");
 +
    for (var i = 0; i < blankcells.snapshotLength; i++) {
 +
        if (blankcells.snapshotItem(i).innerHTML == " ") {
 +
            remove(blankcells.snapshotItem(i));
 +
        }
 +
    }
  
// delete blank cells in the first column to move the added lines into the first column
+
    // delete everything past the second td in each column
var blankcells = get("//table[@class='diff'][1]/tbody/tr/td[1]");
+
    var waste = get("//table[@class='diff'][1]/tbody/tr/td[2]");
for(var i = 0; i < blankcells.snapshotLength; i++) {
+
    for (var i = 0; i < waste.snapshotLength; i++) {
  if(blankcells.snapshotItem(i).innerHTML == " ") {
+
        while (waste.snapshotItem(i).nextSibling) {
    remove(blankcells.snapshotItem(i));
+
            remove(waste.snapshotItem(i).nextSibling);
  }
+
        }
}
+
    }
  
// delete everything past the second td in each column
+
    // delete the 2nd line number cell in each column
var waste = get("//table[@class='diff'][1]/tbody/tr/td[2]");
+
    var line = get("//table[@class='diff'][1]/tbody/tr/td[@class='diff-lineno'][1]");
for(var i = 0; i < waste.snapshotLength; i++) {
+
    for (var i = 0; i < line.snapshotLength; i++) {
  while(waste.snapshotItem(i).nextSibling) {
+
        remove(line.snapshotItem(i));
    remove(waste.snapshotItem(i).nextSibling);
+
    }
  }
 
}
 
  
// delete the 2nd line number cell in each column
+
    // delete the extra cols
var line = get("//table[@class='diff'][1]/tbody/tr/td[@class='diff-lineno'][1]");
+
    var cols = get("//table[@class='diff'][1]/col");
for(var i = 0; i < line.snapshotLength; i++) {
+
    for (var i = 0; i < cols.snapshotLength; i++) {
  remove(line.snapshotItem(i));
+
        var col = cols.snapshotItem(i);
}
+
        if (col.className == "diff-content") {
 +
            col.style.width = "98%";
 +
            col.removeAttribute("class");
 +
        }
 +
        if (i > 1) remove(col);
 +
    }
  
// delete the extra cols
+
    // shorthand function to remove nodes
var cols = get("//table[@class='diff'][1]/col");
+
    function remove(node) {
for(var i = 0; i < cols.snapshotLength; i++) {
+
        node.parentNode.removeChild(node);
  var col = cols.snapshotItem(i);
+
     }
  if(col.className == "diff-content") {
 
    col.style.width = "98%";
 
     col.removeAttribute("class");
 
  }
 
  if(i > 1) remove(col);
 
}
 
  
// shorthand function to remove nodes
 
function remove(node) {
 
  node.parentNode.removeChild(node);
 
}
 
  
 
+
    // xpath function
// xpath function
+
    function get(query) {
function get(query) {
+
        return document.evaluate(
  return document.evaluate(
+
            query,
    query,
+
            document,
    document,
+
            null,
    null,
+
            XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
+
            null
    null
+
        );
  );
+
    }
}
+
})});
});
 
 
//
 
//

Revision as of 21:57, 2 July 2015

/* Any JavaScript here will be loaded for all users on every page load. */
// By User:Pile0nades.
// Makes the diff page display as a single column, instead of two.
$j(function() { 
addOnloadHook(function() {
    // ==UserScript==
    // @name           Wikipedia single column diffs
    // @namespace      http://mywebsite.com/myscripts
    // @description    Make Wikipedia diff pages read like patch-format diffs, only one column needed
    // @include        http://en.wikipedia.org/w/index.php?*&diff=*
    // ==/UserScript==



    // remove the header for the previous revision, and add the next/prev links to the second header
    var prevHeader = get("//table[@class='diff'][1]/tbody/tr[1]/td[@class='diff-otitle']");
    var nextHeader = get("//table[@class='diff'][1]/tbody/tr[1]/td[@class='diff-ntitle']");
    var oldedit = get("//table[@class='diff'][1]/tbody/tr[1]/td[@class='diff-otitle']/a[@id='differences-prevlink']");
    var newedit = get("//table[@class='diff'][1]/tbody/tr[1]/td[@class='diff-ntitle']/a[@id='differences-nextlink']");
    for (var i = 0; i < prevHeader.snapshotLength; i++) {
        if (oldedit.snapshotLength != 0) {
            nextHeader.snapshotItem(i).appendChild(oldedit.snapshotItem(0));
        }
        if (oldedit.snapshotLength != 0 && newedit.snapshotLength != 0) {
            nextHeader.snapshotItem(i).appendChild(document.createTextNode(" | "));
        }
        if (newedit.snapshotLength != 0) {
            nextHeader.snapshotItem(i).appendChild(newedit.snapshotItem(0));
        }
        remove(prevHeader.snapshotItem(i));
    }



    // clone rows where the line changed but wasn't added/deleted, and delete where appropriate
    var changedLines = get("//table[@class='diff'][1]/tbody/tr/td[@class='diff-addedline' and position()=4]/../td[@class='diff-deletedline' and position()=2]")
    for (var i = 0; i < changedLines.snapshotLength; i++) {
        var cell = changedLines.snapshotItem(i);

        cell.parentNode.parentNode.insertBefore(
            cell.parentNode.cloneNode(true),
            cell.parentNode
        );

        remove(cell.previousSibling.previousSibling);
        remove(cell);
    }



    // delete blank cells in the first column to move the added lines into the first column
    var blankcells = get("//table[@class='diff'][1]/tbody/tr/td[1]");
    for (var i = 0; i < blankcells.snapshotLength; i++) {
        if (blankcells.snapshotItem(i).innerHTML == " ") {
            remove(blankcells.snapshotItem(i));
        }
    }

    // delete everything past the second td in each column
    var waste = get("//table[@class='diff'][1]/tbody/tr/td[2]");
    for (var i = 0; i < waste.snapshotLength; i++) {
        while (waste.snapshotItem(i).nextSibling) {
            remove(waste.snapshotItem(i).nextSibling);
        }
    }

    // delete the 2nd line number cell in each column
    var line = get("//table[@class='diff'][1]/tbody/tr/td[@class='diff-lineno'][1]");
    for (var i = 0; i < line.snapshotLength; i++) {
        remove(line.snapshotItem(i));
    }

    // delete the extra cols
    var cols = get("//table[@class='diff'][1]/col");
    for (var i = 0; i < cols.snapshotLength; i++) {
        var col = cols.snapshotItem(i);
        if (col.className == "diff-content") {
            col.style.width = "98%";
            col.removeAttribute("class");
        }
        if (i > 1) remove(col);
    }

    // shorthand function to remove nodes
    function remove(node) {
        node.parentNode.removeChild(node);
    }


    // xpath function
    function get(query) {
        return document.evaluate(
            query,
            document,
            null,
            XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
            null
        );
    }
})});
//