User:DatRoot/Extensions.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:DatRoot/Extensions. |
String.prototype.equals = function(other, ignoreCase)
{
if(ignoreCase) return this.toLowerCase() == other.toLowerCase();
else return this == other;
}
String.isNullOrEmpty = function(s)
{
return !s || s.length == 0;
}
String.prototype.startsWith = function(s, ignoreCase)
{
if(ignoreCase) return this.substr(0, s.length).toLowerCase() == s.toLowerCase();
else return this.substr(0, s.length) == s;
}
String.prototype.trimEnd = function()
{
for(var p = this.length - 1; p >= 0; p--)
{
var c = this.charAt(p);
var matched = false;
for(var i = 0; i < arguments.length; i++)
{
if(arguments[i] == c)
{
matched = true;
break;
}
}
if(!matched) return this.substr(0, p + 1);
}
return "";
}
function getChildByClassName(parent, name)
{
var elem = parent.firstChild;
while(elem != null && elem.className != name) elem = elem.nextSibling;
return elem;
}
function getChildByNodeName(parent, name)
{
var elem = parent.firstChild;
while(elem != null && elem.nodeName != name) elem = elem.nextSibling;
return elem;
}
function getQueryParams()
{
var params = [];
var queryString = location.search;
if(queryString.length > 0)
{
var paramStrings = queryString.substr(1).split("&");
for(var i = 0; i < paramStrings.length; i++)
{
var nameValuePair = unescape(paramStrings[i]).split("=");
params[nameValuePair[0]] = nameValuePair[1] || "";
}
}
return params;
}
function HistoryPageExtensions() {
function setup()
{
var bodyContent = document.getElementById("bodyContent");
var sections = {};
// Get section name filter from address
var params = getQueryParams();
var sectionName = params["section"] || "";
// Create filter link element that will be cloned and added every list item that
// specifies a section
var filterElem = document.createElement("a");
filterElem.href = mw.config.get('wgServer') + mw.config.get('wgScript') + "?title=" + wgPageName + "&action=history§ion=";
filterElem.innerHTML = "F";
//debugger;
for(var itemElem = document.getElementById("pagehistory").firstChild;
itemElem != null; itemElem = itemElem.nextSibling)
{
if(itemElem.nodeName != "LI") continue;
var itemSectionName = "";
// Look for section name in comment and add filter link
var commentElem = getChildByClassName(itemElem, "comment");
if(commentElem) commentElem = getChildByClassName(commentElem, "autocomment");
if(commentElem)
{
itemSectionName = commentElem.lastChild.nodeValue.trimEnd(" ", "-");
sections[itemSectionName] = true;
var itemFilterElem = filterElem.cloneNode(true);
itemFilterElem.href += itemSectionName;
commentElem.insertBefore(itemFilterElem, commentElem.firstChild);
}
// Hide list item if its section doesn't match the filter
if(sectionName.length > 0 && !itemSectionName.equals(sectionName, true)) itemElem.style.display = "none";
}
// Create combo box of section names
var sectionSelect = document.createElement("select");
for(var name in sections)
{
var opt = document.createElement("option");
opt.value = name; opt.text = name;
sectionSelect.add(opt, null);
if(name == sectionName) opt.selected = true;
}
//addHandler(sectionSelect, "onchange", function() {
// alert(sectionSelect.value);
//});
bodyContent.insertBefore(sectionSelect, document.getElementById("histlegend"));
bodyContent.insertBefore(document.createTextNode(" Section:"), sectionSelect);
// Add the section name to other links on the page
if(!String.isNullOrEmpty(sectionName))
{
for(var linkElem = document.getElementById("bodyContent").firstChild;
linkElem != null; linkElem = linkElem.nextSibling)
{
if(linkElem.nodeName == "A") linkElem.href += "§ion=" + sectionName;
}
}
}
if(wgAction == "history") addOnloadHook(setup);
}
HistoryPageExtensions();