Thành viên:NgocAnMaster/LinkReport.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.

/** <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:NgocAnMaster/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
**/
/* jshint esversion: 6, maxerr: 9999 *//* globals $, mw */

mw.loader.using('jquery.ui').then(function() {
	if (mw.config.get('wgDBname') !== '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'
	);

	var ad = ' ([[User:NgocAnMaster/LinkReport.js|LinkReport]])'; // Quảng bá
	var notify = function(content, title, type) {
		mw.notify(content, {
			type, title,
			tag: 'linkreport',
			autoHide: true,
			autoHideSeconds: 5
		});
	};
	
	$('#ca-linkreport').click(function(e) {
		e.preventDefault();
		
		var css = `
			#linkreport input {
				flex-grow: 2;
				padding: 0.5em;
				-webkit-transition: border-color 100ms, box-shadow 100ms;
				transition: border-color 100ms, box-shadow 100ms;
			}
			#linkreport input:focus, #linkreport input:active {
				outline: 0;
				border-color: #3366CC;
				box-shadow: inset 0 0 0 1px #3366CC;
			}
			
			#linkreport {
				display: flex;
				flex-direction: column;
			}
			.linkreport-fieldset {
				display: flex;
				flex-direction: column;
			}
			.linkreport-fieldset > * {
				margin: 0.75em 0.3em 0;
			}
			.linkreport-labels {
				display: block;
				align-self: flex-start;
				font-size: 1.25em;
			}
			#linkreport-links {
				display: flex;
				flex-direction: column;
				gap: 0.3em;
			}
			.linkreport-link-lines {
				display: flex;
				gap: 0.3em;
			}
			.linkreport-link {
				flex-grow: 10 !important;
			}
			.linkreport-remove {
				margin: 0 !important;
			}
			#linkreport-button {
				display: flex;
				flex-direction: row-reverse;
			}
		`; // :focus/:active copied from OO.UI core.
		try {
			mw.util.addCSS(css);
		} catch (ignore) {
			mw.loader.addStyleTag(css);
		}
		
		var $line = $('<div>').attr('class', 'linkreport-link-lines').append(
			$('<input>').attr({
				'type': 'text',
				'class': 'linkreport-link',
				'placeholder': 'Liên kết'
			}),
			$('<input>').attr({
				'type': 'text',
				'class': 'linkreport-user',
				'placeholder': 'Người dùng'
			}),
			$('<button>').attr('class', 'linkreport-remove').text('Xoá').button()
		);
		$('<div>').attr('id', 'linkreport').append(
			$('<div>').attr('class', 'linkreport-fieldset').append(
				$('<label>').attr({
					'for': 'linkreport-links',
					'class': 'linkreport-labels'
				}).text('Liên kết cần báo cáo:'),
				$('<div>').attr('id', 'linkreport-links').append($line.clone()),
				$('<div>').attr('id', 'linkreport-button').append(
					$('<button>').attr('id', 'linkreport-add').text('Dòng mới').button()
				)
			),
			$('<div>').attr('class', 'linkreport-fieldset').append(
				$('<label>').attr({
					'for': 'linkreport-note',
					'class': 'linkreport-labels'
				}).text('Ghi chú (tuỳ chọn):'),
				$('<input>').attr({
					'type': 'text',
					'id': 'linkreport-note'
				})
			)
		).appendTo(document.body);
		
		$('#linkreport').dialog({
			autoOpen: true,
			width: 500,
			minWidth: 500,
			height: 400,
			minHeight: 400,
			title: 'LinkReport.js',
			buttons: [
				{
					text: 'Báo cáo',
					click: function() {
						var note = $('#linkreport-note').val().trim();
						var links = new Map();
						$('.linkreport-link-lines').each(function() {
							var link = $(this).find('.linkreport-link').val();
							var user = $(this).find('.linkreport-user').val();
							if (link.length > 5 && !links.has(link)) {
								links.set(link, user);
							}
						});
						
						if (links.size) {
							$('#linkreport').dialog('destroy').remove();
							notify('Đang gửi báo cáo...');
							
							(new mw.Api()).postWithToken('csrf', {
								action: 'edit',
								title: 'Wikipedia:Tin nhắn cho bảo quản viên',
								nocreate: true,
								appendtext: [
									'', '',
									'== Báo cáo liên kết rác ==',
									[...links].map(
										([link, user]) => `* {{BlockedExternalDomainsReport|${user ? `${link}|user=${user}` : link}}}`
									).join('\n'),
									'',
									`${note}${note.endsWith('.') ? ' ~~~~' : note ? '. ~~~~' : '~~~~'}`
								].join('\n'),
								summary: 'Báo cáo liên kết rác.' + ad,
								format: 'json',
								formatversion: 2
							}).done(function() {
								notify(
									'Đã báo cáo lên Tin nhắn cho bảo quan viên. Vui lòng kiểm tra lại nếu cần thiết, và cân nhắc xem thêm các yêu cầu tại trang Wikipedia:Yêu cầu thay đổi danh sách đen về spam.',
									'Báo cáo thành công', 'success'
								);
							}).fail(function(error, response) {
								notify(
									`${error}: ${response.error.info}`,
									`Có lỗi xảy ra`, 'error'
								);
							});
						} else {
							notify(
								'Bạn không nhập liên kết hoặc không có liên kết nào dài hơn 5 ký tự.',
								'Báo cáo thất bại', 'warn'
							);
						}
					}
				}
			]
		});
		
		$('#linkreport').on('click', '.linkreport-remove', function() {
			$(this).parent().remove();
		});
		$('#linkreport-add').click(function() {
			$('#linkreport-links').append($line.clone());
		});
	});
});
// </nowiki>