|
(function() { |
|
const text = document.body.textContent; /* define text properly */ |
|
try { |
|
JSON.parse(text); |
|
alert("✅ JSON is valid!"); |
|
} catch (error) { |
|
console.error("❌ Invalid JSON: " + error.message); |
|
|
|
/* Extract position from error message */ |
|
const match = error.message.match(/position (\d+)/); |
|
if (match) { |
|
const pos = parseInt(match[1], 10); |
|
|
|
/* Compute line and column */ |
|
const before = text.substring(0, pos); |
|
const lines = before.split("\n"); |
|
const lineNumber = lines.length; |
|
const columnNumber = lines[lines.length - 1].length + 1; |
|
|
|
console.error(`Error at line ${lineNumber}, column ${columnNumber}`); |
|
|
|
/* Show JSON with line numbers and highlight the error line */ |
|
const container = document.createElement("pre"); |
|
container.style.whiteSpace = "pre-wrap"; |
|
container.style.fontFamily = "monospace"; |
|
|
|
const allLines = text.split("\n"); |
|
allLines.forEach((line, i) => { |
|
const div = document.createElement("div"); |
|
div.textContent = (i + 1) + ": " + line; |
|
if (i + 1 === lineNumber) { |
|
div.style.background = "yellow"; /* highlight error line */ |
|
} |
|
container.appendChild(div); |
|
}); |
|
|
|
document.body.innerHTML = ""; /* clear old content */ |
|
document.body.appendChild(container); |
|
|
|
/* Scroll to the highlighted line */ |
|
const highlighted = container.querySelector("div[style*='yellow']"); |
|
if (highlighted) { |
|
highlighted.scrollIntoView({ behavior: "smooth", block: "center" }); |
|
} |
|
} |
|
} |
|
})(); |