forked from xyTom/Url-Shorten-Worker
-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.js
66 lines (62 loc) · 2.1 KB
/
main.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
let res
function shorturl() {
if(document.querySelector("#text").value==""){
alert("Url cannot be empty!")
return
}
document.getElementById("searchbtn").disabled=true;
document.getElementById("searchbtn").innerHTML='<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>Please wait...';
fetch(window.location.pathname, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ url: document.querySelector("#text").value })
}).then(function(response) {
return response.json();
})
.then(function(myJson) {
res = myJson;
document.getElementById("searchbtn").disabled=false;
document.getElementById("searchbtn").innerHTML=' Shorten it';
if(res.key!=="")
document.getElementById("result").innerHTML=window.location.host+res.key;
$('#exampleModal').modal('show')
}).catch(function(err){alert("Unknow error. Please retry!");
console.log(err);
document.getElementById("searchbtn").disabled=false;
document.getElementById("searchbtn").innerHTML=' Shorten it';})
}
function copyurl (id, attr) {
let target = null;
if (attr) {
target = document.createElement('div');
target.id = 'tempTarget';
target.style.opacity = '0';
if (id) {
let curNode = document.querySelector('#' + id);
target.innerText = curNode[attr];
} else {
target.innerText = attr;
}
document.body.appendChild(target);
} else {
target = document.querySelector('#' + id);
}
try {
let range = document.createRange();
range.selectNode(target);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
document.execCommand('copy');
window.getSelection().removeAllRanges();
console.log('Copy success')
} catch (e) {
console.log('Copy error')
}
if (attr) {
// remove temp target
target.parentElement.removeChild(target);
}
}
$(function () {
$('[data-toggle="popover"]').popover()
})