Added basic function for handling errors
This commit is contained in:
parent
9d0e64ff24
commit
94b38993e9
74
main.js
74
main.js
|
@ -12,41 +12,65 @@ clipboard.on('success', function (e) {
|
|||
});
|
||||
|
||||
//Set default url
|
||||
chrome.tabs.query({ active: true, lastFocusedWindow: true }, tabs => {
|
||||
document.getElementById("target").value = tabs[0].url;
|
||||
});
|
||||
// chrome.tabs.query({ active: true, lastFocusedWindow: true }, tabs => {
|
||||
// document.getElementById("target").value = tabs[0].url;
|
||||
// });
|
||||
document.getElementById("target").value = "google.com";
|
||||
|
||||
// Calls the api to create a new short url
|
||||
document.getElementById("create").onclick = createUrl;
|
||||
function createUrl() {
|
||||
let shorturl = {
|
||||
target: document.getElementById("target").value,
|
||||
shortcode: document.getElementById("shortcode").value
|
||||
};
|
||||
let shorturl = {
|
||||
target: document.getElementById("target").value,
|
||||
shortcode: document.getElementById("shortcode").value
|
||||
};
|
||||
|
||||
if (shorturl.shortcode == "" | !shorturl.shortcode) {
|
||||
delete shorturl.shortcode;
|
||||
}
|
||||
if (shorturl.shortcode == "" | !shorturl.shortcode) {
|
||||
delete shorturl.shortcode;
|
||||
}
|
||||
|
||||
fetch(`${baseurl}/api`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify(shorturl),
|
||||
headers: new Headers({
|
||||
'Content-Type': 'application/json'
|
||||
})
|
||||
fetch(`${baseurl}/api`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify(shorturl),
|
||||
headers: new Headers({
|
||||
'Content-Type': 'application/json'
|
||||
})
|
||||
.then(res => res.json())
|
||||
.then(res => {
|
||||
document.getElementById("shorturl").innerText = res.url;
|
||||
document.getElementById("shorturl_container").className = "";
|
||||
document.getElementById("target_container").className = "hidden";
|
||||
document.getElementById("shortcode_container").className = "hidden";
|
||||
document.getElementById("create").classList.add("hidden");
|
||||
document.getElementById("reset").classList.remove("hidden");
|
||||
});
|
||||
})
|
||||
.then(res => {
|
||||
if (!res.ok) {
|
||||
handleError(res);
|
||||
}
|
||||
else {
|
||||
res.json()
|
||||
}
|
||||
})
|
||||
.then(res => {
|
||||
document.getElementById("shorturl").innerText = res.url;
|
||||
document.getElementById("shorturl_container").className = "";
|
||||
document.getElementById("target_container").className = "hidden";
|
||||
document.getElementById("shortcode_container").className = "hidden";
|
||||
document.getElementById("create").classList.add("hidden");
|
||||
document.getElementById("reset").classList.remove("hidden");
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
function handleError(res) {
|
||||
if (res.status == 400) {
|
||||
res.text().then((body)=>{
|
||||
switch (body) {
|
||||
case "Shortcode already exists, please choose another code":
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
break;
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
//Resets the UI
|
||||
document.getElementById("reset").onclick = reset;
|
||||
function reset() {
|
||||
|
|
Loading…
Reference in New Issue