User:Sreejithk2000/MoveDelPagetoFilePage.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
// This script is jsHint-valid

function nukeFile() {
	var params = {
			action: 'delete',
			reason: $('#wpReason').val(),
	        watchlist: 'watch',
	        title: mw.config.get('wgPageName'),
			format: 'json'
		},
		api = new mw.Api();
	
	AjaxQuickDelete.showProgress('Deleting file');
	api.postWithToken( 'csrf', params ).done( function ( data ) {
		console.log( data );
		if (data.delete) {
			window.location.reload();
		}
	} );
}
function getDeletionReason() {
	var deleteUrl = $('span#nuke').find('a').attr('href');
	var deletionAttributes = deleteUrl.split('&');
	var deletionReason = '';
	$.each(deletionAttributes, function(index) {
		if (deletionAttributes[index].match('^wpReason')) {
			var wpReason = deletionAttributes[index].substring(9, deletionAttributes[index].length);
			deletionReason = decodeURIComponent(wpReason.replace(/\+/g, " "));
		}
	});
	return deletionReason;
}

function addDeleteTableToFile() {
	var wpEditToken = mw.user.tokens.get('csrfToken');
	var formattedPageName = mw.config.get('wgCanonicalNamespace') + ":" + encodeURIComponent(mw.config.get('wgTitle').replace(/ /g, '_'));
	
	var fieldsetOpeningTag = '<fieldset><legend>Delete file</legend>';
	var fieldsetClosingTag = '</fieldset>';
	
	var tableOpeningTag = '<table id="mw-img-deleteconfirm-table" style="border-spacing: 5px">';
	var firstTr = '<tr><td class="mw-label"><label for="wpReason">Other/additional reason:</label></td><td class="mw-input"><input type="text" id="wpReason" tabindex="2" maxlength="255" name="wpReason" size="150"></td></tr>';
	var secondTr = '<tr><td></td><td class="mw-submit"><input type="submit" tabindex="4" id="mw-filedelete-submit" name="mw-filedelete-submit" value="Delete" class="cdx-button"></td></tr>';
	var tableClosingTag = '</table>';
	
    var table = tableOpeningTag + firstTr + secondTr + tableClosingTag;

	var fieldset = fieldsetOpeningTag + table + fieldsetClosingTag;
	$('#nuke').parent().append(fieldset);

	$('#wpReason').val(getDeletionReason());
	$('#mw-filedelete-submit').click(nukeFile);
}

// Only if the page already has delete tag
if ($('#nuke').length > 0 && mw.config.get('wgPageName').match(/User:CommonsDelinker/i) === null	) {
	addDeleteTableToFile();
}