User:NhacNy2412/BlankedThePage.js

From Meta, a Wikimedia project coordination wiki

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.
// Install with:
// <code><nowiki>		importScript ('User:NhacNy2412/BlankedThePage.js'); // Backlink: [[m:User:NhacNy2412/BlankedThePage.js]]								</nowiki></code>
// or with
// <code><nowiki>		mw.loader.load('//meta.wikimedia.org/w/index.php?title=User:NhacNy2412/BlankedThePage.js&action=raw&ctype=text/javascript');			</nowiki></code> 
//
// If forking this script, please note my contributions / give me credit
$(() => {
const BlankedThePage = {};
window.BlankedThePage = BlankedThePage;
BlankedThePage.config = {
	name: '[[m:User:NhacNy2412/BlankedThePage.js|BlankedPage.js]]',
	debug: false
};

BlankedThePage.summary =	((mw.config.get('wgUserLanguage') == 'vi') 
								? "Tẩy trống trang (sử dụng "
								: "Blanked the page (using " 
							) + BlankedThePage.config.name + ")";
	
BlankedThePage.run = function () {
	if ( BlankedThePage.config.debug ) {
		console.log ( BlankedThePage.summary );
	}
	
	var api = new mw.Api();
	api.get( {
		action: 'query',
		titles: mw.config.get( 'wgPageName' ),
		prop: 'revisions',
		rvprop: 'content',
		rvslots: 'main',
		formatversion: 2
	} ).done(
		function ( response ) {
			console.log( response );
			var text = response.query.pages[0].revisions[0].slots.main.content;
			text = '';

			api.postWithEditToken( {
				action: 'edit',
				title: mw.config.get( 'wgPageName' ),
				text: '',
				summary: BlankedThePage.summary
			} ).done(
				function() {
					location.reload();
				}
			);
		}
	);
};

});

$(document).ready ( () => {
	mw.loader.using(
		[ 'mediawiki.util' ],
		function () {
			var name = (mw.config.get('wgUserLanguage') == 'vi') ? 'Tẩy trống trang' : 'Blanked the page';
			var link = mw.util.addPortletLink( 'p-cactions', '#', name, 'ca-btp'); 
			$(link).click (function (event) {
				if (window.confirm((mw.config.get('wgUserLanguage') == 'vi') 
									? 'Bạn có chắc chắc muốn tẩy trống trang?'
									: 'Are you sure you want to blank this page?'))
				{
					event.preventDefault();
					mw.loader.using('mediawiki.api', window.BlankedThePage.run );
				}
			} );
		}
	);
} );