-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
136 lines (116 loc) · 3.75 KB
/
Copy pathscript.js
File metadata and controls
136 lines (116 loc) · 3.75 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
class APIViewer {
constructor() {
this.form = document.getElementById("api-form");
this.apiUrlInput = document.getElementById("api-url");
this.imagePathInput = document.getElementById("image-path");
this.previewImageBtn = document.getElementById("preview-image");
this.outputSection = document.getElementById("output-section");
this.imageSection = document.getElementById("image-section");
this.jsonOutput = document.getElementById("json-output");
this.loader = document.querySelector(".loader");
this.outputImage = document.getElementById("output-image");
this.navLinks = document.querySelectorAll(".nav__link");
this.initEventListeners();
}
initEventListeners() {
this.form.addEventListener("submit", this.handleSubmit.bind(this));
this.previewImageBtn.addEventListener("click", this.previewImage.bind(this));
setInterval(() => {
this.locationString = document.URL.toString();
const [__, tag] = this.locationString.split("#");
this.navLinks.forEach((link) => {
const [__, cleanHref] = link.href.split("#");
if (tag === cleanHref)
link.classList.add("active");
else
link.classList.remove("active");
});
})
}
async handleSubmit(e) {
e.preventDefault();
const url = this.apiUrlInput.value.trim();
if (!this.isValidUrl(url)) {
this.showError("Please enter a valid HTTPS URL");
return;
}
try {
this.toggleLoader(true);
const data = await this.fetchData(url);
this.displayJSON(data);
const imagePath = this.imagePathInput.value;
if (imagePath)
this.previewImage();
this.outputSection.hidden = false;
window.location.hash = "#output-section";
} catch (error) {
this.showError(`Failed to fetch data: ${error.message}`);
} finally {
this.toggleLoader(false);
}
}
async previewImage() {
const imagePath = this.imagePathInput.value.trim();
if (!imagePath) {
this.showError("Please enter image path in JSON");
return;
}
try {
const imageUrl = this.getNestedValue(JSON.parse(this.jsonOutput.textContent), imagePath);
if (typeof imageUrl !== "string")
throw new Error("Invalid image path");
this.outputImage.src = imageUrl;
this.outputImage.onload = () => {
this.imageSection.hidden = false;
window.location.hash = "#image-section";
};
} catch (error) {
this.showError(`Image not found: ${error.message}`);
}
}
async fetchData(url) {
const response = await fetch(url, {
headers: { "Content-Type": "application/json" }
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
}
displayJSON(data) {
this.jsonOutput.textContent = JSON.stringify(data, null, 2);
Prism.highlightElement(this.jsonOutput);
}
getNestedValue(obj, path) {
return path.split(".").reduce((o, p) => o?.[p], obj);
}
isValidUrl(url) {
try {
const urlObj = new URL(url);
return urlObj.protocol === "https:";
} catch {
return false;
}
}
toggleLoader(show) {
this.loader.hidden = !show;
}
showError(message) {
const errorEl = document.createElement("div");
errorEl.className = "error-message";
errorEl.textContent = message;
errorEl.style.color = "var(--error-color)";
this.form.insertAdjacentElement("afterend", errorEl);
setTimeout(() => errorEl.remove(), 3000);
}
}
// Initialize app
new APIViewer();
/**
* Copyright
* Code by Sobhan-SRZA (mr.sinre) | https://github.com/Sobhan-SRZA
* Developed for Persian Caesar | https://github.com/Persian-Caesar | https://dsc.gg/persian-caesar
*
* If you encounter any issues or need assistance with this code,
* please make sure to credit "Persian Caesar" in your documentation or communications.
*/