User:SunAfterRain/js/blib/link.js

注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
// <nowiki>
$.when(
	$.ready,
	mw.loader.using(['mediawiki.api', 'ext.gadget.HanAssist'])
).then((_$, require) => {
	if (!mw.config.get('wgPageName') || mw.config.get('wgNamespaceNumber', -3) < -2) {
		return;
	}

	const libPrefix = 'User:Bluedecklibrary/';
	const libDomId = 'gadget-bldklib-link';
	mw.util.addCSS(`
h1 .${libDomId} {font-size: 20px;}
h2 .${libDomId} {font-size: 19px;}
h3 .${libDomId} {font-size: 17px;}
	`);

	const gadgetName = 'blib-archive.js';

	const HanAssist = require('ext.gadget.HanAssist');

	let $link;
	const api = new mw.Api();
	const messages = HanAssist.batchConv({
		libText: {
			other: 'Library',
			hans: '图书馆',
			hant: '圖書館'
		},

		libNoticeTitle: {
			other: 'Library note',
			hans: '图书馆提示',
			hant: '圖書館提示'
		},

		libNoticeText: {
			other: 'This page has an $1.',
			hans: '本页面在图书馆有$1。',
			hant: '本頁面在圖書館有$1。'
		},

		libNoticeTextLinkName: {
			other: 'archive in the library',
			hans: '存档',
			hant: '存檔'
		},
	});

	let titleMapElement = {};
	if (mw.config.get('wgNamespaceNumber', -3) >= 0) {
		allTitles.push(mw.config.get('wgPageName'));
	}

	const articlePathPrefix = mw.config.get('wgArticlePath').endsWith('$1')
		 ? mw.config.get('wgArticlePath').slice(0, -2)
		 : null;
	const wikiScriptRegExp = new RegExp(`^${mw.util.escapeRegExp(mw.util.wikiScript()).replace(/\/(index\\\.php)$/, '(\\/?$1)?')}$`);
	mw.hook('wikipage.content').add(async ($content) => {
		const titleElementMap = new Map();

		if (mw.config.get('wgRevisionId') && mw.config.get('wgAction') === 'view') {
			for (const a of $content.find('a.new:not(.userlink)')) {
				try {
					const url = new URL(a.href);
					let title;
					if (
						articlePathPrefix
						&& url.pathname.startsWith(articlePathPrefix)
					) {
						title = decodeURIComponent(url.pathname.slice(articlePathPrefix.length));
					} else if (wikiScriptRegExp.exec(url.pathname)) {
						title = url.searchParams.get('title');
					}
					title = new mw.Title(title).toString();
					titleElementMap.set(
						title,
						$(titleElementMap.get(title) || $()).add(a)
					);
				} catch (error) {
					continue;
				}
			}
		}

		const titles = [...titleElementMap.keys()]
			.map((title) => `${libPrefix}${title}`)
			.filter((title) => !!mw.Title.newFromText(title));

		if (mw.config.get('wgNamespaceNumber', -3) >= 0) {
			try {
				titles.push(
					new mw.Title(`${libPrefix}${mw.config.get('wgPageName')}`)
						.toString()
				);
			} catch (error) {
				// ignore
			}
		}

		const titlesNeedFetch = titles.filter(title => mw.Title.exists(title) === null);
		
		const promises = [];
		for (let i = 0; i < Math.ceil(titlesNeedFetch.length / 50); i++) {
			promises.push(
				new Promise(
					(resolve, reject) => api.get({
						action: 'query',
						titles: titlesNeedFetch.slice(i * 50, (i + 1) * 50),
						formatversion: '2'
					}).then(resolve, reject)
				)
				.then(
					(data) => {
						const existTitles = [];
						const nonexistTitles = [];
						for (const page of data.query.pages) {
							try {
								if (page.pageid) {
									existTitles.push(
										new mw.Title(page.title)
									);
								} else if (page.missing) {
									nonexistTitles.push(
										new mw.Title(page.title)
									);
								}
							} catch (error) {
								// ignore
							}
						}
						mw.Title.exist.set(existTitles);
						mw.Title.exist.set(nonexistTitles, false);
					}
				)
				.catch(console.error)
			);
			await new Promise((resolve) => setTimeout(resolve, 500));
		}
		await Promise.all(promises);
	});
});
// </nowiki>