🔗 URL 编码解码工具
支持多种编码格式转换
URL 编码
Base64
HTML 实体
Unicode
MD5 哈希
📝
输入
0 字符
🔄
编码
📄
输出
0 字符
📋
复制结果
🔄
交换输入输出
🗑️
清空
📖
加载示例
ℹ️ 编码说明
✅ 操作成功
' }, unicode: { name: 'Unicode 编码', desc: '字符转 Unicode 码点,跨平台兼容', encode: (str) => str.split('').map(c => '\\u' + c.charCodeAt(0).toString(16).padStart(4, '0')).join(''), decode: (str) => str.replace(/\\u([0-9a-fA-F]{4})/g, (_, hex) => String.fromCharCode(parseInt(hex, 16))), example: '你好世界' }, md5: { name: 'MD5 哈希', desc: '单向散列函数,用于数据完整性校验', encode: (str) => simpleMD5(str), decode: null, example: 'password123' } }; function setMode(mode) { currentMode = mode; document.querySelectorAll('.tab-btn').forEach(btn => btn.classList.remove('active')); event.target.classList.add('active'); const info = modeInfo[mode]; document.getElementById('inputTitle').textContent = info.name + '输入'; document.getElementById('outputTitle').textContent = info.name + '输出'; document.getElementById('convertBtn').innerHTML = info.decode ? '
🔄
编码/解码' : '
🔐
生成哈希'; document.getElementById('infoGrid').innerHTML = `
${info.name}
${info.desc}
`; clearAll(); } function updateLength() { const input = document.getElementById('inputText').value; const output = document.getElementById('outputText').value; document.getElementById('inputLength').textContent = `${input.length} 字符`; document.getElementById('outputLength').textContent = `${output.length} 字符`; } function convert() { const input = document.getElementById('inputText').value; if (!input) { showToast('⚠️ 请输入要转换的文本'); return; } const info = modeInfo[currentMode]; try { // 尝试解码 try { if (info.decode) { const decoded = info.decode(input); if (decoded !== input) { document.getElementById('outputText').value = decoded; showToast('✅ 解码成功'); updateLength(); return; } } } catch (e) {} // 编码 const encoded = info.encode(input); document.getElementById('outputText').value = encoded; showToast('✅ 编码成功'); updateLength(); } catch (error) { showToast('❌ 转换失败:' + error.message); } } function copyOutput() { const output = document.getElementById('outputText').value; if (output) { navigator.clipboard.writeText(output).then(() => { showToast('📋 已复制到剪贴板'); }); } else { showToast('⚠️ 无输出内容'); } } function swapText() { const input = document.getElementById('inputText').value; const output = document.getElementById('outputText').value; document.getElementById('inputText').value = output; document.getElementById('outputText').value = input; updateLength(); showToast('🔄 已交换'); } function clearAll() { document.getElementById('inputText').value = ''; document.getElementById('outputText').value = ''; updateLength(); } function loadExample() { const example = modeInfo[currentMode].example; document.getElementById('inputText').value = example; updateLength(); convert(); showToast('📖 示例已加载'); } // 简单 MD5 实现(用于演示) function simpleMD5(str) { // 这是一个简化的哈希函数,不是真正的 MD5 // 实际使用建议使用 crypto 库 let hash = 0; for (let i = 0; i < str.length; i++) { const char = str.charCodeAt(i); hash = ((hash << 5) - hash) + char; hash = hash & hash; } return Math.abs(hash).toString(16).padStart(32, '0'); } function showToast(message) { const toast = document.getElementById('toast'); toast.textContent = message; toast.classList.add('show'); setTimeout(() => toast.classList.remove('show'), 2000); } // 初始化 setMode('url');