跳转到内容

User:Emojiwiki/module/autoVariant.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
$(function() {
	const variantPath = mw.config.get( 'wgVariantArticlePath' );
	const currentVariant = mw.config.get( 'wgUserVariant' );
	if (!variantPath) { return; }
	const pageName = mw.config.get( 'wgPageName' );
	const userName = mw.config.get( 'wgUserName' ) || "Example";
	const userSettingsPage = "User:" + userName + "/EWdefaultVariant_.js";
	mw.loader.getScript("/w/index.php?title=" + userSettingsPage + "&action=raw&ctype=text/javascript").always(function () {
		const defaultVariant = window.EW_defaultVariant || "zh-tw";
		const defaultVariantPath = variantPath.replace('$1', pageName).replace('$2', defaultVariant);
		console.log("[EW_defaultVariant] Variant: " + defaultVariant);
		switch (currentVariant) {
			case 'zh':
			case 'zh-hant':
			case 'zh-hans':
				mw.notify("Changing variant to " + defaultVariant + "...");
				window.location.href = defaultVariantPath;
				break;
		}
		
		/* mw.loader.using( 'mediawiki.api.edit' ).then */(function () {
			const settingsPageContent = "window.EW_defaultVariant = \"$1\";";
			const ewVariantParam = new URLSearchParams(window.location.search).get('ewVariant');
			if (ewVariantParam) {
				$(".editpage-head-copywarn").append("<p>Please add the following code into the box: <code>" + settingsPageContent.replace('$1', 'zh-' + ewVariantParam) + "</code></p>");
			}
			const api = new mw.Api();
			const btn = mw.util.addPortletLink(
        		'p-cactions',
        		'#',
    			'Change Default Variant',
        		'ew-variant',
        		null,
    			null,
        		'#pt-ew-variant'
			);
			$(btn).click(function (e) {
				e.preventDefault();
				const newVariant = prompt('Tell me the variant: (Current: ' + defaultVariant + ')').replace(/^(zh-)/,'');
				switch (newVariant) {
					case 'cn':
					case 'hk':
					case 'mo':
					case 'my':
					case 'sg':
					case 'tw':
						mw.notify("Switching variant...");
						api.edit( userSettingsPage, function () {
        					return {
        						text: settingsPageContent.replace('$1', 'zh-' + newVariant),
    							summary: 'Updating default variant ([[User:Emojiwiki/module/autoVariant.js|Emojiwiki AutoVariant]], [[UT:Emojiwiki|Bug Report]])',
    							nocreate: false
            				};
        				} ).done(function () {
        					mw.notify("Variant switched.");
        					window.location.href = variantPath.replace('$1', pageName).replace('$2',"zh-" + newVariant);;
        				}).catch(function (e) {
        					if (e == 'nocreate-missing') {
        						mw.notify("Page not exist, please create it manually");
        						window.location = "/w/index.php?title=" + userSettingsPage + "&action=edit&ewVariant=" + newVariant;
        					}
        					mw.notify("Error while switching variant: " + e);
        				});
						break;
					case 'hans':
					case 'hant':
					case '':
						mw.notify("Variant cannot be one of the blocked variants!");
						break;
					default:
						mw.notify("Invalid Variant!");
						break;
				}
			});
		})();
	});
});