Skip to content

Commit

Permalink
clip: add support for paste events
Browse files Browse the repository at this point in the history
  • Loading branch information
myhro committed Sep 28, 2023
1 parent 3733641 commit 97addb2
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions clip/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@
return url.toString();
}

async function processClipboard(content) {
if (content == '') {
output.textContent = 'Clipboard is empty';
return;
}

if (!content.startsWith('http')) {
output.textContent = 'URL not found in the clipboard';
return;
}

const url = await cleanURL(content);
navigator.clipboard.writeText(url);
output.textContent = 'Cleaned URL: ' + url;
output.innerHTML += '<br><br> Copied to the clipboard'
}

async function readClipboard() {
const text = await navigator.clipboard.readText();
return text.trim();
Expand All @@ -45,8 +62,8 @@
document.getElementById('clipboard').addEventListener('click', async function () {
const output = document.getElementById('output');

if (!navigator.clipboard) {
output.textContent = 'Clipboard API not supported on this browser';
if (!navigator.clipboard || !navigator.clipboard.readText) {
output.textContent = 'Clipboard API not supported on this browser, please use CTRL+V';
return;
}

Expand All @@ -59,20 +76,12 @@
return;
}

if (content == '') {
output.textContent = 'Clipboard is empty';
return;
}

if (!content.startsWith('http')) {
output.textContent = 'URL not found in the clipboard';
return;
}
await processClipboard(content);
});

const url = await cleanURL(content);
navigator.clipboard.writeText(url);
output.textContent = 'Cleaned URL: ' + url;
output.innerHTML += '<br><br> Copied to the clipboard'
document.addEventListener('paste', async function (event) {
const content = event.clipboardData.getData('Text');
await processClipboard(content);
});
</script>
</body>
Expand Down

0 comments on commit 97addb2

Please sign in to comment.