Capture interactive context for copy to clipboard

This commit is contained in:
Ian Gulliver
2024-12-02 20:35:09 -08:00
parent bdd7593203
commit 7dbddad1bc

View File

@@ -93,7 +93,10 @@ function error(err1, err2) {
document.getElementById('err').show(); document.getElementById('err').show();
} }
async function setFromInputs() { async function setFromInputs(copy = null) {
if (copy == null) {
copy = async (v) => await navigator.clipboard.writeText(v);
}
const short = document.getElementById('short').value; const short = document.getElementById('short').value;
const long = document.getElementById('long').value; const long = document.getElementById('long').value;
@@ -105,10 +108,10 @@ async function setFromInputs() {
document.getElementById('short-icon').setAttribute('name', 'check-square-fill'); document.getElementById('short-icon').setAttribute('name', 'check-square-fill');
document.getElementById('long-icon').setAttribute('name', 'check-square-fill'); document.getElementById('long-icon').setAttribute('name', 'check-square-fill');
await set(short, long); await set(short, long, copy);
} }
async function set(short, long) { async function set(short, long, copy) {
if (short != '') { if (short != '') {
setShortItem(short, 'check-square-fill'); setShortItem(short, 'check-square-fill');
} }
@@ -128,7 +131,7 @@ async function set(short, long) {
}); });
} catch (err) { } catch (err) {
console.log(err); console.log(err);
setTimeout(async () => await set(short, long), 5000); setTimeout(async () => await set(short, long, copy), 5000);
return; return;
} }
@@ -151,7 +154,7 @@ async function set(short, long) {
// Only set the clipboard if the user didn't change the inputs // Only set the clipboard if the user didn't change the inputs
if (document.getElementById('short').value == oldShort && document.getElementById('long').value == oldLong) { if (document.getElementById('short').value == oldShort && document.getElementById('long').value == oldLong) {
try { try {
await navigator.clipboard.writeText(`${window.location.origin}/${newShort}`); await copy(`${window.location.origin}/${newShort}`);
} catch (err) { } catch (err) {
console.log(err); console.log(err);
} }
@@ -237,7 +240,8 @@ document.addEventListener('DOMContentLoaded', async () => {
document.getElementById('short').addEventListener('paste', async () => { document.getElementById('short').addEventListener('paste', async () => {
if (document.getElementById('long').value != '') { if (document.getElementById('long').value != '') {
setTimeout(async () => await setFromInputs(), 0); const copy = async (v) => await navigator.clipboard.writeText(v);
setTimeout(async () => await setFromInputs(copy), 0);
} }
}); });
@@ -261,7 +265,8 @@ document.addEventListener('DOMContentLoaded', async () => {
document.getElementById('long').addEventListener('paste', () => { document.getElementById('long').addEventListener('paste', () => {
if (document.getElementById('short').value != '') { if (document.getElementById('short').value != '') {
setTimeout(async () => await setFromInputs(), 0); const copy = async (v) => await navigator.clipboard.writeText(v);
setTimeout(async () => await setFromInputs(copy), 0);
} }
}); });