User:Prog/common.js
Appearance
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
- Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac);
- Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5;
- Konqueror: simply click the Reload button, or press F5;
- Opera users may need to completely clear their cache in Tools→Preferences.
Warning: Malicious code can compromise your account. Page preview will cause your web browser to execute this page's content as code under some skins, including Monobook. If you have any questions about any code you plan to add, you can ask at the appropriate reading room. |
Documentation for this script can be added at User:Prog/common. This script seems to have an accompanying .css page at User:Prog/common.css. |
function findFirstOf(text, characters){
for(var i = 0; i < text.length; ++i){
for(var j = 0; j < characters.length; ++j){
if(text[i] == characters[j]) return i;
}
}
return text.length;
}
function findLastOf(text, characters){
for(var i = text.length; i > 0; --i){
for(var j = 0; j < characters.length; ++j){
if(text[i-1] == characters[j]) return i-1;
}
}
return text.length;
}
function findFirstNotOf(text, characters){
for(var i = 0; i < text.length; ++i){
for(var j = 0; j < characters.length; ++j){
if(text[i] != characters[j]) return i;
}
}
return text.length;
}
function findLastNotOf(text, characters){
for(var i = text.length; i > 0; --i){
for(var j = 0; j < characters.length; ++j){
if(text[i-1] != characters[j]) return i-1;
}
}
return text.length;
}
function trim(text, characters){
var first = findFirstNotOf(text, characters);
var last = findLastNotOf(text, characters);
var length = last - first + (last == text.length ? 0 : 1);
return text.substr(first, length);
}
function wrapTitle(separators) {
if(wgAction != 'view' && wgAction != 'purge') return false;
var title = document.getElementById("firstHeading");
var saveTitle = title.firstChild.data;
while(title.hasChildNodes()){
title.removeChild(title.firstChild);
}
var littleTitle = document.createElement("span");
littleTitle.appendChild(document.createTextNode(saveTitle));
littleTitle.setAttribute("style", "font-size:50%");
// split title by all separators
var titleParts = new Array(saveTitle);
for(var i = 0; i < separators.length; ++i){
var newParts = new Array();
for(var j = 0; j < titleParts.length; ++j){
var newSubParts = titleParts[j].split(separators[i]);
for(var k = 0; k < newSubParts.length; ++k){
newParts.push(newSubParts[k]);
}
}
titleParts = newParts;
}
// trim all title parts
for(var i = 0; i < titleParts.length; ++i){
titleParts[i] = trim(titleParts[i], " \t\n");
}
title.appendChild(littleTitle);
for(var i = 0; i < titleParts.length; ++i){
title.appendChild(document.createElement("br"));
title.appendChild(document.createTextNode(titleParts[i]));
}
return true;
}
function createTopBox(){
var box = document.createElement("div");
box.setAttribute("style", "float:right;text-align:right;font-size:0.8em;");
var title = document.getElementById("firstHeading")
title.parentNode.insertBefore(box, title);
return box;
}
function addToBox(box, element){
if(box == null || element == null) return false;
var subBox = document.createElement("div");
if(typeof(element) == "object" && (element instanceof Array)){
if(element.length > 0) subBox.appendChild(element[0]);
for(var i = 1; i < element.length; ++i){
subBox.appendChild(document.createTextNode(", "));
subBox.appendChild(element[i]);
}
}else{
subBox.appendChild(element);
}
box.appendChild(subBox);
}
function createPurgeLink(){
if(wgAction != 'view' && wgAction != 'purge') return null;
var link = document.createElement("a");
link.appendChild(document.createTextNode("purge"));
link.setAttribute("href", mw.config.get('wgServer') + mw.config.get('wgScript') + "?title=" + encodeURIComponent(mw.config.get('wgPageName')) + "&action=purge");
return link;
}
function createUserLinks(){
if(wgAction != 'view' && wgAction != 'purge') return null;
if(wgNamespaceNumber != 2 && wgNamespaceNumber != 3) return null;
var username = wgPageName.substring(findFirstOf(wgPageName, ":") + 1, wgPageName.length);
var separators = new Array("/", ":");
username = username.substring(0, findFirstOf(username, separators));
var userpage = document.createElement("a");
userpage.appendChild(document.createTextNode(username));
userpage.setAttribute("href", mw.config.get('wgServer') + mw.config.get('wgArticlePath').replace('$1', wgFormattedNamespaces[2] + ":" + encodeURIComponent(username)));
var usertalk = document.createElement("a");
usertalk.appendChild(document.createTextNode("Diskussion"));
usertalk.setAttribute("href", mw.config.get('wgServer') + mw.config.get('wgArticlePath').replace('$1', wgFormattedNamespaces[3] + ":" + encodeURIComponent(username)));
var contributions = document.createElement("a");
contributions.appendChild(document.createTextNode("Beiträge"));
contributions.setAttribute("href", mw.config.get('wgServer') + mw.config.get('wgArticlePath').replace('$1', wgFormattedNamespaces[-1] + ":" + "Beiträge" + "/" + encodeURIComponent(username)));
return new Array(userpage, usertalk, contributions);
}
function format_watchlists(){
if(wgNamespaceNumber != -1) return false;
$('li').each(function(index){
var elements = $(this).attr("class").split(" ");
var i;
for(i = 0; i < elements.length; ++i){
if(elements[i].substr(0, 10) == "watchlist-"){
$(this).addClass(elements[i].substr(0, 11))
}
}
});
return true;
}
function setup(){
var separators = new Array("/", ":");
wrapTitle(separators);
var box = createTopBox();
addToBox(box, createUserLinks());
addToBox(box, createPurgeLink());
format_watchlists();
}
$(setup);