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
<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:
<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 ==
|