From 97addb21d9fb7a40e548bde85b00a76a32059c78 Mon Sep 17 00:00:00 2001 From: Tiago Ilieve Date: Thu, 28 Sep 2023 13:39:43 -0300 Subject: [PATCH] clip: add support for paste events --- clip/index.html | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/clip/index.html b/clip/index.html index 5284624..2080351 100644 --- a/clip/index.html +++ b/clip/index.html @@ -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 += '

Copied to the clipboard' + } + async function readClipboard() { const text = await navigator.clipboard.readText(); return text.trim(); @@ -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; } @@ -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 += '

Copied to the clipboard' + document.addEventListener('paste', async function (event) { + const content = event.clipboardData.getData('Text'); + await processClipboard(content); });