User:AnYiLin/js/CheckEligibility.js

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。

/*!
 * @author 安忆 [[zh:User:AnYiLin]]
 * @file CheckEligibility.js
 *
 * Copyright (c) 2021-present, 安忆.
 *
 * This source code is licensed under the GPL v3 license.
 *
 * 这里忽略了“在投票开始之前”这个时间节点,直接使用了当前时间。
 * 因为在使用此工具查询时,某用户在投票开始之后才满足了资格要求就随即跑去投票,是不太可能的事情。
 */
(($, mw) => {
	if (!/^zh(?:\.m)?\.wikipedia/.test(window.location.host)) return;
	async function checkEligibility(user) {
		async function queryEligibility(user, step1, step2) {
			const getDate = (count = 0, date = new Date()) => {
						date.setMonth(date.getMonth() - count);
						let [year, mouth, day] = [...date.toLocaleDateString().split('/')];
						day < 10 && (day = `0${day}`);
						mouth < 10 && (mouth = `0${mouth}`);
						return `${year}-${mouth}-${day}`
					},
				queryCount = params => fetch(`https://xtools.wmcloud.org/api/user/simple_editcount/zhwiki/${user}/${params}`).then(data => data.json()).then(data => data.deleted_edit_count + data.live_edit_count),
				allCount = await queryCount('all'),
				allCountOnlyArt = await queryCount('0'),
				beforeFourMouth = await queryCount(`all/1970-01-01/${getDate(4)}`),
				lastThreeMouth = await queryCount(`all/${getDate(3)}/${getDate()}`);
			lastThreeMouth !== 0 && await fetch(`/w/api.php?action=query&format=json&list=usercontribs&uclimit=${lastThreeMouth>=500?500:lastThreeMouth}&ucuser=${user}`).then(data => data.json())
				.then(data => {
					for (const item of data.query.usercontribs) {
						if (item.ns !== 2 && item.ns !== 3) {
							step2 = true;
							break
						}
					}
				});
			(allCount >= 3000 || allCountOnlyArt >= 1500) && (step1 = true);
			return step1 || beforeFourMouth >= 500 && step2
		}
		async function showEligibility(user, eligibility) {
			await mw.loader.using('ext.gadget.ding');
			window.bldkDingExposedInterface(`正在查${wgULS('询', '詢')}…`, 'default', 'long');
			eligibility = await queryEligibility(user);
			document.getElementById('bluedeck_ding').remove();
			OO.ui.alert($(`<div class="oo-ui-window-foot" style="border:.1rem solid #0645ad;display:flex;justify-content:space-evenly"><span style="font-size:1.2rem;font-weight:500;line-height:1.8;padding:.4em 0">${eligibility?`具有投票${wgULS('资', '資')}格`:`不具有投票${wgULS('资', '資')}格`}</span></div>`))
		}
		async function showPrompt() {
			const noUser = wgULS('请输入用户名', '請輸入用戶名', undefined, '請輸入使用者名稱');
			await mw.loader.using(['oojs-ui-windows', 'oojs-ui.styles.icons-interactions']);
			OO.ui.prompt($(`<div class="oo-ui-window-foot" style="display:flex;justify-content:space-evenly"><span style="font-size:1.2rem;font-weight:500;line-height:1.8">${noUser}</span></div>`), {
				textInput: {
					icon: 'help',
					placeholder: `不${wgULS('带', '帶')}User:前${wgULS('缀', '綴')}`
				}
			}).then(user => {
				if (user !== null) user === '' ? OO.ui.alert($(`<div class="oo-ui-window-foot" style="border:.1rem solid #0645ad;display:flex;justify-content:space-evenly"><span style="font-size:1.2rem;font-weight:500;line-height:1.8;padding:.4em 0">${noUser}</span></div>`)).then(() => showPrompt()) : showEligibility(user)
			})
		}
		user ? showEligibility(user) : showPrompt()
	}
	async function checkEligibilityInit() {
		if (!document.getElementById('p-tb')) return;
		await mw.loader.using(['ext.gadget.site-lib', 'mediawiki.user']);
		const [dom, isMinerva] = [mw.util.addPortletLink('p-tb', '#', wgULS('查询人事票资格', '查詢人事票資格'), 'ca-eligibility', wgULS('查询指定用户是否具有人事票资格', '查詢指定用戶是否具有人事票資格', undefined, '查詢指定使用者是否具有人事票資格')), document.body.classList.contains('skin-minerva')];
		if (isMinerva) {
			const style = document.createElement('style');
			style.id = 'css-checkeligibility';
			style.appendChild(document.createTextNode('#ca-eligibility a:before{background:url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%2220%22%20height%3D%2220%22%20viewBox%3D%220%200%2020%2020%22%3E%3Cg%20fill%3D%22%2354595d%22%3E%3Cpath%20d%3D%22M10%200C4.477%200%200%204.477%200%2010s4.477%2010%2010%2010%2010-4.477%2010-10S15.523%200%2010%200zM9%205h2v2H9zm0%204h2v6H9z%22%2F%3E%3C%2Fg%3E%3C%2Fsvg%3E")}'));
			document.head.appendChild(style)
		}
		(isMinerva ? dom : dom.firstElementChild).addEventListener('click', e => {
			e.preventDefault();
			RLCONF.wgRelevantUserName && !mw.util.isIPAddress(RLCONF.wgRelevantUserName) ? checkEligibility(RLCONF.wgRelevantUserName) : checkEligibility()
		})
	}
	document.readyState !== 'loading' ? checkEligibilityInit() : document.addEventListener('DOMContentLoaded', () => checkEligibilityInit())
})(jQuery, mw);