fix(terminal): stop escaping JSON in terminal; pretty-print JSON messages

Decode HTML entities for incoming text and pretty-print valid JSON payloads before appending to the log.
This commit is contained in:
2025-09-30 20:38:14 +02:00
parent 9be4af1c09
commit 75dc122898

View File

@@ -129,8 +129,22 @@
this._appendLine('[open] WebSocket connection established'); this._appendLine('[open] WebSocket connection established');
}); });
ws.addEventListener('message', (evt) => { ws.addEventListener('message', (evt) => {
const data = typeof evt.data === 'string' ? evt.data : '[binary]'; let dataStr = typeof evt.data === 'string' ? evt.data : '[binary]';
this._appendLine(data); // Decode any HTML entities so JSON isn't shown as " etc.
dataStr = this._decodeHtmlEntities(dataStr);
// Try to pretty-print JSON if applicable
const trimmed = dataStr.trim();
if ((trimmed.startsWith('{') && trimmed.endsWith('}')) || (trimmed.startsWith('[') && trimmed.endsWith(']'))) {
try {
const obj = JSON.parse(trimmed);
const pretty = JSON.stringify(obj, null, 2);
this._appendLine(pretty);
return;
} catch (_) {
// fall through if not valid JSON
}
}
this._appendLine(dataStr);
}); });
ws.addEventListener('error', (evt) => { ws.addEventListener('error', (evt) => {
this._appendLine('[error] WebSocket error'); this._appendLine('[error] WebSocket error');
@@ -170,6 +184,16 @@
if (bodyEl) bodyEl.scrollTop = bodyEl.scrollHeight; if (bodyEl) bodyEl.scrollTop = bodyEl.scrollHeight;
} }
_decodeHtmlEntities(text) {
try {
const div = document.createElement('div');
div.innerHTML = text;
return div.textContent || div.innerText || '';
} catch (_) {
return text;
}
}
_adjustRightOffset() { _adjustRightOffset() {
try { try {
const drawer = document.querySelector('.details-drawer'); const drawer = document.querySelector('.details-drawer');