Last active
May 25, 2016 00:00
-
-
Save catrope/a89c136ec53b3a134fe73436a686047b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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