📦 代码压缩工具
0 B
原始大小
0 B
压缩后大小
0 B
节省大小
0%
压缩率
压缩率 0%
'; }); // 移除 HTML 注释 code = code.replace(//g, ''); // 合并换行和空格(保留标签间一个空格) code = code.replace(/\s+/g, ' '); // 移除标签间多余空格 code = code.replace(/>\s+<'); return code.trim(); } function updateStats(origLen, compLen) { var originalSize = formatBytes(origLen); var compressedSize = formatBytes(compLen); var savedSize = formatBytes(Math.max(0, origLen - compLen)); var ratio = origLen > 0 ? (100 - (compLen / origLen * 100)) : 0; document.getElementById('originalSize').textContent = originalSize; document.getElementById('compressedSize').textContent = compressedSize; document.getElementById('savedSize').textContent = savedSize; document.getElementById('compressionRatio').textContent = ratio.toFixed(1) + '%'; document.getElementById('compressionFill').style.width = Math.max(0, Math.min(100, ratio)) + '%'; document.getElementById('compressionText').textContent = '压缩率 ' + ratio.toFixed(1) + '%'; } function formatBytes(bytes) { if (bytes < 1024) return bytes + ' B'; if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB'; return (bytes / (1024 * 1024)).toFixed(2) + ' MB'; } function clearAll() { document.getElementById('inputCode').value = ''; document.getElementById('outputCode').value = ''; updateStats(0, 0); } function copyResult() { var code = document.getElementById('outputCode').value; if (!code) { alert('没有可复制的内容'); return; } navigator.clipboard.writeText(code).then(function() { alert('复制成功'); }); } function downloadResult() { var code = document.getElementById('outputCode').value; if (!code) { alert('没有可下载的内容'); return; } var type = document.getElementById('codeType').value; var ext = type === 'html' ? 'html' : (type === 'javascript' ? 'min.js' : 'min.css'); var blob = new Blob([code], { type: 'text/plain;charset=utf-8' }); var a = document.createElement('a'); a.href = URL.createObjectURL(blob); a.download = 'compressed.' + ext; a.click(); } loadExample();
↑