Jump to content

User:DannyS712/FPC voter.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
// Install the newest version with:
// <code><nowiki>		{{subst:Iusc|User:DannyS712/FPC voter.js}}																	</nowiki></code>
// or with
// <code><nowiki>		importScript( 'User:DannyS712/FPC voter.js' ); // Backlink: [[User:DannyS712/FPC voter.js]] 				</nowiki></code> 
//
// If forking this script, please note my contributions / give me credit
//<nowiki>
$(function (){
var FPC_config = {
	name: '[[User:DannyS712/FPC voter|FPC voter]]',
	version: 1.0,
	debug: false
};
var FPC_summary = 'Vote with ' + FPC_config.name + ' (version ' + FPC_config.version + ')';
var scriptUrl = mw.config.get( 'wgScriptPath' ) + '/api.php';

mw.loader.using( 'mediawiki.util', function () {
    $(document).ready( function () { 
    	if (mw.config.get('wgTitle').includes('Featured picture candidates')) {
	    	mw.util.addPortletLink ( 'p-cactions', 'javascript:void(0)', 'FPC voter', 'ca-FPC-vote', 'vote');
    		$('#ca-FPC-vote').on('click', function() {
        		FPC();
    		} );
    	}
    } );
} );
function FPC(){
	var vote = prompt("Please enter your vote (not including \'''bolding\''')", "Support");
	var reason = prompt("Please enter your rational (optional)", "");
	
	var not_vote = "*'''" + vote + "''' – ";
	
	if (reason !== null && reason !== "") {
		not_vote = not_vote + reason + ' ';
	}
	not_vote = not_vote + '~~~~';
	console.log(not_vote);
	
	var page = mw.config.get( 'wgPageName' );
	var content = get_page( page );
	if (FPC_config.debug) console.log( content );
	var comment = "<!-- additional votes go above this line  -->";
	var with_comment = not_vote + '\n' + comment;
	var new_content = content.replace( comment, with_comment);
	if (FPC_config.debug) console.log( new_content );
	set_page( page, new_content );
}
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 (FPC_config.debug) console.log( page );
			result = page.query.pages["0"].revisions["0"].content;
		    if (FPC_config.debug) console.log( result );
		} 
	});
	return result;
}
function set_page ( page, new_content){
	console.log( page, new_content );
    var to_send = {
        action: 'edit',
        title: page,
        text: new_content,
        summary: FPC_summary,
        token: mw.user.tokens.get( 'csrfToken' )
    };
    console.log( to_send );
    $.when(
        $.post( scriptUrl, to_send, function( response ){ } )
    ).done( function() {
		location.reload();
    } );
}
});
//</nowiki>