MediaWiki:Common.js

From Revenant Elegy Wiki
Revision as of 14:45, 2 April 2026 by Kins (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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