MediaWiki:Common.js: Difference between revisions
Jump to navigation
Jump to search
(Created page with "→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 = '-9999...") |
No edit summary |
||
| Line 2: | Line 2: | ||
mw.loader.using(['mediawiki.util']).then(function () { | mw.loader.using(['mediawiki.util']).then(function () { | ||
function copyText(text) { | function copyText(text) { | ||
if (navigator.clipboard && window.isSecureContext) { | if (navigator.clipboard && window.isSecureContext) { | ||
| Line 13: | Line 14: | ||
ta.style.position = 'fixed'; | ta.style.position = 'fixed'; | ||
ta.style.top = '-9999px'; | ta.style.top = '-9999px'; | ||
document.body.appendChild(ta); | document.body.appendChild(ta); | ||
ta.select(); | ta.select(); | ||
| Line 21: | Line 20: | ||
var ok = document.execCommand('copy'); | var ok = document.execCommand('copy'); | ||
document.body.removeChild(ta); | document.body.removeChild(ta); | ||
ok ? resolve() : reject(); | |||
} catch (err) { | } catch (err) { | ||
document.body.removeChild(ta); | document.body.removeChild(ta); | ||
| Line 33: | Line 28: | ||
} | } | ||
function | function initNavi($content) { | ||
$content.find('.naviClickable').each(function () { | $content.find('.naviClickable').each(function () { | ||
var el = this; | var el = this; | ||
if (el.dataset. | if (el.dataset.bound === '1') return; | ||
el.dataset.bound = '1'; | |||
el.dataset. | |||
el.setAttribute('role', 'button'); | el.setAttribute('role', 'button'); | ||
el.setAttribute('tabindex', '0'); | el.setAttribute('tabindex', '0'); | ||
var originalText = el.textContent; | |||
function activate() { | function activate() { | ||
var text = el.dataset.navi || | var text = el.dataset.navi || originalText; | ||
copyText(text).then(function () { | copyText(text).then(function () { | ||
el.textContent = 'Paste in-game'; | |||
el.classList.add('naviActive'); | |||
setTimeout(function () { | |||
el.textContent = originalText; | |||
el.classList.remove('naviActive'); | |||
}, 2500); | |||
}).catch(function () { | }).catch(function () { | ||
el.textContent = 'Press Ctrl+C'; | |||
el.classList.add('naviError'); | |||
setTimeout(function () { | |||
el.textContent = originalText; | |||
el.classList.remove('naviError'); | |||
}, 2500); | |||
}); | }); | ||
} | } | ||
| Line 91: | Line 76: | ||
} | } | ||
mw.hook('wikipage.content').add( | mw.hook('wikipage.content').add(initNavi); | ||
}); | }); | ||
Latest revision as of 14:45, 2 April 2026
/* 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';
document.body.appendChild(ta);
ta.select();
try {
var ok = document.execCommand('copy');
document.body.removeChild(ta);
ok ? resolve() : reject();
} catch (err) {
document.body.removeChild(ta);
reject(err);
}
});
}
function initNavi($content) {
$content.find('.naviClickable').each(function () {
var el = this;
if (el.dataset.bound === '1') return;
el.dataset.bound = '1';
el.setAttribute('role', 'button');
el.setAttribute('tabindex', '0');
var originalText = el.textContent;
function activate() {
var text = el.dataset.navi || originalText;
copyText(text).then(function () {
el.textContent = 'Paste in-game';
el.classList.add('naviActive');
setTimeout(function () {
el.textContent = originalText;
el.classList.remove('naviActive');
}, 2500);
}).catch(function () {
el.textContent = 'Press Ctrl+C';
el.classList.add('naviError');
setTimeout(function () {
el.textContent = originalText;
el.classList.remove('naviError');
}, 2500);
});
}
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(initNavi);
});