-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
63 lines (58 loc) · 2.38 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
const form = document.querySelector(".wrapper form"),
fullURL = form.querySelector("input"),
shortenBtn = form.querySelector("form button"),
blurEffect = document.querySelector(".blur-effect"),
popupBox = document.querySelector(".popup-box"),
infoBox = popupBox.querySelector(".info-box"),
form2 = popupBox.querySelector("form"),
shortenURL = popupBox.querySelector("form .shorten-url"),
copyIcon = popupBox.querySelector("form .copy-icon"),
saveBtn = popupBox.querySelector("button");
form.onsubmit = (e)=>{
e.preventDefault();
}
shortenBtn.onclick = ()=>{
let xhr = new XMLHttpRequest();
xhr.open("POST", "php/url-controll.php", true);
xhr.onload = ()=>{
if(xhr.readyState == 4 && xhr.status == 200){
let data = xhr.response;
if(data.length <= 5){
blurEffect.style.display = "block";
popupBox.classList.add("show");
let domain = "localhost/url_shortner/";
shortenURL.value = domain + data;
copyIcon.onclick = ()=>{
shortenURL.select();
document.execCommand("copy");
}
saveBtn.onclick = ()=>{
form2.onsubmit = (e)=>{
e.preventDefault();
}
let xhr2 = new XMLHttpRequest();
xhr2.open("POST", "php/save-url.php", true);
xhr2.onload = ()=>{
if(xhr2.readyState == 4 && xhr2.status == 200){
let data = xhr2.response;
if(data == "success"){
location.reload();
}else{
infoBox.classList.add("error");
infoBox.innerText = data;
}
}
}
let shorten_url1 = shortenURL.value;
let hidden_url = data;
xhr2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr2.send("shorten_url="+shorten_url1+"&hidden_url="+hidden_url);
}
}else{
alert(data);
}
}
}
let formData = new FormData(form);
xhr.send(formData);
}