Wikipedia:WikiProject User scripts/Guide/Ajax: Difference between revisions

Content deleted Content added
remove data sources.... don't think it's needed...
Get the wikitext of a page: -superfluous " action: 'query', "; +formatting
Line 31:
=== Get the wikitext of a page ===
 
;==== Using module <code>mediawiki.api</code>====
Note: (make sure to add this"mediawiki.api" to your dependencies!)
<source lang="javascript">
function okdoSomethingWithText( wikitext ) {
/* .. */
alert( 'The wikitext of the page is:\n\n' + wikitext );
}
function errdoSomethingInCaseOfError () {
/* .. */
console.log( 'err' );
Line 43 ⟶ 44:
var api = new mw.Api();
api.get( {
action: 'query',
prop: 'revisions',
rvprop: 'content',
Line 56:
rv = pg && pg.revisions;
if ( rv && rv[0] && rv[0]['*'] ) {
okdoSomethingWithText( rv[0]['*'] );
}
},
err: doSomethingInCaseOfError
err: err
} );
</source>
 
;==== Using plain jQuery: ====
<source lang="javascript">
$.getJSON(
Line 73:
for ( page in data.query.pages ) {
wikitext = data.query.pages[page].revisions[0]['*'];
okdoSomethingWithText( wikitext );
}
} catch ( e ) {
doSomethingInCaseOfError();
err();
}
}
).fail( errdoSomethingInCaseOfError );
</source>