Forum:Coding/Javascript

Explanation
These are some helpful Javascript function to add various types of functionality to the wiki. Feel free to add other useful functions or modify these to make them more useful. To use them, copy the code (in the gray boxes below) to your personal JavaScript page:


 * If you use the default Monaco skin, use monaco.js
 * If you have switched to the Monobook skin, use monobook.js

NavBar "widget" for Monobook
This function lets you add a custom box with custom links to Monobook's navigation bar, similar to a Monaco widget. function addBar { document.getElementById('p-navigation').innerHTML += ' '+ ' '+    ' Command Center '+                                // Name the box whatever you want ' '+             // Also name the div as you like, to use in your .css 'Main Page'+ // Copy this line for each link ' '; }

addOnloadHook(addBar);

Page-top tabs
These functions add tabs to the top of each page on the wiki for easy access to common tasks.

GWW Switch
Adds a tab that links to the same article on Guild Wars Wiki. function addGWWSwitch {   var hist; var url; if (!(hist = document.getElementById('ca-history') )) return; if (!(url = hist.getElementsByTagName('a')[0] )) return; if (!(url = url.href )) return; addPortletLink('p-cactions',      'http://wiki.guildwars.com/wiki/'+wgCanonicalNamespace+':'+encodeURIComponent(wgTitle),      'GWW', 'ca-gww', 'See this page on Guild Wars Wiki', 'g'); } addOnloadHook(addGWWSwitch);

Purge
Adds a tab that will purge the server's cache of that article. function addPurge {   var hist; var url; if (!(hist = document.getElementById('ca-history') )) return; if (!(url = hist.getElementsByTagName('a')[0] )) return; if (!(url = url.href )) return; addPortletLink('p-cactions', url.replace(/([?&]action=)history([&#]|$)/, '$1purge$2'),                  'purge', 'ca-purge', 'Purge server cache', '0'); } addOnloadHook(addPurge);

Credits
Adds a tab that will display the credits for the article. function addCredits {   var hist; var url; if (!(hist = document.getElementById('ca-history') )) return; if (!(url = hist.getElementsByTagName('a')[0] )) return; if (!(url = url.href )) return; addPortletLink('p-cactions', url.replace(/([?&]action=)history([&#]|$)/, '$1credits$2'),                  'credits', 'ca-credits', 'Display article credits', '0'); } addOnloadHook(addCredits);

Custom edit buttons
This function allows you to add to the line of edit buttons above the edit box. See WikiaHelp:Custom edit buttons for more info and Wikia:Category:Custom edit buttons/Commons:Category:ButtonToolbar for premade button icons. /***** Custom edit buttons ****/ if (mwCustomEditButtons) { mwCustomEditButtons[mwCustomEditButtons.length] = { "imageFile": "http://images.wikia.com/central/images/2/23/Button_code.png", // Image file to use "speedTip": "Code", "tagOpen": " ",  // What to insert after the selected text "sampleText": "foo"}    // Text to insert between if nothing is selected // tagOpen or tagClose can be null, i.e. to insert a self-closing tag like " " }

RC hideUser
This pair of functions adds an input box to the options fieldset on Special:RecentChanges, below the Namespace select box. By entering a username or IP in the box and clicking "Hide", all edits made by that user/IP will be hidden on the page.

Note that this only affects the list of edits that you are currently viewing &mdash; it cannot actually interact with the wiki server to "reload" RC with that user omitted. For example, if you are showing 50 edits in RC and you hide a user who made 30 of those edits, you will have 20 entries left. To see more than that, you'll need to increase the number of edits shown on RC to begin with (if you need more than 500, you can edit the "limit=500" part of the URL; it works up to at least 1000).

Other notes: /*** hideUser for RC ***/ /*** Author: User:Dr ishmael ***/ function hideUserWrapper {   if (document.title.substring(0,14) == 'Recent changes') { var tables = document.getElementsByTagName('table'); var RCinput; for (var i = 0; i < tables.length; i++) { if (tables[i].className == 'mw-recentchanges-table') { RCinput = tables[i]; break; }       }        var uls = document.getElementsByTagName('ul'); var RCuls = new Array; for (var i = 0; i < uls.length; i++) { if (uls[i].className == 'special') { RCuls.push(uls[i]); }       }
 * Works on both Monobook and Monaco.
 * Does not "remember" what you entered if you refresh RC (by doing a browser refresh or by clicking on any of the other options). It's possible that this could be fixed easily, but I haven't had the time to try anything.
 * Does not work if you are showing grouped recent changes. The syntax there is extremely messy (divs inside tables inside divs), and I didn't feel like dealing with it.
 * Pressing "Enter" while in the input box will submit the Namespace form. I found a number of code samples to solve this (by capture keypresses and returning false for the Enter key), but I couldn't get any of them to work here.

var myInput = document.createElement('input'); myInput.type = 'text'; myInput.id = 'hideUsername'; myInput.name = 'hideUsername';

var myLabel = document.createElement('label'); myLabel.for = 'hideUsername'; myLabel.innerHTML = "Hide user:";

var mySubmit = document.createElement('input'); mySubmit.type = 'button'; mySubmit.value = 'Hide'; var newRow = RCinput.insertRow(1); var newLabelCell = newRow.insertCell(0); var newInputCell = newRow.insertCell(1);

newLabelCell.appendChild(myLabel);

newInputCell.appendChild(myInput); newInputCell.appendChild(mySubmit);

try { //IE mySubmit.attachEvent("onclick", function{ hideUser(RCuls) } ); }       catch(e) { //Mozilla mySubmit.addEventListener("click", function { hideUser(RCuls) }, false); }   } } addOnloadHook(hideUserWrapper);

function hideUser(RCuls) {   var myUser = document.getElementById('hideUsername').value; for (var j = 0; j < RCuls.length; j++) { var lis = RCuls[j].getElementsByTagName('li'); for (var i = 0; i < lis.length; i++) { var userLink; if (lis[i].innerHTML.substring(0,6) == '(diff)') userLink = lis[i].getElementsByTagName('a')[2]; else userLink = lis[i].getElementsByTagName('a')[3]; if (userLink.innerHTML == myUser) lis[i].style.display = 'none'; else lis[i].style.display = ''; }   } } /*** end hideUser ***/