MediaWiki:Gadget-FastDelete.js

From Wikisource
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
$(document).ready(function() {
	
	if (mw.config.get('wgCanonicalNamespace') == 'Category') {
	
		deletePage = function(pageTitle) {
			mw.loader.using( 'mediawiki.api', function () {
				( new mw.Api() ).post( {
					action: 'query',
					meta: 'tokens',
					format: 'json'
				} ).done(function (data) {
					token = data.query.tokens.csrftoken;
		 
					( new mw.Api() ).post( {
						action: 'delete',
						title: pageTitle,
						token: token
					} ).done(function (data) {
						console.log('Page ' + pageTitle + ' deleted');
					} );
				} );
			} );
		};
	
		$("#mw-content-text li a:not(.new)[href^='/wiki/']").each(function() {
			title = $(this).attr('title');
			href = $(this).attr('href').replace('/wiki/', '');
			if (typeof title == 'undefined') {
				title = href;
			}
			if (typeof title != 'undefined' && title.indexOf('Special:') !== 0 && href.indexOf('/w/index.php') !== 0) {
				link = $('<a class="fastDeleteButton" style="color: red" href="javascript:void(0)" title="Delete this page" data-title="' + title.replace(/"/g, '&quot;') + '">[X]</a>').click(function() {
					deletePage(decodeURIComponent($(this).data('title').replace(/&quot;/g, '"')));
				});
				$(this).after(link).after(' ');
			}
		});
		$('.mw-category').prepend($('<a href="javascript:void(0)" style="color: red">DELETE ALL PAGES!</a>').click(function() {
			if (window.confirm('All pages with [x] will be deleted. Continue?')) {
				$('.fastDeleteButton').each(function() {
					$(this)[0].click();
				});
				location.reload();
			}
		}));
	}
});