From 9d2f8c7e16f20d2fb393fc1b3619b545500d219d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Th=C3=A9riault?= Date: Mon, 28 Oct 2019 10:49:02 -0400 Subject: [PATCH] Copy URLs to clipboard instead of opening them (fixes #4) --- resources/script.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/resources/script.js b/resources/script.js index cbd9359..664a9ab 100644 --- a/resources/script.js +++ b/resources/script.js @@ -59,6 +59,19 @@ const randomUrl = () => { return Math.floor(Math.random() * 2147483647).toString(36); }; +const updateClipboard = (data) => { + navigator.permissions + .query({ name: "clipboard-write" }) + .then((result) => { + if (result.state === "denied") { + throw new Error(); + } + }) + .then(() => navigator.clipboard.writeText(data)) + .then(() => alert("URL copied to clipboard")) + .catch(() => alert(data)); +}; + for (const group in tabs) { tabs[group][0].onclick = () => { const active = document.querySelectorAll(".active"); @@ -75,7 +88,7 @@ for (const group in inputs) { const submitButton = inputs[group][inputs[group].length - 1]; const urlInput = inputs[group][0]; - urlInput.addEventListener("input", () => { + urlInput.addEventListener("input", (e) => { if (urlInput.value[urlInput.value.length - 1] === " ") { urlInput.value = randomUrl(); checkValidity(); @@ -171,7 +184,7 @@ for (const group in inputs) { if (status !== 201) { throw new Error(text); } else { - window.open(url, "_blank"); + updateClipboard(url); clearInputs(); filesValueInput.value = ""; fetchUsed(); @@ -201,7 +214,7 @@ for (const group in inputs) { if (status !== 201) { throw new Error(text); } else { - window.open(url, "_blank"); + updateClipboard(url); clearInputs(); fetchUsed(); } @@ -228,7 +241,7 @@ for (const group in inputs) { if (status !== 201) { throw new Error(text); } else { - window.open(url, "_blank"); + updateClipboard(url); clearInputs(); fetchUsed(); }