Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
myhro committed Sep 27, 2023
1 parent d75c714 commit 3a0a696
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions clip/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
return url.toString();
}

async function readClipboard() {
const text = await navigator.clipboard.readText();
return text.trim();
}

document.getElementById('clipboard').addEventListener('click', async function () {
const output = document.getElementById('output');

Expand All @@ -45,27 +50,29 @@
return;
}

let content = '';
try {
const text = await navigator.clipboard.readText();
const content = text.trim();
if (!content.startsWith('http')) {
output.textContent = 'URL not found in the clipboard';
return;
}

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

const url = await cleanURL(content);
navigator.clipboard.writeText(url);
output.textContent = 'Cleaned URL: ' + url;
output.innerHTML += '<br><br> Copied to the clipboard'
content = await readClipboard();
} catch (error) {
console.error('Failed to read clipboard:', error);
output.textContent = 'Error reading clipboard';
return;
}

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

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

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

0 comments on commit 3a0a696

Please sign in to comment.