-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
92 lines (82 loc) · 2.51 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
let previewImg = document.getElementById("previewImg");
let submitBtn = document.getElementById("submitBtn");
var imgInput = document.getElementById("file");
let bar = document.getElementById("bar");
let url = document.getElementById("url");
let cpy = document.getElementById("cpy");
let cancel = document.getElementById('x')
let expirationTime = '';
imgInput.addEventListener("change", (e) => {
let i = e.target.files[0];
let img = URL.createObjectURL(i);
previewImg.src = img;
document.body.classList.add("active");
submitBtn.disabled = false;
});
function removeImg() {
imgInput.value = "";
document.body.classList.remove("active");
submitBtn.disabled = true;
document.body.classList.remove("success");
cpy.classList.replace("bx-check", "bx-copy");
bar.style.width = 0;
}
function addExpirationTime(e){
expirationTime = `expiration=${e.value}&`
console.log(expirationTime)
}
document
.getElementById("uploadForm")
.addEventListener("submit", function (event) {
event.preventDefault(); // Prevent form submission
var formData = new FormData();
var imageFile = imgInput.files[0];
formData.append("image", imageFile);
document.body.classList.add("upload");
cancel.style.display = 'none'
let api = `https://api.imgbb.com/1/upload?${expirationTime}key=dbc263178e5cda48ca6b54a98e6c968e`
fetch(
api,
{
method: "POST",
body: formData,
}
)
.then((response) => response.json())
.then((data) => {
// Handle response from ImgBB API
if (data && data.data && data.data.url) {
previewImg.src = data.data.url;
url.innerHTML = data.data.url;
document.body.classList.remove("upload");
document.body.classList.add("success");
submitBtn.disabled = true;
cancel.style.display = 'block'
Swal.fire({
title: "Uploaded",
text: "click the copy button to copy imagelink",
icon: "success"
});
} else {
Swal.fire({
title: "Upload Failed",
text: "somthing went wrong",
icon: "error"
});
cancel.click()
}
})
.catch((error) => {
console.error("Error:", error);
Swal.fire({
title: "Upload Failed",
text: "somthing went wrong",
icon: "error"
});
cancel.click()
});
});
cpy.addEventListener("click", () => {
navigator.clipboard.writeText(url.textContent);
cpy.classList.replace("bx-copy", "bx-check");
});