User:DannyS712/AfD.js

From Wikipedia, the free encyclopedia
Template:Script doc autoNote: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: hold down the Ctrl key and click the Refresh or Reload button. Firefox: hold down the Shift key while clicking Reload (or press Ctrl-Shift-R). Google Chrome and Safari users can just click the Reload button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*  _____________________________________________________________________________
 * |                                                                             |
 * |                    === WARNING: GLOBAL GADGET FILE ===                      |
 * |                  Changes to this page affect many users.                    |
 * | Please discuss changes on the talk page or on [[WT:Gadget]] before editing. |
 * |_____________________________________________________________________________|
 *
 * Easily close an AfD discussion
 * Revision: 1.0
 * Author: [[:w:en:User:DannyS712]]
 */
//<nowiki>
$(function (){

//Script-wide configuration information
var Close_AfD_config = {
	name: '[[:w:en:User:DannyS712/Easy AfD.js|Easy AfD]]',
	version: 1.0,
	//Only change the debug statements, not the name or version
	debug: true,
	debug_all: true
};

//Site configuration. The first 4 parameters set up the option to activate the script.
var Close_AfD_settings = {
	portlet_location: 'p-cactions',
	portlet_text: 'Close AfD',
	portlet_id: 'ca-closeAfD',
	portlet_tooltip: 'Close AfD',
	top_template: 'Tt',
	bottom_template: 'Ta',
	confirmation: 'You are now closing this discussion. What is the result? (Press cancel to cancel the close)',
	summary: 'Close discussion',
	help_page: 'Wikipedia:Articles for Deletion'
};

//DO NOT CHANGE ANYTHING BELOW THIS LINE
var Close_AfD_page = {};
var scriptUrl = mw.config.get( 'wgScriptPath' ) + '/api.php';
var summary_post = ' - ' + Close_AfD_config.name + ' (v. ' + Close_AfD_config.version + ') ([[' + Close_AfD_settings.help_page + ']])';

mw.loader.using( 'mediawiki.util', function () {
    $(document).ready( function () { 
    	mw.util.addPortletLink ( Close_AfD_settings.portlet_location, 'javascript:void(0)', Close_AfD_settings.portlet_text, Close_AfD_settings.portlet_id, Close_AfD_settings.portlet_tooltip);
    	$('#' + Close_AfD_settings.portlet_id).on('click', function() {
        	Close_AfD();
    	} );
    } );
} );
function Close_AfD(){
	var page = mw.config.get('wgPageName').replace(/_/g, ' ');
	var index = page.indexOf( 'Wikipédia:Törlésre javasolt lapok/' );
	console.log( page, 'Wikipédia:Törlésre javasolt lapok/', index );
	if ( index > -1 || true ) {
		var reason = prompt( Close_AfD_settings.confirmation );
		if (reason && reason !== ''){
			var top = '{{' + Close_AfD_settings.top_template + '}}\n\n' + reason + ' --~~~~\n\n';
			var bottom = '\n\n{{' + Close_AfD_settings.bottom_template + '}}';
			var new_content = top + get_page( page ) + bottom;
			set_page( page, new_content, Close_AfD_settings.sumamry + summary_post);
		}
	}
}
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 (Close_AfD_config.debug_all) console.log( page );
	    	result = page.query.pages["0"].revisions["0"].content;
	    	if (Close_AfD_config.debug_all) console.log( result );
		} 
	});
	return result;
}
function set_page ( page, new_content, pg_summary ){
	if (Close_AfD_config.debug_all) console.log( page, new_content );
    var to_send = {
        action: 'edit',
        title: page,
        text: new_content,
        summary: pg_summary,
        token: mw.user.tokens.get( 'editToken' )
    };
    if (Close_AfD_config.debug) console.log( to_send );
    $.when(
        $.post( scriptUrl, to_send, function( response ){ } )
    ).done( function() { location.reload(); } );
}
});
//</nowiki>