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:45, 30 January 2019 (copy remind me). 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.
RemindMe_config = {
	name: "[[User:DannyS712/Remind Me.js|Remind me]]",
	version: 1.0,
	debug: true
};

var user = "";

mw.loader.using( 'mediawiki.util', function () {
    importScript('User:DannyS712 test/append.js');
    $(document).ready( function () { 
        var link = mw.util.addPortletLink( 'p-cactions', null, 'Remind me', 'ca-add-reminder', 'Reminder to check this page'); 
        $( link ).click( function ( event ) {
            event.preventDefault();
            add_reminder();
        } );
    } );
} );
function add_reminder(){
	user = mw.config.get( 'wgUserName' );
	var location = "User:" + user + "/remind.json";
	console.log( location );
	var reminder_text = prompt("What would you like the reminder to say?", "Check this page");
	var reminder_wait = parseInt(prompt("How many days from now would you like to be reminded?", "10"), 10);
	var new_reminder = {
		page: pretty_page(mw.config.get( 'wgPageName' )),
		time: (new Date()).getTime(),
		wait: reminder_wait*86400000,
		text: reminder_text
	};
	add_the_reminder ( location, new_reminder );
}
function add_the_reminder ( location, new_reminder ){
	if (RemindMe_config.debug) console.log( location );
	var get_page = {
        action: 'query',
        titles: location,
        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 (RemindMe_config.debug) console.log ( got_page );
    	if (RemindMe_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 (RemindMe_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 (RemindMe_config.debug) console.log( "New: " );
    	if (RemindMe_config.debug) console.log( arr_JSONed );
    	var new_JSON = JSON.stringify( arr_JSONed, null, 2 );
    	if (RemindMe_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.' );
	    } } );
}
function pretty_page ( page_name ){
	var new_name = page_name.replace( '_', ' ' );
	return new_name;
}