User:DannyS712 test/page.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//<nowiki>
var pg_config = {
	name: '[[User:DannyS712/page|page.js]]',
	version: 1.0,
	debug: false,
	manual: false
};
mw.loader.using( 'mediawiki.util', function () {
    $(document).ready( function () {
    	if (pg_config.manual) {
	        var link = mw.util.addPortletLink( 'p-cactions', null, 'Get page', 'ca-get-page', 'get the content of this page'); 
	        $( link ).click( function ( event ) {
	            event.preventDefault();
	            get_page( mw.config.get( 'wgPageName' ) );
	        } );
    	}
    } );
} );
var scriptUrl = mw.config.get( 'wgScriptPath' ) + '/api.php';
function get_page( name ){
    var page_to_get = {
        action: 'query',
        titles: name,
        prop: 'revisions',
        rvprop: 'content',
        format: 'json',
        formatversion: 2
    };
    var result = null;
	$.ajax({
		url: scriptUrl,
		type: 'get',
		data: page_to_get,
		dataType: 'json',
		async: false,
		success: function(page) {
			if (pg_config.debug) console.log( page );
			if (page && page.query && page.query.pages && page.query.pages["0"] && page.query.pages["0"].revisions && page.query.pages["0"].revisions["0"] && page.query.pages["0"].revisions["0"].content)
		    	result = page.query.pages["0"].revisions["0"].content;
		    else result = false;
	    	if (pg_config.debug) console.log( result );
		} 
	});
	return result;
}
function set_page ( page, new_content, pg_summary, isMinor=false ){
	console.log( page, new_content );
    var to_send = {
        action: 'edit',
        title: page,
        text: new_content,
        summary: pg_summary,
        token: mw.user.tokens.get( 'csrfToken' )
    };
    if (isMinor) to_send.minor = true;
    else to_send.notminor = true;
    console.log( to_send );
    $.when(
        $.post( scriptUrl, to_send, function( response ){ } )
    ).done( function() {
		//location.reload();
    } );
}
//</nowiki>