MediaWiki:Gadget-shortURL.js

来自Minecraft基岩版开发Wiki

注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer或Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:Ctrl-F5
/**
 * @name Short URL
 * @author 机智的小鱼君
 *
 * @description Get the "fake" short link provided by MediaWiki.
 *              Solve the very long link of the pages that name contain non-ASCII words.
 */
// Originated from [https://github.com/Wjghj-Project/wjghj-wiki/blob/master/gadgets/Share-btn/script.js here].
(function () {
	var i18n = {
		shareDescription: '您还可以通过此链接分享页面:',
		copy: '复制',
		copied: '已复制!',
	};
	// 对周报页面进行特别处理
	var isWeekly = $('body').hasClass("rootpage-Minecraft基岩版开发Wiki_内核周报") || $('body').hasClass("rootpage-Minecraft基岩版开发Wiki_技术周报");
	if (isWeekly) {
		console.log("短链接将尝试添加到周报页眉。");
	}
	window.shortUrl = window.shortUrl || '';
	// 缓存 mw 变量
	var config = mw.config.get();
	// 判断是否存在文章ID
	if (config.wgArticleId > 0) {
		shortUrl = config.wgServer + '/-/' + config.wgArticleId;
		// 插入短链接
		var shortURLParagraph = $('<div>', { class: 'shortUrl-block', style: 'margin: 5px 12px;' }).append(
			$('<span>', { class: 'shortUrl-description' }).append(
				$('<span>', { text: i18n.shareDescription }),
				$('<br>'),
				$('<strong>', { text: shortUrl + '' }),
				$('<br>'),
				$('<a>', { href: 'javascript:;', text: i18n.copy }).on('click', function () {
					var $this = $(this);
					navigator.clipboard.writeText(shortUrl).then(function () {
						$this.text(i18n.copied);
						setTimeout(function () {
							$this.text(i18n.copy);
						}, 1500);
					});
				})
			)
		);

		// A PopupButtonWidget.
		var popupButton = new OO.ui.PopupButtonWidget({
			icon: 'link',
			flags: isWeekly ? ['progressive'] : [],
			framed: false,
			label: 'Short URL',
			invisibleLabel: true,
			popup: {
				label: 'Short URL',
				$content: shortURLParagraph,
				padded: true,
				align: 'center',
			}
		});
		// Append the button to the DOM.
		if (isWeekly) {
			$('.weekly-header-navigation').after(popupButton.$element);
		} else {
			$('#ca-view').after(popupButton.$element);
		}
	}

	// 将短链接替换进文章
	$('.shortUrl').text(shortUrl);
	$('.shortUrl-link').html($('<a>', { href: shortUrl, text: shortUrl }));
})();