跳转到内容

User:SunAfterRain/js/EasyArchive.js

维基百科,自由的百科全书

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

/**
 * EasyArchive.js
 * fork from https://meta.wikimedia.org/w/index.php?oldid=18915644
 * @author Bluedeck <[email protected]>
 * @author [[User:SunAfterRain]]
 * Licence: CC-BY-SA 4.0
 */
// <nowiki>
$( async () => {
	if ( 'EasyArchive' in mw.libs ) {
		return;
	}
	mw.libs.EasyArchive = {};
	
	const HanAssist = /* require */ ( await mw.loader.using( 'ext.gadget.HanAssist' ) )( 'ext.gadget.HanAssist' );
	await mw.loader.getScript(
		mw.util.getUrl( 'User:SunAfterRain/EasyArchiveConfig.js', {
			action: 'raw',
			ctype: 'text/javascript'
		} )
	);
	if ( window.wgEasyArchiveLoadOverrideScript ) {
		await mw.loader.getScript(
			mw.util.getUrl( `User:${ mw.config.get( 'wgUserName' ) }/EasyArchiveConfig.js`, {
				action: 'raw',
				ctype: 'text/javascript'
			} )
		);
	}
	
	const config = mw.libs.EasyArchive.config;
	if ( !config ) {
		return;
	}

	for ( const regex of [].concat( disablePageRegExp || [] ) ) {
		if ( regex.exec( mw.config.get( 'wgPageName' ) ) ) {
			return;
		}
	}
	
	const messages = HanAssist.batchConv( {
		doDelete: {
			hans: '删除',
			hant: '刪除'
		},
		doArchive: {
			hans: '存档',
			hant: '存檔'
		},
		archiveEnable: {
			hans: '本页使用 Easy Archive 快速存档。',
			hant: '本頁使用 Easy Archive 快速存檔。'
		},
		archiveOthersPage: {
			hans: '本页是他人的用户讨论页,因此不支持 Easy Archive 快速存档。',
			hant: '本頁是他人的使用者討論頁,因此不支援 Easy Archive 快速存檔。',
			hk: '本頁是他人的用戶討論頁,因此不支援 Easy Archive 快速存檔。'
		},
		archiveNoEnable: {
			hans: '本页没有啟用 Easy Archive',
			hant: '本頁没有啟用 Easy Archive'
		},
		archiveEnablePossible: {
			hans: '本页面不是您的用户讨论页面。不过,如果需要,本页面是可以使用 Easy Archive 的。',
			hant: '本頁面不是您的使用者討論頁面。不過,如果需要,本頁面是可以使用 Easy Archive 的。',
			hk: '本頁面不是您的用戶討論頁面。不過,如果需要,本頁面是可以使用 Easy Archive 的。'
		},
		archiveEnablePossibleToEnable: {
			hans: '如果要在本页面使用 Easy Archive,请加入 {{Easy Archive|to=存档位置}}。',
			hant: '如果要在本頁面使用 Easy Archive,請加入 {{Easy Archive|to=存檔位置}}。'
		},
		archivePleaseEnable: {
			hans: '请在本页加入 {{Easy Archive|to=存档地址}} 来启用 Easy Archive',
			hant: '請在本頁加入 {{Easy Archive|to=存檔位置}} 來啟用 Easy Archive。'
		},
		archivePleaseEnableElaborate: {
			hans: '您的账号已支持 Easy Archive,但如果您要开始使用该功能,您还需要在自己的用户讨论页上添加模板 {{Easy Archive|to=存档位置}}。',
			hant: '您的帳號已支援 Easy Archive,但如果您要開始使用該功能,您還需要在自己的使用者討論頁上添加模板 {{Easy Archive|to=存檔位置}}。',
			hk: '您的帳號已支援 Easy Archive,但如果您要開始使用該功能,您還需要在自己的用戶討論頁上添加模板 {{Easy Archive|to=存檔位置}}。'
		},
		archiveUnsupport: {
			hans: '本页不支持 Easy Archive。',
			hant: '本頁不支援 Easy Archive。'
		},
		archiveMultiTemplate: {
			hans: '页面疑似包含多个 Easy Archive 模板,Easy Archive 已自动停用。',
			hant: '頁面疑似包含多個 Easy Archive 模板,Easy Archive 已自動停用。'
		},
		archiveParseFail: {
			hans: '无法解析页面包含的 Easy Archive 模板,Easy Archive 已自动停用。',
			hant: '無法解析頁面包含的 Easy Archive 模板,Easy Archive 已自動停用。'
		},
		archiveSelf: {
			hans: '本页面目前的存档地址是“$1”,和本页的标题相同。Easy Archive 无法按此位址存档。',
			hant: '本頁面當前的存檔地址是「$1」,和本頁的標題相同。Easy Archive 無法按此位址存檔。'
		},
		actionArchiveAll: {
			hans: '存档全部讨论',
			hant: '討論全部存檔'
		},
		actionArchiveOldPercent: {
			hans: '存档最旧的 $1',
			hant: '存檔最舊的 $1'
		},
		actionArchiveOld: {
			hans: '存档最旧的 $1 个',
			hant: '存檔最舊的 $1 個'
		},
		actionArchiveAllBut: {
			hans: '只留下最新的 $1 个',
			hant: '只留下最新的 $1 個'
		},
		settings: {
			hans: '设置',
			hant: '設定'
		},
		disable: {
			hans: '停用',
			hant: '停用'
		},
		change: {
			hans: '更改',
			hant: '變更'
		},
		disableWarning: {
			hans: '停用 Easy Archive 后,如要再次启用,只能手工操作。\n要现在停用 Easy Archive 吗?',
			hant: '停用 Easy Archive 後,如要再次啟用,則必須手動重啟。\n要現在停用 Easy Archive 嗎?'
		},
		disableAutomaticFailure: {
			hans: '经过尝试,恕无法自动停用 Easy Archive。请手动停用。\n请从本页删除以下模版:{{Easy Archive|to=存档位置}}。',
			hant: '經過嘗試,恕無法自動停用 Easy Archive。請手動停用。\n請從本頁刪除以下模板︰{{Easy Archive|to=存檔位置}}。'
		},
		archivePathChange: {
			hans: '将存档位址更改为:',
			hant: '将存檔位址變更為:'
		},
		archivePathChangeBadTitle: {
			hans: '您输入的标题是无效的。',
			hant: '您輸入的標題是無效的。'
		},
		archivePathChangeForceContinue: {
			hans: '这是一段有效的 wikitext',
			hant: '這是一段有效的 wikitext'
		},
		archivePathChangeSelf: {
			hans: '抱歉,不可能存档到原始页面。',
			hant: '抱歉,不可能存檔到原始頁面。'
		},
		archivePathChangeAutomaticFailure: {
			hans: '经过尝试,恕无法自动更改 Easy Archive 存档地址。请手动更改。\n请在本页找到以下模版:{{Easy Archive|to=存档位置}},并更改存档位置。',
			hant: '經過嘗試,恕無法自動變更 Easy Archive 存檔地址。請手動更改。\n請在本頁找到以下模板︰{{Easy Archive|to=存檔位置}},并更改存檔位置。'
		},
		sectionBlock: {
			hans: '$1 至 $2',
			hant: '$1 至 $2'
		},
		sectionLoading: {
			hans: '正在读取第 $1 节的内容',
			hant: '正在讀取第 $1 節的內容'
		},
		sectionArchiving: {
			hans: '正在存档第 $1 节的内容',
			hant: '正在存檔第 $1 節的內容'
		},
		sectionDeleting: {
			hans: '正在删除第 $1 节的内容',
			hant: '正在刪除第 $1 節的內容'
		},
		sectionArchiveDone: {
			hans: '已经将第 $1 节存档到 $2',
			hant: '已經將第 $1 節存檔到 $2'
		},
		sectionDeleteDone: {
			hans: '已经将第 $1 节删除',
			hant: '已經將第 $1 節刪除'
		},
		summaryArchive: '存檔 $1 個段落',
		summaryDelete: '刪除 $1 個段落',
		summaryDisable: '停用 Easy Archive',
		summaryChangePath: '變更 Easy Archive 存檔地址',
	} );

	if ( mw.libs.EasyArchive.overrideMessages ) {
		for ( const key of mw.libs.EasyArchive.overrideMessages ) {
			messages[ key ] = mw.libs.EasyArchive.overrideMessages[ key ];
		}
	}
	const messagePrefix = 'userscript-sunafterrain-easyarchive-';
	for ( const key of messages ) {
		mw.messages.set( messagePrefix + key, messages[ key ] );
	}

	let isSupport = ( () => {
		if (
			mw.config.get( 'wgNamespaceNumber' ) < 0
			|| [
				6 /* File */,
				8 /* MediaWiki */,
				10 /* Template */,
				12 /* Help */,
				14 /* Category */
			].includes( mw.config.get( 'wgNamespaceNumber' ) )
			|| config.disableNamespace?.includes( mw.config.get( 'wgNamespaceNumber' ) )
			|| mw.config.get( 'wgPageContentModel' ) !== 'wikitext'
		) {
			return false;
		}

		for ( const regex of [].concat( disablePageRegExp || [] ) ) {
			if ( regex.exec( mw.config.get( 'wgPageName' ) ) ) {
				return false;
			}
		}
		return true;
	} )();

	if ( !isSupport ) {
		$( '<li>' ).text( mw.msg( prefix + 'archiveUnsupport' ) ).appendTo( '#footer-info' );
		return;
	}

	const $settingElement = $( '.mw-parser-output div.bluedeck_easy_archive_data_point_collection', mw.util.$content );
	let setting = {
		enable: false
	};
	// if ( $settingElement.length > 1 ) {
	//	$( '<li>' ).text( mw.msg( prefix + 'archiveMultiTemplate' ) ).appendTo( '#footer-info' );
	//	return;
	// } else
	if ( $settingElement.length ) {
		setting = {
			enable: true,
			showArchiveLink: false, // sec-del
			showDeleteLink: false, // sec-arc
			archiveLocation: false // arc-loc
		};
		let leftPatten = '(';
		let delimPatten = '|';
		let rightPatten = ')';
		let parseSuccess = ( () => {
			let settingString = $settingElement.text().trim();
			if ( settingString.match( /^[@#%]set/i ) ) {
				leftPatten = settingString.charAt( 4 );
				delimPatten = settingString.charAt( 5 );
				rightPatten = settingString.charAt( 6 );
				if ( leftPatten === rightPatten || leftPatten === delimPatten || rightPatten === delimPatten ) {
					// throw new SyntaxError( 'Pound set statement has repetitive characters. E.g. \'#set|:|\' is illegal.' );
					return false;
				}
				settingString = settingString.slice( 7 );
			}
			while ( settingString.length ) {
				if ( !settingString.startsWith( leftPatten ) ) {
					return false;
				}
				let endSelector = settingString.indexOf( rightPatten );
				if ( endSelector === -1 ) {
					return false;
				}
				const that = settingString.slice( 1, endSelector );
				const keyValueSplit = that.indexOf( delimPatten );
				if ( keyValueSplit === -1 ) {
					return false;
				}
				switch ( that.slice( 0, keyValueSplit ) ) {
					case 'sec-del':
						setting.showDeleteLink = that.slice( keyValueSplit + 1 ) === '1';
						break;
					case 'sec-arc':
						setting.showArchiveLink = that.slice( keyValueSplit + 1 ) === '1';
						break;
					case 'arc-loc':
						let title = that.slice( keyValueSplit + 1 );
						let mTitle;
						try {
							mTitle = new mw.Title( that.slice( keyValueSplit + 1 ) );
						} catch ( e ) {
							return false;
						}
						setting.archiveLocation = mTitle.toText();
						break;
					case 'data-init':
						// 對原版來講可能有意義,但這裡當作沒有初始化就當作解析失敗
						if ( that.slice( keyValueSplit + 1 ) === '1' ) {
							return false;
						}
						break;
				}
				settingString = settingString.slice( endSelector + 1 );
			}
			return true;
		} )();
		if ( !parseSuccess ) {
			$( '<li>' ).text( mw.msg( prefix + 'archiveParseFail' ) ).appendTo( '#footer-info' );
			return;
		}
	}
	
	// TODO

	const api = new mw.Api();

	// TODO
} );
// </nowiki>