User talk:The Transhumanist/WatchlistSorter.js
This script sorts your watchlist by namespace, making it much easier to browse. It is not compatible with Internet Explorer, or its successors. It is not compatible with the "Enhanced recent changes" option in Preferences. Script's workshop
Description / instruction manual for WatchlistSorter.js
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 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. (I'm in the process of doing that now). How to install this scriptTo install this script, add this line to your common.js (or your vector.js) page:
Save the page and bypass your cache to make sure the changes take effect. By the way, only logged-in users can install scripts. Explanatory notes (source code walk-through)This section explains the source code, in detail. It is for JavaScript programmers, and for those who want to learn how to program in JavaScript. Hopefully, this will enable you to adapt existing source code into new user scripts with greater ease, and perhaps even compose user scripts from scratch. You can only use so many comments in the source code before you start to choke or bury the programming itself. So, I've put short summaries in the source code, and have provided in-depth explanations here. My intention is Threefold:
In addition to plain vanilla JavaScript code, this script relies heavily on the jQuery library. If you have any comments or questions, feel free to post them at the bottom of this page under Discussions. Be sure to {{ping}} me when you do. General approach(general approach goes here) More specifically, starting at the beginning... AliasesAn alias is one string defined to mean another. Another term for "alias" is "shortcut". In the script, the following aliases are used:
These two aliases are set up like this: ( function ( mw, $ ) {}( mediaWiki, jQuery ) );
That also happens to be a "bodyguard function", which is explained in the section below... Bodyguard functionThe bodyguard function assigns an alias for a name within the function, and reserves that alias for that purpose only. For example, if you want "t" to be interpreted only as "transhumanist". Since the script uses jQuery, we want to defend jQuery's alias, the "$". The bodyguard function makes it so that "$" means only "jQuery" inside the function, even if it means something else outside the function. That is, it prevents other javascript libraries from overwriting the $() shortcut for jQuery within the function. It does this via scoping. The bodyguard function is used like a wrapper, with the alias-containing source code inside it, typically, wrapping the whole rest of the script. Here's what a jQuery bodyguard function looks like: 1 ( function($) {
2 // you put the body of the script here
3 } ) ( jQuery );
See also: bodyguard function solution. To extend that to lock in "mw" to mean "mediawiki", use the following (this is what the script uses): 1 ( function(mw, $) {
2 // you put the body of the script here
3 } ) (mediawiki, jQuery);
For the best explanation of the bodyguard function I've found so far, see: Solving "$(document).ready is not a function" and other problems (Long live Spartacus!) The ready() event listener/handlerThe ready() event listener/handler makes the rest of the script wait until the page (and its DOM) is loaded and ready to be worked on. If the script tries to do its thing before the page is loaded, there won't be anything there for the script to work on (such as with scripts that will have nowhere to place the menu item mw.util.addPortletLink), and the script will fail. In jQuery, it looks like this: You can do that in jQuery shorthand, like this: $().ready( function() {} );
Or even like this: $(function() {});
The part of the script that is being made to wait goes inside the curly brackets. But you would generally start that on the next line, and put the ending curly bracket, closing parenthesis, and semicolon following that on a line of their own), like this: 1 $(function() {
2 // Body of function (or even the rest of the script) goes here, such as a click handler.
3 });
This is all explained further at the jQuery page for For the plain vanilla version see: http://docs.jquery.com/Tutorials:Introducing_$(document).ready() Deactivation filtersMany scripts are written to work on a particular page or page type, and might have unexpected results if run on some other page. So a deactivation filter is used so the program does not run for the wrong pages. For example: 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;
}
What this if statement does is checks that the current page is not the one we want, and if that is true, we end the program via a return statement. What You could do something similar with a straight if construct without "return;", checking for a page match, but then you'd have to have your whole script body inside the construct, which adds a level of indentation. The more filters, the more levels of indentation. The above approach avoids unnecessary indentation, and makes it easier to keep track of the curly brackets, as the closing bracket isn't way off at the end of the program. varThis is the reserved word var, which is used to declare variables. A variable is a container you can put a value in. To declare the variable portletlink, write this: var portletlink
A declared variable has no value, until you assign it one, such as like this: portletlink = "yo mama";
You can combine declaration and assignment in the same statement, like this: var portletlink = mw.util.addPortletLink('p-tb', '#', 'Remove red links');
Caveat: if you assign a value to a variable that does not exist, the variable will be created automatically. If it is created outside of a function, it will have global scope. For user scripts used on Wikipedia, having a variable of global scope means the variable may affect other scripts that are running, as the scripts are technically part of the same program, being called via import from a .js page (.js pages are programs). So, be careful. Here are some scope-related resources: Change log for WatchlistSorter.js
Task listBugs reportsDesired/completed features
Development notes for WatchlistSorter.jsLegacy JavaScript
Hello! This script has been detected as using deprecated parameters that need to be replaced with the updated version. Examples include Script dependenciesDiscussions
toggle on/off?I was wondering about being able to toggle this on and off. Sometimes I have plenty of time to meander through my watchlist and don't mind just paging through until I hit that horizontal line. Other times I want to check certain namespaces. Obviously I can just uninstall/reinstall, but a toggle button would be cool. —valereee (talk) 20:45, 22 June 2021 (UTC) |