MediaWiki:Gadget-shortURL.js
来自存档计划,Project Archive by Lakejason0
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-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 HanAssist = require('ext.gadget.HanAssist');
var i18n = HanAssist.batchConv({
shareDescription: {hans: '您还可以通过此链接分享页面:', hant: '您還可以透過此連結分享頁面:'},
copy: {hans: '复制', hant: '複製'},
copied: {hans: '已复制!', hant: '已複製!'},
});
// 对周报页面进行特别处理
var isTeahouseWeekly = $('body').hasClass("rootpage-茶馆周报");
var isLakeus = $('body').hasClass("skin-lakeus");
if (isTeahouseWeekly) {
console.log("Teahouse weekly header detected. Adding short URL button to the header.");
}
window.shortUrl = window.shortUrl || '';
// 缓存 mw 变量
var config = mw.config.get();
// 判断是否存在文章ID
if (config.wgArticleId > 0) {
shortUrl = config.wgServerName + '/-/' + config.wgArticleId;
// 插入短链接
var shortURLParagraph = $('<div>', { class: 'shortUrl-block', style: 'margin: 1em;' }).append(
$('<span>', { class: 'shortUrl-description' }).append(
$('<span>', { text: i18n.shareDescription }),
$('<strong>', { text: shortUrl + ' ' }),
$('<br>'),
$('<a>', { text: i18n.copy , href: 'javascript:;' }).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: [],
framed: false,
label: 'Short URL',
invisibleLabel: true,
popup: {
label: 'Short URL',
$content: shortURLParagraph,
align: 'center',
padded: true,
}
} );
// Append the button to the DOM.
if(isTeahouseWeekly) {
$( '.weekly-header-id' ).after( " |", popupButton.$element );
} else if(isLakeus) {
$( '.mw-indicators' ).append( popupButton.$element );
} else {
$( '#ca-view' ).after( popupButton.$element );
}
}
// 将短链接替换进文章
$('.shortUrl').text(shortUrl);
$('.shortUrl-link').html($('<a>', { href: shortUrl, text: shortUrl }));
})();