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.

/** <nowiki>
 * Công cụ hỗ trợ báo cáo liên kết spam.
 * Cài đặt: Thêm dòng dưới đây vào trang common.js của bạn:
 * mw.loader.load('/wiki/User:NguoiDungKhongDinhDanh/LinkReport.js?action=raw&ctype=text/javascript');
 * 
 * Danh sách người sử dụng: //w.wiki/4rX3
 * Giấy phép: CC BY-SA 3.0
 */

var linkreportad = ' ([[User:NguoiDungKhongDinhDanh/LinkReport.js|LinkReport]])'; // Quảng bá

// Thêm liên kết trong một lần nhấp
oneclickadder = function(string, rev) {
	var sbl = 'MediaWiki:Spam-blacklist',
		edsummary = (typeof rev == 'undefined' ?
						'Thêm ' + string + ' theo yêu cầu tại [[Special:Permalink/' + rev + '|WP:TNCBQV]]'
					:
						'Thêm ' + string
					) + linkreportad,
		params1 = { // Lấy nội dung trang đã có
			action: 'query',
			prop: 'revisions',
			titles: sbl,
			rvslots: '*',
			rvprop: 'content',
			formatversion: 2
		},
		params2 = {
			action: 'edit',
			title: sbl,
			summary: edsummary,
			format: 'json'
		};
	new mw.Api().get(params1).done(function(response) {
		var blwikitext = response.query.pages[0].revisions[0].slots.main.content,
			blarray = blwikitext.split('\n'),
			thismonth = new Date().getMonth() + 1,
			thisyear = new Date().getFullYear(),
			thismonthline = 0,
			thismonthregexp = new RegExp('^# Tháng ' + thismonth + ' năm ' + thisyear + '$');
			preline = 0,
			prelineregex = new RegExp('^.*<pre>.*$'),
			blwikitextnew = '';
			
		for (let i = 0; i < blarray.length; i++) {
			if (blarray[i].match(thismonthregexp)) {
				thismonthline = i + 1;
				break;
			}
		}
		for (let i = 0; i < blarray.length; i++) {
			if (blarray[i].match(prelineregex)) {
				preline = i + 1;
				break;
			}
		}
		
		if (thismonthline > 1) { // Đã có đề mục cho tháng này, thêm liên kết vào đó
			blarray[thismonthline - 1] += '\n' + string;
		} else { // Chưa có đề mục, thêm đề mục kèm theo liên kết
			blarray[preline - 1] += '\n# Tháng ' + thismonth + ' năm ' + thisyear + '\n' + string + '\n';
		}
		blwikitextnew = blarray.join('\n');
		params2.text = blwikitextnew;
		new mw.Api().postWithToken('csrf', params2).done(function(data) {
			mw.notify('Đã thêm liên kết vào danh sách đen.', {
				title: 'Thành công.'
			});
		});
	});
};

oneclickblocker = function(user) {
	var sbl = 'MediaWiki:Spam-blacklist',
		params = {
			action: 'block',
			user: user,
			expiry: 'indefinite',
			nocreate: true,
			autoblock: true,
			noemail: true,
			allowusertalk: false,
			reblock: true,
			reason: 'Sử dụng Wikipedia với mục đích [[WP:SPAM|spam]] hoặc [[WP:KHONGQUANGCAO|quảng cáo]]',
			format: 'json'
		};
	new mw.Api().get(params).done(function(response) {
		mw.notify('Đã cấm người dùng vô hạn.', {
			title: 'Thành công!'
		});
	});
};

linkreport = function() {
	$('#ca-linkreport').click(function(e) {
		e.preventDefault();
		var link1 = prompt('Liên kết cần báo cáo:').trim(),
			link = (link1.length > 5 ? link1 : ''),
			user = prompt('Người dùng cần báo cáo (có thể bỏ qua):').trim(),
			note = prompt('Ghi chú thêm (có thể bỏ qua):').trim(),
			summary = 'Báo cáo liên kết rác: $1.' + linkreportad,
			params = {
				action: 'edit',
				title: 'Wikipedia:Tin nhắn cho bảo quản viên',
				appendtext: '\n\n== Báo cáo liên kết rác ==\n{{Spam\n| ' + link + (user.length > 0 ? ' >> ' + user : '') + '\n}}\n' +
							(note.length > 0 ? note : '') +
							(note.endsWith('.') ?
								' ~~~~'
							:
								(note.length > 0 ?
									'. ~~~~'
								:
									'~~~~'
								)
							),
				summary: summary.replace('$1', link1),
				format: 'json'
			};
		if (link1.length > 5) { // Không thể có liên kết nào ngắn hơn 5 ký tự, tính cả tên miền.
			new mw.Api().postWithToken('csrf', params).done(function(data) {
				mw.notify('Đã báo cáo thành công. Vui lòng kiểm tra lại nếu cần thiết.', {
					title: 'Đã báo cáo.',
					autoHide: true
				});
			});
		} else {
			mw.notify('Bạn không nhập liên kết hoặc có thể đã nhập sai.', {
				title: 'Huỷ báo cáo.',
				autoHide: true
			});
		}
	});
};

linkreportsysop = function() {
	$('#ca-linkreport').click(function(e) {
		e.preventDefault();
		var escapeRegex = function(string) { // Ghi công: //stackoverflow.com/questions/3561493#3561711
				return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
			},
			link1 = prompt('Thêm liên kết:').trim(),
			link = '\\b' + escapeRegex(link1).replace(/\\\\/, '\\') + '\\b',
			user = prompt('Cấm người dùng (có thể bỏ qua):').trim();
		if (link.length > 5) {
			oneclickadder(link);
		} else {
			mw.notify('Bạn không nhập liên kết hoặc có thể đã nhập sai.', {
				title: 'Huỷ báo cáo.',
				autoHide: true
			});
		}
		if (user.length > 0) {
			oneclickblock(user);
		}
	});
	
	// Tải tệp CSS
	mw.loader.load('//vi.wikipedia.org/w/index.php?title=User:NguoiDungKhongDinhDanh/LinkReport.css&action=raw&ctype=text/css', 'text/css');
	
	// Thêm một lần nhấp
	$('.linkreport-oneclickadder').each(function() {
		$(this).append(
			' <button \
				class="linkreport-addbuttons" \
				value="\\b' + $(this).attr('id').replace('linkreport-', '') + '\\b">' +
				'Thêm' +
			'</button>'
		);
	});
	$('.linkreport-addbuttons').click(function() {
		oneclickadder($(this).attr('value'), mw.config.get('wgRevisionId'));
		$(this).html('Đã thêm');
		$(this).prop('disabled', true);
	});
	
	// Cấm một lần nhấp
	$('.linkreport-oneclickblocker').each(function() {
		$(this).append(
			' <button \
				class="linkreport-blockbuttons" \
				value="' + $(this).attr('id').replace('linkreport-', '') + '">' +
				'Cấm' +
			'</button>'
		);
	});
	$('.linkreport-blockbuttons').click(function() {
		oneclickblocker($(this).attr('value'));
		$(this).html('Đã cấm');
		$(this).prop('disabled', true);
	});
};

$(document).ready(function() {
	var db = mw.config.get('wgDBname'),
		ug = mw.config.get('wgUserGroups'),
		pid = mw.config.get('wgArticleId');
	
	if (db !== 'viwiki') return; // Đề phòng có người cài vào global.js
	mw.util.addPortletLink(
		'p-cactions',
		'', 'LR',
		'ca-linkreport',
		'Hỗ trợ xử lý liên kết rác'
	);
	if (ug.indexOf('sysop') > -1 && pid === 5759) { // Dành cho bảo quản viên.
		linkreportsysop();
	} else { // Dành cho thành viên thường.
		linkreport();
	}
});
// </nowiki>