37 lines
2.1 KiB
JavaScript
37 lines
2.1 KiB
JavaScript
// ═══════════════════════════════════════════════════════
|
|
// JSON Formatter
|
|
// ═══════════════════════════════════════════════════════
|
|
async function formatJSON() {
|
|
const input = document.getElementById('jsonInput').value.trim();
|
|
if (!input) return setStatus('jsonStatus','error','Paste some JSON first.');
|
|
const iv = document.getElementById('jsonIndent').value;
|
|
const indent = iv === '\\t' ? '\t' : parseInt(iv);
|
|
const d = await apiPost('/api/json/format', { json: input, indent });
|
|
if (d.success) { document.getElementById('jsonOutput').value = d.result; setStatus('jsonStatus','success','Formatted ✓'); }
|
|
else setStatus('jsonStatus','error', d.error);
|
|
}
|
|
async function minifyJSON() {
|
|
const input = document.getElementById('jsonInput').value.trim();
|
|
if (!input) return setStatus('jsonStatus','error','Paste some JSON first.');
|
|
const d = await apiPost('/api/json/minify', { json: input });
|
|
if (d.success) { document.getElementById('jsonOutput').value = d.result; setStatus('jsonStatus','success','Minified ✓'); }
|
|
else setStatus('jsonStatus','error', d.error);
|
|
}
|
|
async function validateJSON() {
|
|
const input = document.getElementById('jsonInput').value.trim();
|
|
if (!input) return setStatus('jsonStatus','error','Paste some JSON first.');
|
|
const d = await apiPost('/api/json/validate', { json: input });
|
|
setStatus('jsonStatus', d.valid ? 'success' : 'error', d.message);
|
|
}
|
|
function clearJSON() {
|
|
document.getElementById('jsonInput').value=''; document.getElementById('jsonOutput').value='';
|
|
document.getElementById('jsonStatus').className='status';
|
|
}
|
|
|
|
// Keyboard shortcuts
|
|
document.getElementById('jsonInput').addEventListener('keydown', e => {
|
|
if(e.key==='Enter' && (e.ctrlKey||e.metaKey)) formatJSON();
|
|
if(e.key==='Tab') { e.preventDefault(); const t=e.target,s=t.selectionStart,en=t.selectionEnd; t.value=t.value.substring(0,s)+' '+t.value.substring(en); t.selectionStart=t.selectionEnd=s+2; }
|
|
});
|
|
|