Thành viên:NguoiDungKhongDinhDanh/massunblock.js

Bách khoa toàn thư mở Wikipedia

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.

//Tks4Fish's mass unblocking tool
//Adapted from [[:w:en:User:Timotheus_Canens/massblock.js]], who adapted it from [[User:Animum/massdelete.js]]

/* jshint esversion: 8, esnext:false */

async function domassunblock() {
	document.getElementById("wpMassUnblockSubmit").disabled = true;
	var users = document.getElementById("wpMassUnblockUsers").value.split("\n");
	if(users.length == 0) return;
	
	var wpMassUnblockReason = document.getElementById("wpMassUnblockReason").value;
	
	var unblocked = 0;
	var failed = [];
	var error = [];
	
	for(i = 0; i < users.length; i++) {
		var user = users[i];
		if(user.length > 0) {
			let data = await new mw.Api().postWithToken("csrf", {
				"action": "unblock",
				"format": "json",
				"user": user,
				"reason": wpMassUnblockReason
			});
			if(data.unblock) { //If unblocked, update the unblocked count and the button.
				unblocked++;
				document.getElementById("wpMassUnblockSubmit").value = "(" + unblocked + ")";
			}
			else { //If not unblocked, add the title to the "failed" array and a description of the error to the "error" array.
				failed.push("Special:Unblock/" + user);
				error.push(data.error.info);
			}
		}
		if(!users[i+1]) {
			document.getElementById("wpMassUnblockSubmit").value = "(" + unblocked + ")";
			if(failed.length > 0) {
				var linkedList = "";
				
				for(x = 0; x < failed.length; x++) {
					linkedList += "<li><a href=\"" + wgScript + "?title=" + encodeURIComponent(failed[x]) + "\">" + failed[x] + "</a>: " + error[x] + "</li>"; //Links the titles in the "failed" array
				}
				document.getElementById("wpMassUnblockFailedContainer").innerHTML += '<br /><b>Failed actions:</b><ul>' + linkedList + '</ul>';
			}
		}
	}
}

function massunblockform() {
	var bodyContent = (mw.config.get("skin") == "cologneblue" ? "article" : "bodyContent");
	document.getElementsByTagName("h1")[0].textContent = "Tks4Fish's mass-unblocking tool";
	document.title = "Tks4Fish's mass-unblocking tool - Wikipedia, the free encyclopedia";
	document.getElementById(bodyContent).innerHTML = '<h3 id="siteSub">From Wikipedia, the free encyclopedia</h3><br /><br />'
		+ '<form id="wpMassUnblock" name="wpMassUnblock">'
		+ '<b>If you abuse this tool, it\'s <i>your</i> fault, not mine.</b>'
		+ '<div id="wpMassUnblockFailedContainer"></div>'
		+ '<br /><br />'
			+ 'Users to unblock (one on each line, please):<br />'
				+ '<textarea tabindex="1" accesskey="," name="wpMassUnblockUsers" id="wpMassUnblockUsers" rows="10" cols="80"></textarea>'
			+ '<br /><br /><table style="background-color:transparent">'
			+ '<tr><td>Reason:</td>'
				+ '<td><input type="text" id="wpMassUnblockReason" name="wpMassUnblockReason" maxlength="255" /></td></tr>'
			+ '<tr><td><input type="button" id="wpMassUnblockSubmit" name="wpMassUnblockSubmit" value="Unblock" onclick="domassunblock()" /></td></tr>'
		+ '</form>';
}
 
if (mw.config.get('wgNamespaceNumber') == -1 &&
	mw.config.get('wgCanonicalSpecialPageName') === 'Blankpage' &&
	mw.config.get('wgUserGroups').indexOf('sysop') !== -1 &&
	mw.config.get('wgTitle').split('/',2)[1] === 'MassUnblock') {
		massunblockform();
}