Skip to content

Commit

Permalink
json editor added
Browse files Browse the repository at this point in the history
  • Loading branch information
Yashgabani845 committed Jun 7, 2024
1 parent 0648846 commit 3848dcb
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 106 deletions.
27 changes: 0 additions & 27 deletions FlashCard Maker/popup.html

This file was deleted.

71 changes: 0 additions & 71 deletions FlashCard Maker/popup.js

This file was deleted.

4 changes: 2 additions & 2 deletions FlashCard Maker/manifest.json → JSON Editor/manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"manifest_version": 3,
"name": "Flashcard Maker",
"name": "JSON Formatter",
"version": "1.0",
"description": "Create, manage, and study flashcards within your browser.",
"description": "A Chrome extension to format and validate JSON data.",
"permissions": ["storage"],
"action": {
"default_popup": "popup.html"
Expand Down
17 changes: 17 additions & 0 deletions JSON Editor/popup.html
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>
39 changes: 39 additions & 0 deletions JSON Editor/popup.js
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, '&amp;').replace(/(>)/g, '&gt;').replace(/(<)/g, '&lt;');
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>';
});
}
});

26 changes: 20 additions & 6 deletions FlashCard Maker/styles.css → JSON Editor/styles.css
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
body {
font-family: Arial, sans-serif;
padding: 10px;
width: 300px;
width: 400px;
}

h1, h2 {
h1 {
color: #333;
}

input[type="text"] {
textarea {
width: calc(100% - 22px);
height: 100px;
padding: 10px;
margin: 5px 0;
margin-bottom: 10px;
box-sizing: border-box;
}

Expand All @@ -28,7 +29,20 @@ body {
background-color: #45a049;
}

#decks {
.json-output {
white-space: pre-wrap;
background-color: #f5f5f5;
padding: 10px;
border: 1px solid #ddd;
margin-top: 10px;
}

.error-message {
color: red;
margin-top: 10px;
}

.string { color: green; }
.number { color: darkorange; }
.boolean { color: blue; }
.null { color: magenta; }
.key { color: red; }

0 comments on commit 3848dcb

Please sign in to comment.