Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
80 lines (66 loc) · 2.15 KB
/
Copy pathscript.js
File metadata and controls
80 lines (66 loc) · 2.15 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
const errorCheckBtn = document.getElementById("error-check-btn");
const formateBtn = document.getElementById("formate-btn");
const inputField = document.getElementById("inputfield");
const outputField = document.getElementById("outputfield");
const changeValueInput = document.getElementById("change-value");
const needValueInput = document.getElementById("need-value");
const editBtn = document.getElementById("edit-btn");
const tabs = document.getElementById("tabs");
const copyBtn = document.getElementById("copy-btn");
function editvalue(inputValue, changeValue, needValue) {
if (inputValue == "" || changeValue == "" || needValue == "") {
outputField.value = "please enter value to edit";
return;
}
try {
let editedJson = inputValue.replaceAll(changeValue, needValue);
outputField.value = editedJson;
} catch (error) {
// console.log(error);
outputField.value = error;
}
}
editBtn.addEventListener("click", () => {
editvalue(inputField.value, changeValueInput.value, needValueInput.value);
});
async function formateJson(inputValue, tabs) {
if (inputValue == "" ) {
outputField.value = "please enter value to edit";
return;
}
try {
let jsonvalue = JSON.parse(inputValue)
let formattedJson = JSON.stringify(jsonvalue, null, Number(tabs))
outputField.value = formattedJson;
} catch (error) {
// console.log(error);
outputField.value = error;
}
}
formateBtn.addEventListener("click", () => {formateJson(inputField.value, tabs.value)})
async function errorcheck(inputValue) {
if (inputValue == "") {
outputField.value = "please enter JSON code first";
return;
}
try {
let data = await JSON.parse(inputValue);
if (data) {
outputField.value = JSON.stringify(data);
}
} catch (error) {
// console.log(error);
outputField.value = error;
}
}
errorCheckBtn.addEventListener("click", () => {
errorcheck(inputField.value);
});
copyBtn.addEventListener("click",()=> {
navigator.clipboard.writeText(outputField.value).then(() => {
alert("Copied to clipboard");
}).catch(err => {
alert("Failed to copy: " + err);
console.error("Copy failed:", err);
});
})