MediaWiki:Common.js
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */
mw.loader.using(['mediawiki.util']).then(function () {
function copyText(text) {
if (navigator.clipboard && window.isSecureContext) {
return navigator.clipboard.writeText(text);
}
return new Promise(function (resolve, reject) {
var ta = document.createElement('textarea');
ta.value = text;
ta.setAttribute('readonly', '');
ta.style.position = 'fixed';
ta.style.top = '-9999px';
ta.style.left = '-9999px';
document.body.appendChild(ta);
ta.focus();
ta.select();
try {
var ok = document.execCommand('copy');
document.body.removeChild(ta);
if (ok) {
resolve();
} else {
reject(new Error('Copy command failed'));
}
} catch (err) {
document.body.removeChild(ta);
reject(err);
}
});
}
function setStatus(block, text, isError) {
var status = block.querySelector('.naviStatus');
if (!status) {
return;
}
var oldText = status.dataset.defaultText || status.textContent || 'Copy';
if (!status.dataset.defaultText) {
status.dataset.defaultText = oldText;
}
status.textContent = text;
status.classList.toggle('naviError', !!isError);
clearTimeout(status._naviTimer);
status._naviTimer = setTimeout(function () {
status.textContent = status.dataset.defaultText;
status.classList.remove('naviError');
}, 2500);
}
function initNaviCopy($content) {
$content.find('.naviClickable').each(function () {
var el = this;
if (el.dataset.naviBound === '1') {
return;
}
el.dataset.naviBound = '1';
el.setAttribute('role', 'button');
el.setAttribute('tabindex', '0');
function activate() {
var text = el.dataset.navi || el.textContent.trim();
var block = el.closest('.naviBlock') || el.parentNode;
copyText(text).then(function () {
setStatus(block, 'Paste in-game', false);
}).catch(function () {
setStatus(block, 'Press Ctrl+C', true);
});
}
el.addEventListener('click', function (e) {
e.preventDefault();
activate();
});
el.addEventListener('keydown', function (e) {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
activate();
}
});
});
}
mw.hook('wikipage.content').add(function ($content) {
initNaviCopy($content);
});
});