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.
/*Test script by TheDJFor processing list items:to replace each en dash with XXX*/vari=0;i++;// This runs immediately.// It is safe to do things like this, UNLESS they need to change something in the page.// You CAN use it to do data processing, start requests to the api etc.// A anonymous function block wrapped inside $()$(function(){// do stuff to change the page});// $() is an alias for jQuery(), the main function of the jQuery library,// which is always available for script writers on Wikipedia// When used like this, it is a shortcut for $( document ).ready(), and it will// execute your anonymous function as soon as the page is ready to be// manipulated by JS, or immediately if it is ready now.// See also: http://api.jquery.com/jquery/#jQuery3// Attach a function to be run once the 'wikipage.content' part of a page is availablemw.hook('wikpage.content').add(function($content){// Like document.ready, this will run once that part of the page is// ready/updated.// $content is a jQuery list of DOM elements, that has just become available.// See also: http://api.jquery.com/jquery/});// This is used by many scripts in MediaWiki, because it also works after// saving with Visual Editor, or when previewing with Live preview tools etc.// There are several such named hooks like:// wikipage.diff, wikipage.editform, wikipage.categories// mw, an alias for mediaWiki, is another javascript libary that is always// to MediaWiki and Wikipedia scripters.// See also: https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw// For the hook specific part of this library, which we use above, see:// https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.hook// Note that large parts of this mw library are not ready for usage by default,// and you might have to load some parts using mw.loader, before they can be used.// For loading dependencies and guaranteeing order between dependencies,// use ResourceLoader.// See also: https://www.mediawiki.org/wiki/ResourceLoader/Developing_with_ResourceLoader#Client-side_.28dynamically.29// Example if(mw.config.get('wgAction')==='view'){mw.hook('wikipage.content').add(function($contents){// Get all the <li> elements from $contents, but skip those with a class or ID,// because they might have special functionality that you don't want to break.// We generally avoid things like this, because they will break easily// Wikipedia is so diverse and big, that to do anything,// your content really needs a class or ID.varlistItems=$contents.find('li:not([class]):not([id])');// Iterate over each of the <li> itemslistItems.each(function(index,li){// This part is complicated, because we need to look at text// and text is not structured. Get the li item, and wrap it with jquery// Then get all the direct children nodesvarnodes=$(li).contents();varmarker=false;varul=false;for(vari=0;i<nodes.length;i++){// We need to find text nodes, that have our - marker.if(nodes[i].nodeType===Node.TEXT_NODE&&nodes[i].textContent.indexOf(' – ')>=0){// We found the node which contains our markermarker=i;break;}}// Check to see if the last node is an UL, so we don't break nestingif(nodes[nodes.length-1].tagName==="UL"){ul=nodes[nodes.length-1];}// only useful if it's the second element or laterif(marker>0){// Use jquery to create a new span// We use span, because it is an inline element.// We give it a class so we can find it back later// This element is already part of the document, but it is not attached to anything visible yet.varwrapper=$('<span>').addClass('myscript-wrapper');// Move the node with our marker, and all nodes that follow it into this new wrapper.// This removes them from the original <li>wrapper.append(nodes.slice(marker,ul?nodes.length-1:nodes.length));// Append the wrapper to our original list item to make the wrapper visible.$(li).append(wrapper);if(ul){$(li).append(ul);}}});// Now we have structure, and we can apply our manipulations and functionality// We simply hide all our elements$('.myscript-wrapper').hide();// You would now add controls to show them etc. etc.});}/*//List item processor//Based on a script by Writ Keeper//This selector grabs all of the <li> elements that are within #mw-content-text, and then does something for each of them$("#mw-content-text li").each( function (ind, el) { //first, grab the text from the current li element; we want only the top level text, so filter out all of the other stuff. var currentText = $(el).contents().filter(function(){return this.nodeType === 3; }); //Process only if there's something to process if(currentText.text().length > 0) { //replace the character var newText = currentText.text().replace(/–/g,'XXX'); //reinsert the updated string back into the DOM currentText.replaceWith(newText); }});*/