Wikipedia:WikiProject User scripts/Guide/Ajax: Difference between revisions
Content deleted Content added
→Basic examples: Added example of how to get the wikicode of a page and links to "mediawiki.api" and "mediawiki.api.edit" modules |
|||
Line 29:
</source>
=== Get the
;Using module <code>mediawiki.api</code> (make sure to add this to your dependencies!)
<source lang="javascript">
function ok(wikitext) {
/* .. */
alert( 'The wikitext of the page is:\n\n' + wikitext );
}
function err () {
/* .. */
console.log( 'err' );
}
var api = new mw.Api();
api.get( {
Line 46 ⟶ 56:
rv = pg && pg.revisions;
if ( rv && rv[0] && rv[0]['*'] ) {
}
}
} );
</source>
;Using plain jQuery:
<source lang="javascript">
$.getJSON(
mw.util.wikiScript('api'),
'format=json&action=query&prop=revisions&rvprop=content&rvlimit=1&titles=Wikipedia:Sandbox',
function (data) {
var page, wikitext;
try {
for ( page in data.query.pages ) {
wikitext = data.query.pages[page].revisions[0]['*'];
ok( wikitext );
}
} catch ( e ) {
err();
}
}
).fail( err );
===Edit a page and other common actions===
|