Skip to content

Instantly share code, notes, and snippets.

@catrope
Last active May 25, 2016 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catrope/a89c136ec53b3a134fe73436a686047b to your computer and use it in GitHub Desktop.
Save catrope/a89c136ec53b3a134fe73436a686047b to your computer and use it in GitHub Desktop.
$.ajax( {
// Get this URL
url: '/api/rest_v1/page/html/' + mw.config.get( 'wgPageName' ),
// Interpret the result as HTML (as opposed to XML or JSON or something)
dataType: 'html'
} )
.done( function( html ) {
// Parse the result as HTML
var doc = new DOMParser().parseFromString( html, 'text/html' ),
// You can now use jQuery to find stuff using $( 'selector', doc )
// (you have to specify doc because $('p') finds paragraphs on the current page)
// For example, the below finds all paragraphs
$paragraphs = $( 'p', doc );
// $paragraphs is now a list of all <p> tags in doc
// Insert <b>YAY</b> before every paragraph
$paragraphs.before( '<b>YAY</b>' );
$.ajax( {
url: '/api/rest_v1/transform/html/to/wikitext/Main_Page',
method: 'POST',
dataType: 'text',
data: {
html:'<b>Yay</b>'
}
} )
.done( function( wikitext ) {
// wikitext is the result of the conversion
console.log( wikitext );
$( '#wpTextbox1' ).val( wikitext );
} );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment