MediaWiki:Common.js: Difference between revisions

From Revenant Elegy Wiki
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';
ta.style.left = '-9999px';
document.body.appendChild(ta);
document.body.appendChild(ta);
ta.focus();
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);
if (ok) {
ok ? resolve() : reject();
resolve();
} else {
reject(new Error('Copy command failed'));
}
} catch (err) {
} catch (err) {
document.body.removeChild(ta);
document.body.removeChild(ta);
Line 33: Line 28:
}
}


function setStatus(block, text, isError) {
function initNavi($content) {
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 () {
$content.find('.naviClickable').each(function () {
var el = this;
var el = this;


if (el.dataset.naviBound === '1') {
if (el.dataset.bound === '1') return;
return;
el.dataset.bound = '1';
}
el.dataset.naviBound = '1';


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 || el.textContent.trim();
var text = el.dataset.navi || originalText;
var block = el.closest('.naviBlock') || el.parentNode;


copyText(text).then(function () {
copyText(text).then(function () {
setStatus(block, 'Paste in-game', false);
el.textContent = 'Paste in-game';
el.classList.add('naviActive');
 
setTimeout(function () {
el.textContent = originalText;
el.classList.remove('naviActive');
}, 2500);
}).catch(function () {
}).catch(function () {
setStatus(block, 'Press Ctrl+C', true);
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(function ($content) {
mw.hook('wikipage.content').add(initNavi);
initNaviCopy($content);
});
});
});

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);
});