مدیاویکی:Eliminator-vote-eligibility.js

از ویکی‌پدیا، دانشنامهٔ آزاد

نکته: برای دیدن تغییرات، ممکن است نیاز باشد که حافظهٔ نهانی مرورگر خود را پس از انتشار پاک‌سازی کنید. گوگل کروم، فایرفاکس، مایکروسافت اج و سافاری: کلید Shift را نگه دارید و روی دکمهٔ Reload در نوار ابزار مرورگر کلیک کنید. برای آگاهی از جزئیات و نحوهٔ پاک‌سازی حافظهٔ نهانی سایر مرورگرها، صفحهٔ ویکی‌پدیا:میانگیر مرورگرتان را خالی کنید را ببینید.

$(() => {
	'use strict';

	function getPageCreationTimestamp(pageName) {
		return new mw.Api().get({
			action: 'query',
			prop: 'revisions',
			titles: pageName,
			rvprop: 'timestamp',
			rvdir: 'newer',
			rvlimit: 1,
		}).then(x => +new Date(Object.values(x?.query?.pages ?? {})[0]?.revisions?.[0]?.timestamp));
	}

	function checkEligibility(usernames, timestamp) {
		return $.post('https://linkstranslator.toolforge.org/users-eligibility/', {
			usernames: usernames.join('|'),
			timestamp: timestamp,
			dbname: mw.config.get('wgDBname'),
		}).then(response => {
			const result = {};
			for (let username in response) {
				const firstCriteria = $('<span>', {
					title: 'در ۶ ماه قبل رأی‌گیری باید حداقل ۱۰۰ باشد',
					style: `background-color: white; color: ${response[username].sixMonthsEdits >= 100 ? 'green' : 'red'}`,
					text: `ویرایش‌های ۶ ماه: ${response[username].sixMonthsEdits.toLocaleString('fa')}`
				});
				const secondCriteria = $('<span>', {
					title: 'باید پیش از سه‌ماه قبل از شروع رأی‌گیری باشد',
					style: `background-color: white; color: ${timestamp - response[username].creationTime > 7889231500/*3 months in millis*/ ? 'green' : 'red'}`,
					text: `ایجاد حساب: ${new Date(response[username].creationTime).toLocaleDateString('fa')}`
				});
				result[username] = $('<bdi>').append('(', firstCriteria, '، ', secondCriteria, ')').html();
			}
			return result;
		});
	}
	
	function checkUsersOnPage(timestamp) {
		checkEligibility([...
			new Set($('a[title^="کاربر:"]').get().map(link => {
				if (link.href.includes('/wiki/'))
					return link.href.split('/wiki/')[1].split(':')[1];
				else return new mw.Uri(link.href).query.title?.split(':')[1];
			}).filter(x => x).map(decodeURI))
		], timestamp).then(result => {
			for (let username in result) {
				$(`a[href*="${encodeURI('کاربر:' + username.replace(/ /g, '_'))}"]`).get()
					.filter(x => x.title.startsWith('کاربر:'))
					.forEach(link => $(link).after(result[username]));
			}
		}).catch(mw.notify);
	}
	
	function setupEligibilityCheckForm(timestamp) {
		const usernameInput = $('<input>', {
			type: 'text',
			style: 'height: 1em; margin-left: .5em; padding: .5em;',
			value: mw.config.get('wgUserName')
		});
		const resultContainer = $('<div>', timestamp);
		usernameInput.on('input', () => resultContainer.empty());
		function onSubmit() {
			resultContainer.empty();
			checkEligibility([usernameInput.val()], timestamp).then(result => {
				resultContainer.html(Object.values(result)[0]);
			}).catch(mw.notify);
		}
		usernameInput.keyup(e => void(e.keyCode === 13 && onSubmit()));
		$('#mw-content-text').prepend($('<center>').css('padding', '2em').append(
			$('<div>').append(
				usernameInput,
				$('<button>', { 
					text: 'بررسی',
					class: 'mw-ui-button mw-ui-progressive',
				}).click(onSubmit)
			),
			resultContainer
		));
	}

	getPageCreationTimestamp(mw.config.get('wgPageName')).then(timestamp => {
		checkUsersOnPage(timestamp);
		setupEligibilityCheckForm(timestamp);
	});
});