document.getElementById('currentYear').textContent = new Date().getFullYear(); const imageUrlInput = document.getElementById('imageUrl'); const positionInput = document.getElementById('position'); const fetchButton = document.getElementById('fetchButton'); const buttonText = document.getElementById('buttonText'); const loadingSpinner = document.getElementById('loadingSpinner'); const outputDiv = document.getElementById('output'); const errorDisplayDiv = document.getElementById('errorDisplay'); const byteInspectorForm = document.getElementById('byteInspectorForm'); byteInspectorForm.addEventListener('submit', async function(event) { event.preventDefault(); await fetchByteValue(); }); async function fetchByteValue() { fetchButton.disabled = true; buttonText.textContent = 'Fetching...'; loadingSpinner.style.display = 'inline-block'; outputDiv.style.display = 'none'; outputDiv.innerHTML = ''; errorDisplayDiv.innerHTML = ''; const url = imageUrlInput.value; const positionStr = positionInput.value; const position = parseInt(positionStr); if (!url) { displayError('Please enter a URL.'); resetButton(); return; } if (isNaN(position) || position < 1) { displayError('Please enter a valid byte position (starting at 1).'); resetButton(); return; } try { const response = await fetch(url); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status} ${response.statusText}`); } const buffer = await response.arrayBuffer(); const byteArray = new Uint8Array(buffer); const zeroBasedIndex = position - 1; if (zeroBasedIndex >= byteArray.length) { displayError(`Position ${position} exceeds file length (${byteArray.length} bytes).`); resetButton(); return; } const targetByte = byteArray[zeroBasedIndex]; const hexValue = targetByte.toString(16).padStart(2, '0').toUpperCase(); const decimalValue = targetByte; const unicodeChar = String.fromCharCode(targetByte); const unicodeCodePoint = `U+${hexValue.padStart(4, '0')}`; outputDiv.innerHTML = `

Byte Information:

Hex Value: ${hexValue}
Decimal Value: ${decimalValue}
Unicode Char: '${escapeHtml(unicodeChar)}'
Unicode Code Point: ${unicodeCodePoint}
File Size: ${byteArray.length.toLocaleString()} bytes
`; outputDiv.style.display = 'block'; } catch (e) { console.error(e); displayError(`Error: ${e.message}. Ensure the URL is correct, publicly accessible, and allows CORS (Cross-Origin Resource Sharing). For local files, try using a simple HTTP server instead of file:/// URLs due to browser security restrictions.`); } finally { resetButton(); } } function resetButton() { fetchButton.disabled = false; buttonText.textContent = 'Fetch Byte Value'; loadingSpinner.style.display = 'none'; } function displayError(message) { errorDisplayDiv.innerHTML = `

Error:

${message}

`; } function escapeHtml(unsafe) { if (unsafe === undefined || unsafe === null) return ''; return unsafe .toString() .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); }