linkylinky-chrome/main.js

54 lines
2.0 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
});
});
// Calls the api to create a new short url
2021-08-14 13:58:58 +00:00
document.getElementById("create").onclick = createUrl;
2021-08-14 13:21:47 +00:00
function createUrl() {
2021-08-14 13:32:03 +00:00
chrome.tabs.query({ active: true, lastFocusedWindow: true }, tabs => {
let shorturl = {
target: tabs[0].url,
shortcode: document.getElementById("shortcode").value
};
2021-08-14 13:21:47 +00:00
2021-08-14 13:32:03 +00:00
if (shorturl.shortcode == "" | !shorturl.shortcode) {
delete shorturl.shortcode;
}
fetch(`${baseurl}/api`, {
method: "POST",
body: JSON.stringify(shorturl),
headers: new Headers({
'Content-Type': 'application/json'
})
2021-08-14 13:21:47 +00:00
})
2021-08-14 13:32:03 +00:00
.then(res => res.json())
.then(res => {
document.getElementById("shorturl").innerText = res.url;
document.getElementById("shorturl_container").className = "";
document.getElementById("shortcode_container").className = "hidden";
document.getElementById("create").classList.add("hidden");
document.getElementById("reset").classList.remove("hidden");
});
});
2021-08-14 13:21:47 +00:00
}
2021-08-14 13:58:58 +00:00
//Resets the UI
2021-08-14 13:21:47 +00:00
document.getElementById("reset").onclick = reset;
function reset() {
document.getElementById("shorturl").innerText = "";
document.getElementById("shortcode_container").className = "";
document.getElementById("shorturl_container").className = "hidden";
document.getElementById("reset").classList.add("hidden");
document.getElementById("create").classList.remove("hidden");
}