MediaWiki:Common.js: Difference between revisions
Allows different style (e.g., a border) while visible. |
Custom hex strings |
||
Line 18: | Line 18: | ||
}); | }); | ||
} | } | ||
/* Custom hex strings */ | |||
document.addEventListener('DOMContentLoaded', function () { | |||
var hexStringElements = document.getElementsByClassName('hex-string'); | |||
for (var i = 0; i < hexStringElements.length; i++) { | |||
hexStringElements[i].addEventListener('click', toggleHexString); | |||
} | |||
}); | |||
function toggleHexString(event) { | |||
var element = event.currentTarget; | |||
var hexString = element.dataset.hex; | |||
if (element.dataset.state === 'hex') { | |||
// Switch to English | |||
element.querySelector('.hex-string-content').innerText = hexToString(hexString); | |||
element.dataset.state = 'text'; | |||
} else { | |||
// Switch to Hex | |||
element.querySelector('.hex-string-content').innerText = hexString; | |||
element.dataset.state = 'hex'; | |||
} | |||
} | |||
function hexToString(hex) { | |||
var str = ''; | |||
for (var i = 0; i < hex.length; i += 2) { | |||
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16)); | |||
} | |||
return str; | |||
} |
Revision as of 09:52, 20 November 2023
/* Any JavaScript here will be loaded for all users on every page load. */ /* Custom animated hints: */ var coll = document.getElementsByClassName("custom-collapsible"); var i; for (i = 0; i < coll.length; i++) { coll[i].addEventListener("click", function() { this.classList.toggle("active"); var content = this.nextElementSibling; if (content.style.maxHeight){ content.style.maxHeight = null; content.classList.remove("custom-collapsible-content-visible"); } else { content.style.maxHeight = content.scrollHeight + "px"; content.classList.add("custom-collapsible-content-visible"); } }); } /* Custom hex strings */ document.addEventListener('DOMContentLoaded', function () { var hexStringElements = document.getElementsByClassName('hex-string'); for (var i = 0; i < hexStringElements.length; i++) { hexStringElements[i].addEventListener('click', toggleHexString); } }); function toggleHexString(event) { var element = event.currentTarget; var hexString = element.dataset.hex; if (element.dataset.state === 'hex') { // Switch to English element.querySelector('.hex-string-content').innerText = hexToString(hexString); element.dataset.state = 'text'; } else { // Switch to Hex element.querySelector('.hex-string-content').innerText = hexString; element.dataset.state = 'hex'; } } function hexToString(hex) { var str = ''; for (var i = 0; i < hex.length; i += 2) { str += String.fromCharCode(parseInt(hex.substr(i, 2), 16)); } return str; }