Jump to content

User:DannyS712 test/get JSON.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by DannyS712 test (talk | contribs) at 19:49, 30 January 2019 (create). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
get_JSON_config = {
	name: "[[User:DannyS712/get JSON.js|get JSON]]",
	version: 1.0,
	debug: true
};

mw.loader.using( 'mediawiki.util', function () {
    $(document).ready( function () { 
        var link = mw.util.addPortletLink( 'p-cactions', null, 'Get JSON', 'ca-get-JSON', 'get the JSON of this page'); 
        $( link ).click( function ( event ) {
            event.preventDefault();
            get_JSON( mw.config.get( 'wgPageName' ) );
        } );
    } );
} );
function get_JSON ( page ){
	if (get_JSON_config.debug) console.log( location );
	var get_page = {
        action: 'query',
        titles: page,
        prop: 'revisions',
        rvprop: 'content',
        format: 'json',
        formatversion: 2
    };
    $.get( mw.config.get( 'wgScriptPath' ) + '/api.php', get_page, function( got_page ) {
    	var text = got_page.query.pages["0"].revisions["0"].content;
    	if (get_JSON_config.debug) console.log ( got_page );
    	if (get_JSON_config.debug) console.log( "Text:", text, "End text" );
    	var JSONed = JSON.parse( text );
    	var arr_JSONed = [];
    	for (var reminder in JSONed){
    		arr_JSONed.push(JSONed[reminder]);
    	}
    	if (get_JSON_config.debug) console.log( arr_JSONed );
    });
}
/**
 * arr_JSONed.push( {page: new_reminder.page, start: new_reminder.time + new_reminder.wait, custom: new_reminder.text, id: user + "_" + new_reminder.time} );
    	if (get_JSON_config.debug) console.log( "New: " );
    	if (get_JSON_config.debug) console.log( arr_JSONed );
    	var new_JSON = JSON.stringify( arr_JSONed, null, 2 );
    	if (get_JSON_config.debug) console.log( new_JSON );
    	Remind_replace( location, new_JSON, "add a reminder for " + new_reminder.page );
function Remind_replace ( page, new_content, edit_summary ){
	$.ajax({
	    url: mw.util.wikiScript( 'api' ),
	    data: {
	        format: 'json',
	        action: 'edit',
	        title: page,
	        text: new_content,
	        summary: edit_summary,
	        token: getToken()
	    },
	    dataType: 'json',
	    type: 'POST',
	    success: function( data ) {
	        if ( data && data.edit && data.edit.result == 'Success' ) {
	            alert( 'Reminder scheduled' );
	        } else if ( data && data.error ) {
	            alert( 'Error: API returned error code "' + data.error.code + '": ' + data.error.info );
	        } else {
	            alert( 'Error: Unknown result from API.' );
	        }
	    },
	    error: function( xhr ) {
	        alert( 'Error: Request failed.' );
	    } } );
}
**/