linkylinky-chrome/src/main.js

109 lines
4.3 KiB
JavaScript
Raw Normal View History

2021-08-14 13:21:47 +00:00
//Clipboard stuff
const baseurl = "https://kauft.es";
const btn = document.getElementById("shorturl_copy");
const clipboard = new ClipboardJS(btn);
clipboard.on('success', function (e) {
console.info('Action:', e.action);
console.info('Text:', e.text);
2021-08-14 13:31:52 +00:00
new Notification('Copied Shorturl', {
2021-08-14 13:34:49 +00:00
icon: 'https://git.odit.services/user/avatar/kauft.es/140',
2021-08-14 13:31:52 +00:00
body: `Copied the shorturl to your clipboard: ${e.text}`,
2021-08-14 13:21:47 +00:00
});
});
2021-08-17 17:46:07 +00:00
//Set default url as current tab
chrome.tabs.query({ active: true, lastFocusedWindow: true }, tabs => {
document.getElementById("target").value = tabs[0].url;
});
2021-08-17 17:06:03 +00:00
2021-08-17 17:46:07 +00:00
// Create button click handler calls the api to create a new short url
2021-08-14 13:58:58 +00:00
document.getElementById("create").onclick = createUrl;
2021-08-17 17:46:07 +00:00
/**
* Calls the api to create a new short url and handels displaying the ui for "created" urls
*/
2021-08-14 13:21:47 +00:00
function createUrl() {
let shorturl = {
target: document.getElementById("target").value,
shortcode: document.getElementById("shortcode").value,
no_preview: document.getElementById("bot_toggle").checked,
clientside: document.getElementById("clientside_toggle").checked
};
2021-08-14 13:21:47 +00:00
if (shorturl.shortcode == "" | !shorturl.shortcode) {
delete shorturl.shortcode;
}
2021-08-14 13:32:03 +00:00
fetch(`${baseurl}/api`, {
method: "POST",
body: JSON.stringify(shorturl),
headers: new Headers({
'Content-Type': 'application/json'
})
})
.then(res => {
if (!res.ok) {
handleError(res);
}
else {
2021-08-21 07:34:14 +00:00
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("bot_container").classList.add("hidden");
2021-09-25 16:50:00 +00:00
document.getElementById("clientside_container").classList.add("hidden");
2021-08-21 07:34:14 +00:00
document.getElementById("create").classList.add("hidden");
document.getElementById("reset").classList.remove("hidden");
document.getElementById("bot_toggle").checked = false;
document.getElementById("clientside_toggle").checked = false;
2021-08-21 07:34:14 +00:00
});
}
});
2021-08-14 13:32:03 +00:00
2021-08-14 13:21:47 +00:00
}
2021-08-17 17:46:07 +00:00
//Add error dismiss event handler
2021-08-17 17:43:39 +00:00
document.getElementById("error_dismiss").onclick = () => {
2021-08-17 17:42:02 +00:00
document.getElementById("error_container").classList.add("hidden");
};
2021-08-17 17:46:07 +00:00
2021-08-17 17:42:11 +00:00
/**
* Display alerts for errors (only handling 400 correctly r/n)
* @param {response} res The fetch response object
*/
function handleError(res) {
if (res.status == 400) {
2021-08-17 17:43:39 +00:00
res.text().then((body) => {
document.getElementById("error_text").innerText = body;
2021-08-17 17:42:02 +00:00
document.getElementById("error_container").classList.remove("hidden");
})
}
2021-08-17 17:43:39 +00:00
else {
console.log(res);
document.getElementById("error_text").innerText = "An unknown error occured (see console for more details)";
document.getElementById("error_container").classList.remove("hidden");
}
}
2021-08-17 17:46:07 +00:00
//Add ui reset error handler
2021-08-14 13:21:47 +00:00
document.getElementById("reset").onclick = reset;
2021-08-17 17:46:07 +00:00
/**
* Resets the ui by emtying/resetting all inputs and showing the "default" home ui containers again.
*/
2021-08-14 13:21:47 +00:00
function reset() {
document.getElementById("shorturl").innerText = "";
document.getElementById("shorturl_container").className = "hidden";
document.getElementById("reset").classList.add("hidden");
2021-08-17 17:06:28 +00:00
chrome.tabs.query({ active: true, lastFocusedWindow: true }, tabs => {
document.getElementById("target").value = tabs[0].url;
document.getElementById("target_container").className = "";
document.getElementById("shortcode_container").className = "";
2021-09-25 16:50:00 +00:00
document.getElementById("bot_container").classList.remove("hidden");
document.getElementById("bot_container").value = false;
2021-09-25 16:50:00 +00:00
document.getElementById("clientside_container").classList.remove("hidden");
document.getElementById("clientside_container").value = false;
document.getElementById("create").classList.remove("hidden");
2021-08-17 17:06:28 +00:00
});
2021-08-14 13:21:47 +00:00
}