Wikipedia:User scripts/Guide: Difference between revisions
Content deleted Content added
→Your first script: update. resolve {{Why}} tag |
update |
||
Line 106:
</syntaxhighlight>
Save this to your ''User:YourUserName/common.
== Built-in scripts ==
Line 166:
=== Text editors ===
You can use anything from a simple [[text editor]], to a more feature-packed [[code editor]] or [[Integrated development environment|IDE]]. Here are some
* Windows
Line 453 ⟶ 446:
One way to coordinate this is use the [https://doc.wikimedia.org/mediawiki-core/master/js/Hooks.html mw.hook] interface. Perhaps the other script sends a <code>[[#mw.hook('wikipage.content').add(...)|wikipage.content]]</code> event when it is done, or can be modified to do so (or you can ask the maintainer).
Another way to avoid this is to use a [https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver MutationObserver].
}</syntaxhighlight>▼
=== User settings ===
Line 477 ⟶ 466:
Your code here.
//</nowiki></syntaxhighlight>If you need to print <nowiki> or </nowiki> tags within your user script, use a trick such as <code>const tag = '</' + 'nowiki>';</code> to keep from messing up the nowiki tag on line 1 and on the last line.
=== Function scope ===
Line 487 ⟶ 476:
$(function(){/* main code here */});</syntaxhighlight>
What if another of your user scripts also declares a <code><nowiki>submitEdit()</nowiki></code> function
<syntaxhighlight lang="javascript">$(function(){
Line 496 ⟶ 485:
== Ajax ==
[[Ajax (programming)|AJAX]] (''asynchronous JavaScript and XML'') is a popular name for a web programming technique that queries the server or fetches content without reloading the entire page. This is great for API requests. We do not have access to the SQL database in front end code, so the [[mw:Action_API|MediaWiki action API]] (or one of the [[mw:REST_API|other APIs]]) is the main way we retrieve data.
=== Basic examples ===
==== mediawiki.api ====
MediaWiki provides some modules with helper functions facilitating the use of its API. The main modules available are
* [https://doc.wikimedia.org/mediawiki-core/master/js/mw.Api.html mediawiki.api]
Line 514 ⟶ 496:
Be sure to follow the [[meta:User-Agent policy|user agent policy]] by setting a user agent header (see code there). See also [[mw:API:Etiquette]].
Fetching a page content can be done using [[GET (HTTP)|GET]].▼
$.ajax({▼
url: mw.util.getUrl( 'Wikipedia:Sandbox' )▼
})▼
.then(function( data ) {▼
alert( 'The remote page contains:\n' + data );▼
})▼
.catch(function() {▼
alert( 'The ajax request failed.' );▼
});▼
==== Get the wikitext of a page ====
===== Using module <code>mediawiki.api</code>=====
Note: make sure to add <code>mediawiki.api</code> to your dependencies!
Line 561 ⟶ 528:
</syntaxhighlight>
===== Using
<syntaxhighlight lang="javascript">
$.getJSON(
Line 588 ⟶ 555:
</syntaxhighlight>
====
▲Fetching a page content can be done using jQuery <code>$.ajax</code>, which does an HTTP [[GET (HTTP)|GET]] request.<syntaxhighlight lang="javascript">
▲$.ajax({
▲ url: mw.util.getUrl( 'Wikipedia:Sandbox' )
▲})
▲.then(function( data ) {
▲ alert( 'The remote page contains:\n' + data );
▲})
▲.catch(function() {
▲ alert( 'The ajax request failed.' );
▲});
==== Edit a page and other common actions ====
Scripts can perform common actions (like editing, protection, blocking, deletion, etc.) through the [{{SERVER}}/w/api.php API]. These actions require an edit token, valid for any action during the same session. (However, you should get a new token for different tasks in case this changes in the future.)
Line 686 ⟶ 666:
== Working with CSS ==
Some user scripts also use some CSS code, or even are built with CSS only. Then you need to code and test CSS code. That can be done
=== Loading a localhost file ===
<syntaxhighlight lang="css">
@import "http://localhost/wikipediatest.css";
|