-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
263 lines (241 loc) · 10.3 KB
/
popup.js
File metadata and controls
263 lines (241 loc) · 10.3 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
document.addEventListener("DOMContentLoaded", () => {
// چک کردن لود شدن X2JS
if (typeof window.X2JS === "undefined") {
console.error("X2JS is not loaded properly");
document.getElementById("output").textContent = "Error: Required library (X2JS) is not loaded";
return;
}
// چک کردن لود شدن jsyaml
if (typeof jsyaml === "undefined") {
console.error("jsyaml is not loaded properly");
document.getElementById("output").textContent = "Error: Required library (js-yaml) is not loaded";
return;
}
const x2js = new window.X2JS(); // ساختن نمونه از X2JS
const tabLinks = document.querySelectorAll(".tab-link");
const tabContents = document.querySelectorAll(".tab-content");
const input = document.getElementById("input");
const output = document.getElementById("output");
const xmlToJsonBtn = document.getElementById("xmlToJsonBtn");
const jsonToXmlBtn = document.getElementById("jsonToXmlBtn");
const yamlToJsonBtn = document.getElementById("yamlToJsonBtn");
const jsonToYamlBtn = document.getElementById("jsonToYamlBtn");
const hexToRgbBtn = document.getElementById("hexToRgbBtn");
const rgbToHexBtn = document.getElementById("rgbToHexBtn");
const colorPreview = document.getElementById("colorPreview");
const minifyBtn = document.getElementById("minifyBtn");
const unminifyBtn = document.getElementById("unminifyBtn");
const copyOutputBtn = document.getElementById("copyOutputBtn");
const copyCssRgbBtn = document.getElementById("copyCssRgbBtn");
// مدیریت تبها
tabLinks.forEach(link => {
link.addEventListener("click", () => {
tabLinks.forEach(l => l.classList.remove("active"));
tabContents.forEach(c => c.classList.remove("active"));
link.classList.add("active");
const tabId = link.getAttribute("data-tab");
document.getElementById(tabId).classList.add("active");
input.value = "";
output.textContent = "";
if (tabId !== "colorTab") {
colorPreview.style.display = "block";
}
});
});
// XML: JSON to XML
jsonToXmlBtn.addEventListener("click", () => {
try {
const inputData = input.value.trim();
if (!inputData) {
output.textContent = "Error: Input is empty";
return;
}
if (!inputData.startsWith("{") && !inputData.startsWith("[")) {
output.textContent = "Error: Input must be a valid JSON (starting with { or [)";
return;
}
const jsonData = JSON.parse(inputData);
const wrappedJson = { root: jsonData }; // اضافه کردن root
const xmlData = x2js.json2xml_str(wrappedJson);
output.textContent = xmlData;
} catch (e) {
output.textContent = `Error: Invalid JSON - ${e.message}`;
}
});
// XML: XML to JSON
xmlToJsonBtn.addEventListener("click", () => {
try {
const xmlData = input.value.trim();
if (!xmlData) {
output.textContent = "Error: Input is empty";
return;
}
if (!xmlData.startsWith("<")) {
output.textContent = "Error: Input must be a valid XML (starting with <)";
return;
}
const jsonData = x2js.xml_str2json(xmlData);
if (jsonData === null) {
output.textContent = "Error: Invalid XML";
} else {
if (jsonData && jsonData.root) {
output.textContent = JSON.stringify(jsonData.root, null, 2); // حذف root
} else {
output.textContent = JSON.stringify(jsonData, null, 2);
}
}
} catch (e) {
output.textContent = `Error: Invalid input - ${e.message}`;
}
});
// YAML: YAML to JSON
yamlToJsonBtn.addEventListener("click", () => {
try {
const yamlData = input.value.trim();
if (!yamlData) {
output.textContent = "Error: Input is empty";
return;
}
const jsonData = jsyaml.load(yamlData);
output.textContent = JSON.stringify(jsonData, null, 2);
} catch (e) {
output.textContent = `Error: Invalid YAML - ${e.message}`;
}
});
// YAML: JSON to YAML
jsonToYamlBtn.addEventListener("click", () => {
try {
const inputData = input.value.trim();
if (!inputData) {
output.textContent = "Error: Input is empty";
return;
}
if (!inputData.startsWith("{") && !inputData.startsWith("[")) {
output.textContent = "Error: Input must be a valid JSON (starting with { or [)";
return;
}
const jsonData = JSON.parse(inputData);
const yamlData = jsyaml.dump(jsonData);
output.textContent = yamlData;
} catch (e) {
output.textContent = `Error: Invalid JSON - ${e.message}`;
}
});
// Hex to RGB
hexToRgbBtn.addEventListener("click", () => {
try {
const hex = document.getElementById("colorInput").value.trim();
const hexRegex = /^#?([0-9A-Fa-f]{6}|[0-9A-Fa-f]{3})$/;
if (!hexRegex.test(hex)) {
output.textContent = "Error: Invalid Hex color code";
return;
}
const cleanHex = hex.replace("#", "");
const fullHex = cleanHex.length === 3 ? cleanHex.split("").map(char => char + char).join("") : cleanHex;
const r = parseInt(fullHex.substring(0, 2), 16);
const g = parseInt(fullHex.substring(2, 4), 16);
const b = parseInt(fullHex.substring(4, 6), 16);
document.getElementById("red255").value = r;
document.getElementById("green255").value = g;
document.getElementById("blue255").value = b;
const r1 = (r / 255).toFixed(8);
const g1 = (g / 255).toFixed(8);
const b1 = (b / 255).toFixed(8);
document.getElementById("red1").value = r1;
document.getElementById("green1").value = g1;
document.getElementById("blue1").value = b1;
document.getElementById("cssRgb255").value = `rgb(${r}, ${g}, ${b})`;
document.getElementById("cssRgbPercent").value = `rgb(${(r1 * 100).toFixed(2)}%, ${(g1 * 100).toFixed(2)}%, ${(b1 * 100).toFixed(2)}%)`;
document.getElementById("colorPreview").style.backgroundColor = `rgb(${r}, ${g}, ${b})`;
output.textContent = `rgb(${r}, ${g}, ${b})`;
} catch (e) {
output.textContent = "Error: Invalid input";
}
});
// Minify/Unminify: Minify
minifyBtn.addEventListener("click", () => {
const inputData = input.value.trim();
if (!inputData) {
output.textContent = "Error: Input is empty";
return;
}
try {
const parsed = JSON.parse(inputData);
const minified = JSON.stringify(parsed);
output.textContent = minified;
} catch (e) {
output.textContent = `Error: Invalid JSON - ${e.message}`;
}
});
// Minify/Unminify: Unminify
unminifyBtn.addEventListener("click", () => {
const inputData = input.value.trim();
if (!inputData) {
output.textContent = "Error: Input is empty";
return;
}
try {
const parsed = JSON.parse(inputData);
const unminified = JSON.stringify(parsed, null, 2);
output.textContent = unminified;
} catch (e) {
output.textContent = `Error: Invalid JSON - ${e.message}`;
}
});
function showNotification(message) {
const notification = document.getElementById("notification");
notification.textContent = message;
notification.classList.add("show");
setTimeout(() => {
notification.classList.remove("show");
}, 2000); // محو شدن بعد از 2 ثانیه
}
// Copy CSS RGB (0-255)
function setupCopyCssRgbBtn() {
const copyCssRgbBtn = document.getElementById("copyCssRgbBtn");
if (copyCssRgbBtn) {
copyCssRgbBtn.addEventListener("click", () => {
const textToCopy = document.getElementById("cssRgb255").value;
if (textToCopy) {
navigator.clipboard.writeText(textToCopy).then(() => {
showNotification("CSS RGB color copied!");
}).catch(() => {
showNotification("Error: Could not copy!");
});
} else {
showNotification("Nothing to copy!");
}
});
} else {
console.error("Could not find copyCssRgbBtn");
}
}
// Copy Output
copyOutputBtn.addEventListener("click", () => {
const textToCopy = output.textContent;
if (textToCopy) {
navigator.clipboard.writeText(textToCopy).then(() => {
showNotification("Output copied!");
}).catch(() => {
showNotification("Error: Could not copy!");
});
} else {
showNotification("Nothing to copy!");
}
});
// Copy CSS RGB (0-255)
copyCssRgbBtn.addEventListener("click", () => {
console.log('textToCopy 2222: ')
const textToCopy = document.getElementById("cssRgb255").value;
console.log('textToCopy : ', textToCopy)
if (textToCopy) {
navigator.clipboard.writeText(textToCopy).then(() => {
setupCopyCssRgbBtn("CSS RGB color copied to clipboard!");
}).catch(() => {
setupCopyCssRgbBtn("Error: Could not copy to clipboard");
});
} else {
setupCopyCssRgbBtn("Nothing to copy!");
}
});
});