This script provides the menu item Navbox as list that, when clicked, changes the formatting of all navboxes on the page to include (right on the screen) the wikicode for formatting outlines. It makes headings look like this: == Heading ==
(just like you see inside WP's wiki editor). It also converts the lists in the navboxes from horizontal to vertical bullet lists. It then disables the graphical bullets, and wraps * ]
around the page names. This allows you to copy and paste the material right from the screen to an outline you are editing in another window without having to type in the wikicodes manually. To undo the view, reload the page.
This script provides the menu item Navbox as list that, when clicked, changes the formatting of all navboxes on the page to include (right on the screen) the wikicode for formatting outlines. It makes headings look like this: == Heading ==
(just like you see inside WP's wiki editor). It also converts the lists in the navboxes from horizontal to vertical bullet lists. It then disables the graphical bullets, and wraps * ]
around the page names. This allows you to copy and paste the material right from the screen to an outline you are editing in another window without having to type in the wikicodes manually. To undo the view, reload the page.
To copy/paste, simply select the material desired, including headings. It will all display in the proper left alignment in the edit window.
The script still needs some visual fine tuning.
Important: this script was developed for use with the Vector skin (it's Wikipedia's default skin), and might not work with other skins. See the top of your Preferences appearance page, to be sure Vector is the chosen skin for your account.
To install this script, add this line to your vector.js page:
importScript("User:The Transhumanist/ViewAsOutline-Templates.js");
Save the page and bypass your cache to make sure the changes take effect. By the way, only logged-in users can install scripts.
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.
An alias is one string defined to mean another. Another term for "alias" is "shortcut". In the script, the following aliases are used:
$
is the alias for jQuery (the jQuery library)
mw
is the alias for mediawiki (the mediawiki library)
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...
The 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/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: $( document ).ready(function() {});
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 .ready()
For the plain vanilla version see: http://docs.jquery.com/Tutorials:Introducing_$(document).ready()
Initially each script I write is made to work only on the vector skin, the skin under which I developed it, and by default the only skin for which it is initially tested with. To limit the script to working for vector only, I use the following if control structure:
if ( mw.config.get( 'skin' ) === 'vector' ) {
}
To test it with another skin, remove or comment out the above code from the script.
*]
wikicode to linksSkipped.
in HTML, dd is used for these:
ul {
list-style-type: none;
}
$('.someElements').css( propertyName, value )
becomes:
$( 'ul' ).css( list-style-type, none )
$( 'ul' ).css( list-style-image, none )
I need a way to add the appropriate number of equals signs for headings, and bullets for offspring list items.
There's some nesting already in some of the navboxes, and it was causing the "* ]" to wrap multiple times. That could be used to advantage, but it doesn't cover every case.
Dear Evad,
Happy Hollidays! I hope yours went well.
I've been spending mine banging my head against some new scripts. And I've run into an enigma...
I'd like to remove the bullets from unordered lists, and the following code doesn't have any effect:
// Make the bullets go bye bye
$( ".navbox").find( "ul" ).css( "list-style-type", "none" );
I also tried this:
// Make the bullets go bye bye
$( "ul" ).css( "list-style-type", "none" );
And it didn't work either.
Do you have any idea what I'm doing wrong? The Transhumanist 14:47, 30 December 2017 (UTC)
::after
CSS pseudo element . Which means that to override it, you have to add more CSS rules to the page. There is a handy function mw.util.addCSS()
that you can use:// Make the bullets go bye bye
mw.util.addCSS('.hlist dd:after, .hlist li:after { content: ""; }');
mw.util
is available, so you need to surround your script with:// Load dependencies
mw.loader.using( , function () {
// ... your script goes here ...
});
mw.loader.using
line after the $(function() {
line - Evad37 03:17, 31 December 2017 (UTC)$( ".navbox").find( "*" ).removeClass( "hlist" );
// Make the ul's targettable by adding a class to them
$( ".navbox").find( "ul" ).addClass( "navbox-ul" ); //this part worked
// Make the bullets go bye bye, by altering the elements' style
var elements = document.getElementsByClassName('navbox-ul');
elements.style.listStyleType="none";
// Make the bullets go bye bye
mw.util.addCSS('.navbox-list dd:after, .navbox-list li:after { content: ""; }');
// Make the bullets go bye bye
$( ".navbox-ul" ).css( "list-style-type", "none" );
list-style-image: none;
to override the image of a bullet that Vector uses by default. So that line should actually be// Make the bullets go bye bye
$( ".navbox-ul" ).css( {"list-style-type":"none", "list-style-image":"none"} );
// Make the bullets go bye bye
$( ".navbox-ul" ).css( "list-style-type", "none" );
$( ".navbox-ul" ).css( "list-style-image", "none" );
// Make the bullets go bye bye, by altering the elements' style
var elements = document.getElementsByClassName('navbox-ul');
elements.style.listStyleType="none";
elements.style.listStyleImage="none";
In the script above, I'd like to force all the navigation footers on the page to be uncollapsed. I've also run into this obstacle on another script, for working on pages like this, where I need the boxes expanded. How is that done in a script? The Transhumanist 07:51, 6 January 2018 (UTC)
I went back and looked at the HTML source more closely, after studying the .css method, and found "display: none". So I checked what was there when the boxes were displaying, and invoked that with this line of code:
// Force the navboxes to show
$('tr').css('display', 'table-row');
I'm guessing the .find method may allow this to be restricted to occurrences of tr within navboxes only. I'll keep you posted. The Transhumanist 07:09, 13 January 2018 (UTC)
I've learned that looking at page source is a tricky business - it doesn't show the current state. Just looking at the page source isn't good enough; it turns out that other scripts (like NavFrame in mw:common.js) change the source after that picture is taken, requiring that you view selected source to see its current state.
So, while in "view page source" NavContent in a series box showed no indication of being collapsed (and so no code available to change), looking at the current page source (via "View Selection Source" in drop-down menu on right-click) reveals that some JavaScript came along afterward and added some styling (display: none). When expanded, that changes (to display: block).
So, I added a few "display; block"s of my own. :) Now I have a script, that among other things, forces all the content in sidebar and series boxes to be displayed, which is very useful for someone inspecting and collecting list components. Still needs refining of the formatting though. The script is ViewAsOutline-Sidebar.js.
This (acquiring JavaScript) is a grueling learning curve!
Do you have any suggestions that might make this journey easier? :) The Transhumanist 06:47, 18 January 2018 (UTC)
console.log()
, and also evaluate snippets of Javascript you type into it. - Evad37 04:42, 19 January 2018 (UTC)By the way, do I need to load any dependencies for the following code?
// ============== activation filters ==============
// Only activate on Vector skin
if ( mw.config.get( 'skin' ) === 'vector' ) {
Do all "mw." lines have dependencies? The Transhumanist 13:40, 1 January 2018 (UTC)
mw.config
is always available, and contains lots of other useful stuff – see mw:mw.config. Other mw modules, detailed at mw:ResourceLoader/Core modules, aren't necessarily available unless you load them – see mw:ResourceLoader/Migration_guide_(users)#mw.loader for further details. - Evad37 14:10, 1 January 2018 (UTC)