Thành viên:Hide on Rosé/WarningDialog.js

Từ điển mở Wiktionary

Chú ý: Sau khi lưu trang này, phải xóa bộ nhớ đệm (cache) của trình duyệt để những thay đổi hiện ra

  • Firefox / Safari: Nhấn giữ phím Shift trong khi nhấn Tải lại (Reload), hoặc nhấn tổ hợp Ctrl-F5 hay Ctrl-R (⌘R trên Mac)
  • Google Chrome: Nhấn tổ hợp Ctrl-Shift-R (⇧⌘R trên Mac)
  • Internet Explorer / Edge: Nhấn giữ phím Ctrl trong khi nhấn Làm tươi (Refresh), hoặc nhấn tổ hợp Ctrl-F5
  • Opera: Nhấn tổ hợp Ctrl-F5.
/**
 * Công cụ hỗ trợ cảnh báo/báo cáo phá hoại
 * Dựa trên [[:meta:User:NguoiDungKhongDinhDanh/WarningDialog.js]]
*
 * See also: [[User:NguoiDungKhongDinhDanh/RfDHelper.js]]
 * 
 * For attribution: [[User:1234qwer1234qwer4/warnings.js]] 
**/
// <nowiki>

$(function() {
	if ((![2, 3].includes(mw.config.get('wgNamespaceNumber')) &&
		mw.config.get('wgCanonicalSpecialPageName') !== 'Contributions') ||
		!mw.config.get('wgRelevantUserName') ||
		mw.config.get('wgRelevantUserName') === mw.config.get('wgUserName') || // [[Special:Diff/23314073]]
		![undefined, ''].includes((new mw.Uri()).query.noWD) 
	) {
		return;
	}
		
	mw.util.addPortletLink('p-cactions', '', 'Cảnh báo/Báo cáo', 'ca-warningdialog', 'Đang khởi tạo WarningDialog');
	
	$('#ca-warningdialog').click(function(e) {
		e.preventDefault();
		if ($('#warningdialog').length) {
			$('#warningdialog').dialog('open');
			return;
		}
		
		var api = new mw.Api();
		
		var u = mw.config.get('wgRelevantUserName');
		var ad = ' ([[User:Tryvix1509/WarningDialog.js|WD]])';
		var type = [
			{
				h: '',
				t: 'welcome',
				s: 'Hoan nghênh đến với Wiktionary!',
				l: 'Chào mừng thành viên',
				c: true,
				i: true
			},
			{
				l: 'Báo cáo tới WT:TNCBQV',
				c: true,
				n: 'report',
				p: function() {
					$('#warningdialog-report').parents('li').append(
						$('<div>').attr({
							class: 'warningdialog-fieldset',
							id: 'warningdialog-fieldset-report'
						}).css({
							'display': 'flex',
							'flex-direction': 'column-reverse',
							'justify-content': 'space-between'
						}).append(
							$('<input>').attr({
								type: 'text',
								placeholder: 'Vui lòng nhập lý do...',
								class: 'warningdialog-input',
								id: 'warningdialog-input-report'
							}).css({
								'flex-grow': 2,
								'margin': '0.2em',
								'padding': '0.2em'
							}),
							$('<select>').attr({
								class: 'warningdialog-select',
								id: 'warningdialog-select-report'
							}).css({
								'margin': '0.2em',
								'padding': '0.2em'
							}).append(
								$('<option>').attr('value', 'Các hành động chỉ ra một tài khoản chỉ phá hoại.').prop('selected', true).text('Phá hoại'),
								$('<option>').attr('value', 'Có nhiều sửa đổi gây hại.').text('Sửa đổi gây hại'),
								$('<option>').attr('value', 'Tẩy trống trang, xóa nội dung.').text('Tẩy trống trang, xóa nội dung'),
     							$('<option>').attr('value', 'Tài khoản chỉ sử dụng cho các mục đích quảng cáo.').text('Quảng cáo'),
								$('<option>').attr('value', 'Tài khoản chỉ troll, đùa giỡn.').text('Troll'),
								$('<option>').attr('value', 'Spam / spambot.').text('Spam / spambot')
							)
						)
					);
					
					$('#warningdialog').on('change', '#warningdialog-report', function() {
						$('#warningdialog-fieldset-report').toggle($(this).prop('checked'));
					});
					$('#warningdialog-report').trigger('change');
					
					$('#warningdialog-select-report').on('change', function() {
						$('#warningdialog-input-report').val($(this).val());
					}).trigger('change');
				},
				u: true,
				f: function() {
					if (!$('#warningdialog-input-report').val().trim()) {
						$('#warningdialog-reportmessage').css({
							'color': '#D33',
							'font-weight': 'bold'
						}).text('Không có lý do được cung cấp. Đang hủy bỏ');
					}
					$('#warningdialog').append(
						$('<p>').attr('id', 'warningdialog-reportmessage').css({
							'padding': '1em',
							'font-size': '1.25em'
						}).text('Đang báo cáo...')
					);
					api.get({
						action: 'parse',
						page: 'Wiktionary:Tin nhắn cho bảo quản viên',
						prop: ['sections'],
						format: 'json',
						formatversion: 2
					}).done(function(response) {
						var sections = response.parse.sections;
						for (let section of sections) {
							if (section.line.replace(/_/g, ' ').toLowerCase() === 'Báo cáo ' + u) {
								$('#warningdialog-reportmessage').css({
									'color': '#D33',
									'font-weight': 'bold'
								}).text('Thành viên này đã được báo cáo trước đó.');
								return;
							}
						}
						api.postWithToken('csrf', {
							action: 'edit',
							title: 'Wiktionary:Tin nhắn cho bảo quản viên',
							section: 'new',
							sectiontitle: 'Báo cáo [[Special:Contributions/' + u + '|' + u + ']]',
							appendtext: '* {{Vandal|' + u + '}}\n' + $('#warningdialog-input-report').val().replace(/\s*~{4}$/, '') + ' – ~~~~',
							summary: 'Đang báo cáo [[Special:Contributions/' + u + '|' + u + ']]' + ad,
							nocreate: true,
							format: 'json',
							formatversion: 2
						}).done(function(response) {
							$('#warningdialog-reportmessage').css({
								'color': '#006400',
								'font-weight': 'bold'
							}).text('Báo cáo thành công.');
						}).fail(function(error, response) {
							$('#warningdialog-reportmessage').css({
								'color': '#D33',
								'font-weight': 'bold'
							}).text('Báo cáo không thành công. Lỗi API: ' + response.error.info);
						});
					}).fail(function(error, response) {
						$('#warningdialog-reportmessage').css({
							'color': '#D33',
							'font-weight': 'bold'
						}).text('Lỗi API: ' + response.error.info);
					});
				}
			},
			{
				h: 'Không cảnh báo',
				t: '',
				s: ''
			},
			{
				h: 'Phá hoại (Cấp 1)',
				t: 'Cb1',
				s: 'Lưu ý chung: Chỉnh sửa không theo quy định.'
			},
			{
				h: '',
				t: 'Cb2',
				s: 'Chú ý: Chỉnh sửa không theo quy định.',
				l: 'Phá hoại (Cấp 2)'
			},
			{
				h: '',
				t: 'Cb3',
				s: 'Cảnh báo: Phá hoại.',
				l: 'Phá hoại (Cấp 3)'
			},
			{
				h: '',
				t: 'Cb4im',
				s: 'Cảnh báo cuối cùng: Phá hoại',
				l: 'Phá hoại (Cấp 4)'
			},
			{
				h: 'Sửa đổi thử nghiệm',
				t: 'Nghịch thử',
				s: 'Cảnh báo: Vui lòng sử dụng [[Wiktionary:Chỗ thử|chỗ thử]] để thử nghiệm sửa đổi.',
				l: 'Sửa đổi thử nghiệm'
			},
			{
				h: 'Phá hoại (cấp 1) foo bar',
				t: 'bv',
				s: 'Lưu ý chung: Chỉnh sửa không theo quy định.'
			},
			{
				h: 'Quảng cáo',
				t: 'spam',
				s: 'Cảnh báo: Sử dụng Wiktionary để quảng cáo hoặc khuyến mãi.'
			},
			{
				h: 'Trang (con) thành viên sẽ sớm/đã bị xóa',
				t: 'deleted userpage',
				s: 'Cảnh báo: Trang (con) thành viên sẽ/đã bị xóa.'
			},
//			{
//				h: 'Off-topic contributions',
//				t: 'offtopic',
//				s: 'Warning: Your page was outside of Meta\'s [[Meta:Inclusion policy|scope]].'
//			},
//			{
//				h: 'Off-topic edits',
//				t: 'offtopic-edits',
//				s: 'Warning: Your edits were outside of Meta\'s [[Meta:Inclusion policy|scope]].'
//			},
			{
				h: 'Tạo lại trang đã bị xóa',
				t: 'norecreate',
				s: 'Cảnh báo: Tạo lại các trang/nội dung đã bị xóa'
			},
			{
				h: 'Tấn công cá nhân',
				t: 'attack',
				s: 'Cảnh báo: Tấn công cá nhân các biên tập viên khác.'
			}
			/* {
				h: 'Thông báo cấm',
				t: 'blocked|1=$1|2=$2|sig=~~~~',
				s: 'Bạn đã bị cấm sửa đổi trên Wiktionary.'
			} */
		];
		var getcontent = function(button) {
			var t = {};
			t.s = [];
			t.c = [];
			$('.warningdialog-checkbox').each(function() {
				if ($(this).prop('checked')) {
					var c = JSON.parse($(this).val());
					if (c.u) {
						for (let i of type) {
							if (i.l === c.l) {
								if (button !== 'preview') i.f();
							}
						}
					} else {
						t.c.push((c.h ? '== ' + c.h + ' ==\n' : '') + '{{subst:' + c.t + '}}\n' + (c.i ? '~~~~' : ''));
						t.s.push(c.s);
					}
				}
			});
			t.r = JSON.parse($('.warningdialog-radio:checked').val());
			if (t.r.h !== 'Không cảnh báo') {
				t.r = {
					c: (t.r.h ? '== ' + t.r.h + ' ==\n' : '') + '{{subst:' + t.r.t + '}}\n' + (t.r.i ? '~~~~' : ''),
					s: t.r.s
				};
			} else {
				t.r = '';
			}
			t.text = t.c.join('\n\n') + (t.c.join('') && t.r && t.r.c ? '\n\n' : '') + (t.r ? t.r.c : '');
			return t;
		};
		
		$('<div>').attr('id', 'warningdialog').append(function() {
			var l1 = $('<ul>').css({
				'flex-grow': 2,
				'flex-basis': '50%',
				'list-style-type': 'none',
				'list-style-image': 'none',
				'margin': '0.3em',
				'border': '1px solid #C8CCD1',
				'min-height': '7.5em',
				'overflow-y': 'auto',
				'padding': '1em',
				'font-size': '1.2em'
			});
			var l2 = l1.clone();
			
			for (let i of type) {
				(i.c ? l1 : l2).append(
					$('<li>').css({
						'display': 'flex',
						'flex-direction': 'column',
						'align-content': 'stretch'
					}).html(
						$('<label>').attr({
							class: 'warningdialog-label'
						}).append(
							i.c ?
								$('<input>')
								.attr({
									type: 'checkbox',
									value: JSON.stringify(i),
									class: 'warningdialog-checkbox',
									id: 'warningdialog-' + i.n
								})
							:
								$('<input>')
								.attr({
									type: 'radio',
									name: 'warningdialog-radio',
									value: JSON.stringify(i),
									class: 'warningdialog-radio',
									id: 'warningdialog-' + i.n
								})
						).append(
							$('<span>').attr({
								class: 'warningdialog-labelname',
								style: 'margin: 0 0.5em;'
							}).text(i.h || i.l)
						)
					)
				);
			}
			l2.find('.warningdialog-radio').first().prop('checked', true);
			return $('<div>').css({
				'display': 'flex'
			}).append(l1, l2);
		}).append(
			$('<div>').attr('id', 'warningdialog-preview').css({
				'display': 'none',
				'margin': '0.3em',
				'border': '1px solid #C8CCD1',
				'max-height': '30em',
				'overflow-y': 'auto',
				'padding': '1em'
			})
		).appendTo('body');
		
		type.forEach(function(i) {
			if (i.p) i.p();
		});
		
		$('#warningdialog').dialog({
			autoOpen: true,
			width: '60%',
			title: 'Đang cảnh báo ' + u,
			buttons: [
				{
					text: 'Xem trước',
					click: function() {
						if (!$('#warningdialog-preview').is(':visible')) {
							$('#warningdialog-preview').show();
						}
						$('#warningdialog-preview').html(
							$('<p>').css({
								'padding': '1em',
								'font-size': '1.25em'
							}).text('Đang tải...')
						);
						
						var t = getcontent('preview');
						var text = t.text;
						
						api.get({
							action: 'parse',
							title: 'Thảo luận Thành viên:' + u,
							text: text,
							prop: ['text'],
							pst: true,
							disablelimitreport: true,
							disableeditsection: true,
							sectionpreview: true,
							disabletoc: true,
							useskin: mw.config.get('skin'),
							contentmodel: 'wikitext',
							format: 'json',
							formatversion: 2
						}).done(function(response) {
							$('#warningdialog-preview').html(response.parse.text);
						}).fail(function(error, response) {
							console.warn(error, response);
							$('#warningdialog-preview').html(response.error.info);
						});
					}
				},
				{
					text: 'Xác nhận và cảnh báo',
					click: function() {
						$(this).parent().find('button').button('disable');
						$('#warningdialog').children().hide();
						
						var t = getcontent('submit');
						var text = t.text;
						if (!text.trim()) return;
						
						$('#warningdialog').append(
							$('<p>').attr('id', 'warningdialog-main').css({
								'padding': '1em',
								'font-size': '1.25em'
							}).text('Đang thực hiện...')
						);
						
						api.get({
							action: 'query',
							titles: ['Thảo luận Thành viên:' + u],
							prop: ['revisions'],
							rvprop: ['content'],
							rvslots: '*',
							rvlimit: 1,
							format: 'json',
							formatversion: 2
						}).done(function(res) {
							var m = res.query.pages[0].missing || (res.query.pages[0].revisions[0].slots.main.content.trim() === '');
							text = (m ? '' : '\n\n') + text;
							
							api.postWithToken('csrf', {
								action: 'edit',
								title: 'User talk:' + u,
								watchlist: window.WDwatchlist || 'preferences',
								appendtext: text,
								summary: (t.r.s || t.s[0]) + ad,
								format: 'json',
								formatversion: 2
							}).done(function(response) {
								$('#warningdialog-main').css({
									'color': '#006400',
									'font-weight': 'bold'
								}).text('Đã thực hiện.');
							}).fail(function(error, response) {
								$('#warningdialog-main').css({
									'color': '#D33',
									'font-weight': 'bold'
								}).text('Không thể gửi cảnh báo. Lỗi API: ' + response.error.info);
							});
						}).fail(function(error, response) {
							$('#warningdialog-main').css({
								'color': '#D33',
								'font-weight': 'bold'
							}).text('Lỗi API: ' + response.error.info);
						});
					}
				}
			]
		});
		
		$('#warningdialog-preview').click(function() {
			$(this).toggle();
		});
	});
});

// </nowiki>