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.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see
Wikipedia:Bypass your cache.
// <syntaxhighlight lang="javascript">/*WatchlistSorter.jsWhat 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 furtherimprove 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 titleif(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 functionreturn;}// =================== Prep work =====================// Variable declarations, etc., go herevard;varday;vardiff;varnewday;varAs;varhdrs;varhdr;varj;varnamespace;varnamespacesub;varpagename;// ================= Core program ================= // BODY OF PROGRAM//days = document.getElementById('bodyContent').getElementsByTagName('ul');vardays=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 dayday=days.snapshotItem(d);newday=document.createElement('ul');//This will replace the old listingwhile((diffs=day.getElementsByTagName('li')).length>0){//Are there any diffs left?//Try to extract the namespaceAs=diffs0].getElementsByTagName('a');if(As0].innerHTML=='diff')pagename=As2].innerHTML;elsepagename=As1].innerHTML;if(pagename.indexOf(':')==-1)namespace='Main';elsenamespace=pagename.split(':')[0];//This will fail for articles which contain ":" in namehdrs=newday.getElementsByTagName('h5');//Get the list of namespace headershdr=null;for(j=0;j<hdrs.length;j++)//Find the headerif(hdrsj].innerHTML==namespace){hdr=hdrsj];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(diffs0]);//Move the diff}newday.appendChild(document.createElement('hr'));//For readablilityday.parentNode.replaceChild(newday,day);}});}(mediaWiki,jQuery));// </syntaxhighlight>