Thành viên:Như Gây Mê/BlankedThePage.js

Chú ý: Sau khi lưu thay đổi trang, bạn phải xóa bộ nhớ đệm của trình duyệt để nhìn thấy các thay đổi. Google Chrome, Firefox, Internet ExplorerSafari: Giữ phím ⇧ Shift và nhấn nút Reload/Tải lại trên thanh công cụ của trình duyệt. Để biết chi tiết và hướng dẫn cho các trình duyệt khác, xem Trợ giúp:Xóa bộ nhớ đệm.

// 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 );
				}
			} );
		}
	);
} );