-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
300 lines (272 loc) · 8.78 KB
/
script.js
File metadata and controls
300 lines (272 loc) · 8.78 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
(() => {
const $ = (id) => document.getElementById(id);
const input = $("json-input");
const output = $("json-output");
const btnFormat = $("btn-format");
const btnCancel = $("btn-cancel");
const btnCancelOverlay = $("btn-cancel-overlay");
const btnCopy = $("btn-copy");
const btnDownload = $("btn-download"); // ADDED: download button
const btnFile = $("btn-file");
const fileInput = $("file-input");
const btnUrl = $("btn-url");
const btnClear = $("btn-clear");
const progress = $("progress");
const bar = $("bar");
const stage = $("stage");
const detail = $("detail");
const toast = $("toast");
const modal = $("modal");
const urlInput = $("url-input");
const urlLoad = $("url-load");
const urlCancel = $("url-cancel");
const stats = $("input-stats");
const sourceInfo = $("source-info");
let worker = new Worker("worker.js");
let jobId = 0;
let busy = false;
let prettyText = "";
worker.onmessage = (e) => {
const { type, jobId: id } = e.data || {};
if (id !== jobId) return;
if (type === "progress") {
showProgress(true, e.data.percent, e.data.stage, e.data.detail);
} else if (type === "chunk") {
// Use a fragment to minimize reflows
const frag = document
.createRange()
.createContextualFragment(e.data.html + "\n");
output.appendChild(frag);
} else if (type === "done") {
const { formatted, totalLines, ms } = e.data;
prettyText = formatted;
btnCopy.disabled = false;
if (btnDownload) btnDownload.disabled = false; // ADDED: enable download when done
showProgress(false);
busy = false;
info(`Done in ${ms}ms • ${totalLines.toLocaleString()} lines`);
} else if (type === "error") {
showProgress(false);
busy = false;
error(`Invalid JSON: ${e.data.message}`);
}
};
btnFormat.addEventListener("click", () => {
const text = input.value.trim();
if (!text) {
error("Input is empty.");
return;
}
runFormat(text);
});
const cancelJob = () => {
jobId += 1; // invalidate current job
showProgress(false);
busy = false;
info("Cancelled.");
};
btnCancel.addEventListener("click", cancelJob);
btnCancelOverlay?.addEventListener("click", cancelJob);
btnCopy.addEventListener("click", async () => {
if (!prettyText) return;
try {
await navigator.clipboard.writeText(prettyText);
flash(btnCopy, "✅ Copied!", "📋 Copy");
} catch {
error("Copy failed.");
}
});
// ADDED: Download handler
btnDownload?.addEventListener("click", () => {
const data = prettyText && prettyText.length ? prettyText : input.value;
if (!data) {
error("Nothing to download yet.");
return;
}
const name = suggestJsonFilename(
sourceInfo?.title || sourceInfo?.textContent || "download.json"
);
downloadTextAsFile(data, name);
flash(btnDownload, "✅ Saved!", "⬇️ Download JSON");
});
btnFile.addEventListener("click", () => fileInput.click());
fileInput.addEventListener("change", onFilePicked);
btnUrl.addEventListener("click", () => {
urlInput.value = "";
modal.classList.remove("hidden");
urlInput.focus();
});
urlCancel.addEventListener("click", () => modal.classList.add("hidden"));
urlLoad.addEventListener("click", loadFromUrl);
urlInput.addEventListener("keydown", (e) => {
if (e.key === "Enter") loadFromUrl();
else if (e.key === "Escape") modal.classList.add("hidden");
});
btnClear.addEventListener("click", () => {
input.value = "";
output.textContent = "";
prettyText = "";
btnCopy.disabled = true;
if (btnDownload) btnDownload.disabled = true; // ADDED: keep states in sync
sourceInfo.textContent = "";
sourceInfo.title = "";
updateStats();
if (window.gsap) {
gsap.fromTo(
input,
{ backgroundColor: "#35171a" },
{ backgroundColor: "#0e172a", duration: 0.45 }
);
}
input.focus();
});
input.addEventListener("input", updateStats);
window.addEventListener("load", updateStats);
function runFormat(text) {
if (busy) return;
busy = true;
jobId += 1;
btnCopy.disabled = true;
if (btnDownload) btnDownload.disabled = true; // ADDED: disable while processing
output.innerHTML = "";
showProgress(true, 8, "Starting…");
worker.postMessage({ type: "format", jobId, text });
}
async function onFilePicked(ev) {
const f = ev.target.files?.[0];
if (!f) return;
try {
const text = await f.text();
input.value = text;
const hint =
f.webkitRelativePath && f.webkitRelativePath.length > 0
? f.webkitRelativePath
: f.name;
const sizeStr = formatBytes(f.size);
const dateStr = new Date(f.lastModified).toLocaleString();
sourceInfo.title = hint;
sourceInfo.textContent = `File: ${hint} • ${sizeStr} • ${dateStr}`;
updateStats();
} catch {
error("Unable to read file.");
}
}
async function loadFromUrl() {
const url = urlInput.value.trim();
if (!url) return;
modal.classList.add("hidden");
try {
showProgress(true, 5, "Fetching…");
const res = await fetch(url, { credentials: "omit" });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const text = await res.text();
input.value = text;
const safeText = escapeHtml(url);
sourceInfo.innerHTML = `URL: ${safeText}`;
sourceInfo.title = url;
updateStats();
showProgress(false);
} catch (e) {
showProgress(false);
error(`Fetch failed: ${e.message}`);
}
}
function escapeHtml(str) {
return str
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll('"', '"')
.replaceAll("'", "'");
}
function formatBytes(bytes) {
if (bytes === 0) return "0 B";
const k = 1024,
sizes = ["B", "KB", "MB", "GB", "TB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${(bytes / Math.pow(k, i)).toFixed(i ? 1 : 0)} ${sizes[i]}`;
}
function updateStats() {
const len = input.value.length;
if (!len) {
stats.textContent = "";
return;
}
const kb = (len / 1024).toFixed(1);
const lines = input.value.split("\n").length;
stats.textContent = `${kb}KB • ${len.toLocaleString()} chars • ${lines.toLocaleString()} lines`;
}
function showProgress(on, percent = 0, msg = "", extra = "") {
progress.classList.toggle("hidden", !on);
btnCancel.classList.toggle("hidden", !on);
btnFormat.disabled = on;
bar.style.width = `${Math.max(0, Math.min(100, percent))}%`;
stage.textContent = msg || "Working…";
detail.textContent = extra || "";
}
function error(msg) {
toast.textContent = msg;
toast.style.background = "#b91c1c";
pop();
}
function info(msg) {
toast.textContent = msg;
toast.style.background = "#0f766e";
pop(2200);
}
function pop(ms = 3200) {
toast.style.opacity = "1";
setTimeout(() => {
toast.style.opacity = "0";
}, ms);
}
function flash(btn, tmp, orig) {
const o = btn.textContent;
btn.textContent = tmp;
setTimeout(() => (btn.textContent = orig || o), 1200);
}
// ADDED: filename + download helpers
function suggestJsonFilename(hint) {
try {
if (hint && hint.startsWith("http")) {
const u = new URL(hint);
const base = (u.pathname.split("/").pop() || "download.json").replace(
/[?#].*$/,
""
);
return ensureJsonExt(base);
}
} catch {}
if (hint && (hint.includes("/") || hint.includes("\\"))) {
const base = hint.split(/[/\\]/).pop() || "download.json";
return ensureJsonExt(base);
}
if (hint && hint.includes(":")) {
const name = hint
.split(":")
.slice(1)
.join(":")
.trim()
.split("•")[0]
.trim();
if (name) return ensureJsonExt(name);
}
return ensureJsonExt("download.json");
}
function ensureJsonExt(name) {
return name.toLowerCase().endsWith(".json")
? name
: name.replace(/\.*$/, "") + ".json";
}
function downloadTextAsFile(text, filename) {
const blob = new Blob([text], { type: "application/json;charset=utf-8" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename || "download.json";
document.body.appendChild(a);
a.click();
a.remove();
setTimeout(() => URL.revokeObjectURL(url), 1000);
}
})();