-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
39 lines (39 loc) · 1.44 KB
/
popup.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
let generateBtn = document.querySelector("#shortURL");
let api = document.querySelector("#myurl")
let toastError = document.querySelector('.toast-error')
let toastSuccess = document.querySelector('.toast-success')
let loader = document.querySelector('.loading')
const url = new URL("https://t.ly/api/v1/link/shorten");
let headers = {
"Content-Type": "application/json",
"Accept": "application/json",
}
generateBtn.addEventListener('click', () => {
if (api.value) {
loader.classList.remove('d-hide')
// storing the data
chrome.storage.local.get(['API'], function (result) {
fetch(url, {
method: "POST",
headers: headers,
body: JSON.stringify({
"long_url": api.value,
"domain": "https://t.ly/",
"api_token": result.API
})
}).then(response => response.json())
.then(json => {
loader.classList.add('d-hide')
toastSuccess.classList.remove('d-hide');
toastSuccess.textContent = json.short_url;
})
.catch(err => { alert(err) })
});
} else {
toastError.classList.remove('d-hide')
setTimeout(() => {
toastError.classList.add('d-hide')
}, 1500)
}
})
//pWk9fzB1MUKFuVQkfYXx0vPRWUJbUg79a1eff26ineaYbOmcYftoP3TXFSJl