fix: parse first web probe report object

This commit is contained in:
Codex
2026-06-18 03:59:20 +00:00
parent f8cef5c99d
commit e062422931
+34 -4
View File
@@ -9884,11 +9884,10 @@ function parseJsonObject(text: string): Record<string, unknown> | null {
const parsed = JSON.parse(trimmed) as unknown;
return typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) ? parsed as Record<string, unknown> : null;
} catch {
const start = trimmed.indexOf("{");
const end = trimmed.lastIndexOf("}");
if (start >= 0 && end > start) {
const objectText = firstJsonObjectText(trimmed);
if (objectText) {
try {
const parsed = JSON.parse(trimmed.slice(start, end + 1)) as unknown;
const parsed = JSON.parse(objectText) as unknown;
return typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) ? parsed as Record<string, unknown> : null;
} catch {}
}
@@ -9896,6 +9895,37 @@ function parseJsonObject(text: string): Record<string, unknown> | null {
}
}
function firstJsonObjectText(text: string): string | null {
const start = text.indexOf("{");
if (start < 0) return null;
let depth = 0;
let inString = false;
let escaped = false;
for (let index = start; index < text.length; index += 1) {
const char = text[index];
if (inString) {
if (escaped) {
escaped = false;
} else if (char === "\\") {
escaped = true;
} else if (char === "\"") {
inString = false;
}
continue;
}
if (char === "\"") {
inString = true;
continue;
}
if (char === "{") depth += 1;
else if (char === "}") {
depth -= 1;
if (depth === 0) return text.slice(start, index + 1);
}
}
return null;
}
function compactWebProbeResult(report: Record<string, unknown> | null): Record<string, unknown> | null {
if (report === null) return null;
const dom = record(report.dom);