forked from Sulagna-Dutta-Roy/GGExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0648846
commit 3848dcb
Showing
6 changed files
with
78 additions
and
106 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>JSON Formatter</title> | ||
<link rel="stylesheet" type="text/css" href="styles.css"> | ||
</head> | ||
<body> | ||
<div id="app"> | ||
<h1>JSON Formatter</h1> | ||
<textarea id="json-input" placeholder="Paste your JSON here..."></textarea> | ||
<button id="format-button">Format JSON</button> | ||
<div id="json-output" class="json-output"></div> | ||
<div id="error-message" class="error-message"></div> | ||
</div> | ||
<script src="popup.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
document.addEventListener('DOMContentLoaded', function () { | ||
const formatButton = document.getElementById('format-button'); | ||
const jsonInput = document.getElementById('json-input'); | ||
const jsonOutput = document.getElementById('json-output'); | ||
const errorMessage = document.getElementById('error-message'); | ||
|
||
formatButton.addEventListener('click', function () { | ||
const jsonString = jsonInput.value.trim(); | ||
try { | ||
const json = JSON.parse(jsonString); | ||
const formattedJson = JSON.stringify(json, null, 2); | ||
jsonOutput.innerHTML = syntaxHighlight(formattedJson); | ||
errorMessage.textContent = ''; | ||
} catch (e) { | ||
errorMessage.textContent = 'Invalid JSON: ' + e.message; | ||
jsonOutput.textContent = ''; | ||
} | ||
}); | ||
|
||
function syntaxHighlight(json) { | ||
json = json.replace(/(&)/g, '&').replace(/(>)/g, '>').replace(/(<)/g, '<'); | ||
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|\b(\d+)\b)/g, function (match) { | ||
var cls = 'number'; | ||
if (/^"/.test(match)) { | ||
if (/:$/.test(match)) { | ||
cls = 'key'; | ||
} else { | ||
cls = 'string'; | ||
} | ||
} else if (/true|false/.test(match)) { | ||
cls = 'boolean'; | ||
} else if (/null/.test(match)) { | ||
cls = 'null'; | ||
} | ||
return '<span class="' + cls + '">' + match + '</span>'; | ||
}); | ||
} | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters