User:The Transhumanist/WatchlistSorter.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:The Transhumanist/WatchlistSorter. |
// <syntaxhighlight lang="javascript">
/*
WatchlistSorter.js
What it does:
This script sorts your watchlist by namespace, making it much easier to browse.
Important:
It is not compatible with Internet Explorer, or its successors.
It is not compatible with the "Enhanced recent changes" option in Preferences.
Attribution: This is the "watchlistSorter.js" script by User:Misza13, with some
very minor upgrades. That version had some deprecated code, and Miszal13 had not
logged in since Feb 2015, so I undertook to bring the script up to date and further
improve it.
Brief comments are provided within the source code below. For extensive explanatory
notes on what the source code does and how it works, see the Script's workshop on
the talk page. (Not ready yet.)
*/
// ============== Set up ==============
// Start off with a bodyguard function to reserve the aliases mw and $
( function ( mw, $ ) {
// we can now rely on mw and $ within the safety of our “bodyguard” function, to mean
// "mediawiki" and "jQuery", respectively
// ============== ready() event listener/handler ==============
// below is jQuery short-hand for $(document).ready(function() { ... });
// it makes the rest of the script wait until the page's DOM is loaded and ready
$(function() {
// End of set up
// ============== deactivation filters (guard clauses) ==============
//if (location.href.indexOf('Special:Watchlist') == -1) return; //Are we on a watchlist?
// End the script if "Watchlist - Wikipedia" is not in the page title
if (document.title.indexOf("Watchlist - Wikipedia") == -1) {
// use a return statement to end the local function and hence the program's body
// important: this approach does not work outside of a function
return;
}
// =================== Prep work =====================
// Variable declarations, etc., go here
var d;
var day;
var diff;
var newday;
var As;
var hdrs;
var hdr;
var j;
var namespace;
var namespacesub;
var pagename;
// ================= Core program =================
// BODY OF PROGRAM
//days = document.getElementById('bodyContent').getElementsByTagName('ul');
var days = document.evaluate( //Hell knows how it works - found in "Dive into Greasemonkey"
"//ul[@class='special']",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);
for (d = 0; d < days.snapshotLength; d++) { //For each day
day = days.snapshotItem(d);
newday = document.createElement('ul'); //This will replace the old listing
while ((diffs = day.getElementsByTagName('li')).length > 0) { //Are there any diffs left?
//Try to extract the namespace
As = diffs[0].getElementsByTagName('a');
if (As[0].innerHTML == 'diff')
pagename = As[2].innerHTML;
else
pagename = As[1].innerHTML;
if (pagename.indexOf(':') == -1)
namespace = 'Main';
else
namespace = pagename.split(':')[0]; //This will fail for articles which contain ":" in name
hdrs = newday.getElementsByTagName('h5'); //Get the list of namespace headers
hdr = null;
for (j=0; j<hdrs.length; j++) //Find the header
if (hdrs[j].innerHTML==namespace) {
hdr = hdrs[j]; break;
}
if (hdr===null) { //Not found? Make a new one!
hdr = document.createElement('h5');
hdr.innerHTML = namespace;
newday.appendChild(hdr);
namespacesub = document.createElement('ul');
namespacesub.className = "special";
newday.appendChild(namespacesub);
}
hdr.nextSibling.appendChild(diffs[0]); //Move the diff
}
newday.appendChild(document.createElement('hr')); //For readablility
day.parentNode.replaceChild(newday,day);
}
} );
}( mediaWiki, jQuery ) );
// </syntaxhighlight>