-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
163 lines (127 loc) · 4.49 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
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
var input = document.querySelector('#link-form');
var loading = document.querySelector('.load-cover');
var alertBox = document.querySelector('.alert-box');
var alertBoxText = document.querySelector('.alert-box-text');
var shortenedLinksHolder = document.querySelector('.shortened-links');
var linksLog = [];
// mobile nav function
function mobileNav(){
document.querySelector('.burger').addEventListener('click',()=>{
document.querySelector('.mobile-nav').classList.toggle('mobile-nav-display');
})
};
// functions for handling input
function textInput(){
input.addEventListener('keyup',(e)=>{
if(input.value.length === 0 && e.key === "Enter"){
setError('Field cannot be empty!')
}
else if(e.keycode ==13 || e.key === "Enter"){
if(!isValidHttpUrl(input.value)){
setError('Please enter a valid URL with the http!');
}
else{
inputSearch(e.target.value);
input.value ='';
}
}
})
};
textInput();
// shorten button function
document.querySelector('.shorten-now').addEventListener('click',()=>{
if(input.value.length === 0){
setError('Field cannot be empty!')
}
else if(!isValidHttpUrl(input.value)){
setError('Please enter a valid URL with the http!');
}
else{
inputSearch(input.value);
input.value ='';
}
});
function inputSearch(linkAddress){
apiShorty(linkAddress)
}
// localstorage
//save to local storage
function saveLink(){
var str = JSON.stringify(linksLog);
localStorage.setItem('linkHolder', str);
}
// get data from localStorage
function getLink(){
var str = localStorage.getItem('linkHolder');
linksLog = JSON.parse(str);
if(!linksLog){
linksLog = [];
}
};
function linkList(value, shortenedValue){
var linkHolder = document.createElement('div');
linkHolder.classList.add('link-list');
var textReceived = document.createElement('p');
textReceived.classList.add('text-input');
textReceived.textContent = value;
var copySection = document.createElement('div');
copySection.classList.add('right');
var output = document.createElement('p');
output.classList.add('shortened-output');
output.textContent = shortenedValue;
var copyText = document.createElement('div');
copyText.textContent = 'Copy';
copyText.classList.add('copy-shortened', 'click');
copySection.appendChild(output);
copySection.appendChild(copyText)
linkHolder.appendChild(textReceived);
linkHolder.appendChild(copySection);
shortenedLinksHolder.appendChild(linkHolder);
loading.style.display = 'none';
copyText.onclick = function(){copyToClipboard(output, copyText)};
};
// APi function
async function apiShorty(value) {
loading.style.display = 'flex';
const response = await fetch('https://api.shrtco.de/v2/shorten?url='+value);
const data = await response.json();
var shortenedValue = data.result.short_link;
linksLog.push({value: value , shortened: shortenedValue});
saveLink();
linkList(value, shortenedValue);
}
// copy to clipbpoard function
function copyToClipboard(output, copyText){
var tempInput = document.createElement("input");
tempInput.style = "position: absolute; left: -1000px; top: -1000px";
tempInput.value = output.textContent;
document.body.appendChild(tempInput);
tempInput.select();
document.execCommand("copy");
document.body.removeChild(tempInput);
copyText.textContent = 'Copied';
setTimeout(()=>{copyText.textContent = 'Copy';}, 2000);
}
function isValidHttpUrl(string) {
if (string && string.length > 1 && string.slice(0, 2) == '//') {
string = 'http:' + string; //dummy protocol so that URL works
}
try {
var url = new URL(string);
return url.hostname && url.hostname.match(/^([a-z0-9])(([a-z0-9-]{1,61})?[a-z0-9]{1})?(\.[a-z0-9](([a-z0-9-]{1,61})?[a-z0-9]{1})?)?(\.[a-zA-Z]{2,4})+$/) ? true : false;
} catch (_) {
return false;
}
}
function setError(message){
alertBoxText.textContent = message;
alertBox.style.display = 'flex';
setTimeout(closeError, 1000);
}
function closeError(){
alertBox.style.display = 'none';
}
getLink();
for(var i =0; i<linksLog.length; i++){
linkList(linksLog[i].value, linksLog[i].shortened);
};