-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
167 lines (150 loc) · 4.88 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
document.addEventListener("DOMContentLoaded", () => {
const jsonInput = document.getElementById("jsonInput");
const jsonOutput = document.getElementById("jsonOutput");
const error = document.getElementById("error");
const tabSpaceSelect = document.getElementById("tabSpaceSelect");
const specificationSelect = document.getElementById("specificationSelect");
const themeToggle = document.getElementById("themeToggle");
const copyButton = document.getElementById("copyButton");
const downloadButton = document.getElementById("downloadButton");
const uploadButton = document.getElementById("uploadButton");
const fileInput = document.createElement("input");
fileInput.type = "file";
fileInput.accept = ".json";
const savedTheme = localStorage.getItem("theme");
if (savedTheme) {
document.body.classList.toggle("dark", savedTheme === "dark");
}
themeToggle.textContent = "Change Theme";
document
.getElementById("exampleButton")
.addEventListener("click", loadExampleJSON);
document.getElementById("clearButton").addEventListener("click", clearInput);
document
.querySelector(".buttons button:nth-child(1)")
.addEventListener("click", formatJSON);
document
.querySelector(".buttons button:nth-child(2)")
.addEventListener("click", minifyJSON);
themeToggle.addEventListener("click", toggleTheme);
copyButton.addEventListener("click", copyToClipboard);
downloadButton.addEventListener("click", downloadFormattedJSON);
uploadButton.addEventListener("click", () => fileInput.click());
fileInput.addEventListener("change", handleFileUpload);
function loadExampleJSON() {
const specification = specificationSelect.value;
jsonInput.value =
specification === "JSON5"
? `{
name: "Bob",
age: 25,
isEmployed: false,
skills: ["HTML", "CSS", "React"],
address: {
city: "Somewhere",
zipcode: "67890"
}
}`
: `{
"name": "Alice",
"age": 30,
"isEmployed": true,
"skills": ["JavaScript", "Python", "C++"],
"address": {
"city": "Wonderland",
"zipcode": "12345"
}
}`;
}
function parseJSONInput(input, specification) {
try {
return specification === "JSON5" ? JSON5.parse(input) : JSON.parse(input);
} catch (e) {
throw new Error(
escapeHTML(
`Oops! Something went wrong. Please check your JSON and the selected specification and try again.`
)
);
}
}
function stringifyJSON(json, specification, tabSpaces = null) {
return specification === "JSON5"
? JSON5.stringify(json, null, tabSpaces)
: JSON.stringify(json, null, tabSpaces);
}
function formatJSON() {
const input = jsonInput.value;
const tabSpaces = parseInt(tabSpaceSelect.value, 10);
const specification = specificationSelect.value;
try {
const json = parseJSONInput(input, specification);
jsonOutput.value = stringifyJSON(json, specification, tabSpaces);
error.style.display = "none";
} catch (e) {
displayError(e.message);
}
}
function minifyJSON() {
const input = jsonInput.value;
const specification = specificationSelect.value;
try {
const json = parseJSONInput(input, specification);
jsonOutput.value = stringifyJSON(json, specification);
error.style.display = "none";
} catch (e) {
displayError(e.message);
}
}
function displayError(message) {
error.textContent = message;
error.style.display = "block";
jsonOutput.value = "";
}
function clearInput() {
jsonInput.value = "";
jsonOutput.value = "";
error.style.display = "none";
}
function copyToClipboard() {
navigator.clipboard.writeText(jsonOutput.value).then(() => {
copyButton.textContent = "Copied!";
setTimeout(() => (copyButton.textContent = "Copy"), 2000);
});
}
function downloadFormattedJSON() {
const blob = new Blob([jsonOutput.value], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "formatted.json";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
function handleFileUpload(event) {
const file = event.target.files[0];
if (file) {
if (file.type !== "application/json") {
displayError("Please upload a valid JSON file.");
return;
}
const reader = new FileReader();
reader.onload = (e) => {
jsonInput.value = e.target.result;
error.style.display = "none";
};
reader.readAsText(file);
}
}
function toggleTheme() {
const isDark = !document.body.classList.contains("dark");
document.body.classList.toggle("dark", isDark);
localStorage.setItem("theme", isDark ? "dark" : "light");
}
function escapeHTML(str) {
const div = document.createElement("div");
div.textContent = str;
return div.innerHTML;
}
});