// DNS Things - Punycode Module // Real Punycode conversion functions using punycode.js library function toPunycode(domain) { try { // Check if punycode library is available if (typeof punycode === 'undefined') { throw new Error('Punycode library not loaded'); } // Split domain into parts and convert each part const parts = domain.split('.'); const convertedParts = parts.map(part => { // Check if part contains non-ASCII characters if (/[^\x00-\x7F]/.test(part)) { return punycode.toASCII(part); } return part; }); return convertedParts.join('.'); } catch (error) { // Fallback to URL API if punycode library fails try { const url = new URL(`http://${domain}`); return url.hostname; } catch { throw new Error('Invalid domain format'); } } } function fromPunycode(domain) { try { // Check if domain contains punycode if (!domain.includes('xn--')) { return domain; // No punycode to convert } // Check if punycode library is available if (typeof punycode === 'undefined') { throw new Error('Punycode library not loaded'); } // Split domain into parts and convert each part const parts = domain.split('.'); const convertedParts = parts.map(part => { if (part.startsWith('xn--')) { try { return punycode.toUnicode(part); } catch { return part; // Return original if conversion fails } } return part; }); return convertedParts.join('.'); } catch (error) { throw new Error('Invalid punycode format'); } } // Initialize IDN Punycode Converter function initPunycodeConverter() { const unicodeInput = document.getElementById('unicode-input'); const punycodeInput = document.getElementById('punycode-input'); const unicodeToPunycodeBtn = document.getElementById('unicode-to-punycode-btn'); const punycodeToUnicodeBtn = document.getElementById('punycode-to-unicode-btn'); const punycodeResult = document.getElementById('punycode-result'); const unicodeResult = document.getElementById('unicode-result'); if (!unicodeInput || !punycodeInput) return; // Unicode to Punycode conversion const convertUnicodeToPunycode = () => { const input = unicodeInput.value.trim(); if (!input) { punycodeResult.innerHTML = ''; return; } try { const converted = toPunycode(input); const isConverted = converted !== input; punycodeResult.innerHTML = `
✅ Conversion applied
' : 'ℹ️ No conversion needed (ASCII only)
'}${error.message}
✅ Conversion applied
' : 'ℹ️ No conversion needed (no punycode found)
'}${error.message}