MediaWiki:Gadget-mucPageTools.js
MediaWiki界面页面
更多操作
注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。
- Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
- Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
- Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5。
(function () {
'use strict';
function addCopyButtons($content) {
$content.find('pre').each(function () {
var pre = this;
if (pre.dataset.mucCopyReady === '1') {
return;
}
pre.dataset.mucCopyReady = '1';
var wrap = document.createElement('div');
wrap.className = 'muc-codeblock';
pre.parentNode.insertBefore(wrap, pre);
wrap.appendChild(pre);
var button = document.createElement('button');
button.type = 'button';
button.className = 'muc-codecopy';
button.textContent = '复制';
button.addEventListener('click', function () {
var text = pre.innerText || pre.textContent || '';
navigator.clipboard.writeText(text).then(function () {
button.textContent = '已复制';
setTimeout(function () {
button.textContent = '复制';
}, 1500);
});
});
wrap.appendChild(button);
});
}
function hardenExternalLinks($content) {
$content.find('a.external').each(function () {
this.target = '_blank';
this.rel = 'noopener noreferrer';
});
}
function addBackToTop() {
if (document.getElementById('muc-backtop')) {
return;
}
var button = document.createElement('button');
button.id = 'muc-backtop';
button.type = 'button';
button.setAttribute('aria-label', '返回顶部');
button.textContent = '↑';
button.addEventListener('click', function () {
window.scrollTo({ top: 0, behavior: 'smooth' });
});
document.body.appendChild(button);
function sync() {
button.classList.toggle('is-visible', window.scrollY > 480);
}
window.addEventListener('scroll', sync, { passive: true });
sync();
}
mw.hook('wikipage.content').add(function ($content) {
addCopyButtons($content);
hardenExternalLinks($content);
});
$(addBackToTop);
}());