Wikipedia:User scripts/Guide: Difference between revisions

Content deleted Content added
Your first script: Why does syntax highlighting have to be disabled?
Your first script: update. resolve {{Why}} tag
Line 37:
 
=== Your first script ===
We will be writing ana independentuser modulescript by modifying your common.js, so you may want to get our [[Wikipedia:User scripts/Module template|module template]]. For the purpose of this tutorial, we will write a simple version of the [[Wikipedia:WikiProject User scripts/Scripts/Quick wikify|Quick wikify]] module, addingwhich adds the <code><nowiki>{{Wikify}}</nowiki></code> maintenance template to articlesthe top of an article when you click a link called "Wikify" in the "More" menu. To begin, change <code>MODULE_NAME</code> in the module template to "Qwikify". Your template should look like this:
 
<syntaxhighlight lang="javascript">
Line 46:
</syntaxhighlight>
 
In <code>MODULE_CODE</code>, we want to add the "Wikify" tab, so we will use the [[#Adding elements|<code>addPortletLink()</code> function]] (requiring the <code>mediawiki.util</code> module). Replace <code>MODULE_CODE</code> with a call to this function. Then we will bind an event handler so that when this link is clicked, we will call another function named <code>doQwikify()</code> that will actually execute the code. The <code>name</code> is what is shown on the tab, so set that to <code>'Wikify'</code>. Most tabs have an ID of <code>ca-''name''</code>, so set the ID to <code>'ca-wikify'</code>. The title (also known as [[mouseover]] or [[rollover]] text) should be something like <code>'Mark for wikification'</code>.

Lastly, we use jQuery's [//api.jquery.com/click/ .click()] to listen for clicks on this link, and when that happens, execute a function. After we call <code>doQwikify()</code>, it says <code>event.preventDefault()</code>. Since we clicked on a link, we need to tell the browser to prevent its default behavior (going to the URL, <code>'#'</code>). We want the page to stay right where it is at, so to prevent the browser from following the link, we prevent that and do our own custom action.
 
Altogether, your new function should look like this:
Line 82 ⟶ 84:
</syntaxhighlight>
 
And that's it! Combine it all together and it should look like this:
And that's it! Save the common.js and then do a hard refresh. Go and edit a page such as the [[Wikipedia:Sandbox|Sandbox]], and see what happens!<ref>This section originally written by [[User:Raylu|raylu]] <sup>[[User:raylu/monobook.js|my monobook.js]]</sup>
 
Thanks a ton to all the users who helped improve this tutorial!</ref> Note that syntax highlighting must be disabled for this to work.{{Why|date=January 2025}}
<syntaxhighlight lang="javascript">
// Make sure the utilities module is loaded (will only load if not already)
mw.loader.using( 'mediawiki.util', function () {
// Wait for the page to be parsed
$( document ).ready( function () {
// See the "Portlets (menus and tabs)" subsection below
var link = mw.util.addPortletLink( 'p-cactions', '#', 'Wikify', 'ca-wikify', 'Mark for wikification');
$( link ).click( function ( event ) {
event.preventDefault();
doQwikify();
} );
} );
} );
 
function doQwikify() {
document.editform.wpTextbox1.value = "{" + "{wikify}}\n\n" + document.editform.wpTextbox1.value;
document.editform.submit();
}
</syntaxhighlight>
 
Save this to your User:YourUserName/common.js page. Then go visit a page such as the [[Wikipedia:Sandbox|Sandbox]], go into the "More" menu, click "Wikify", and watch the user script add the maintenance tag for you.
 
== Built-in scripts ==