Skip to content

Commit

Permalink
Add 'clip', clipboard URL cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
myhro committed Sep 27, 2023
1 parent f0ce2e7 commit 2e3f09f
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions clip/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Clipboard URL Cleaner</title>
<style>
body,
html {
height: 100%;
}

body {
display: flex;
flex-direction: column;
justify-content: center;
}

p {
height: 2em;
text-align: center;
}
</style>
</head>

<body>
<p>
<button id="clipboard">Clean URL from Clipboard</button>
</p>
<p id="output"></p>

<script>
async function cleanURL(rawURL) {
const url = new URL(rawURL);
url.search = '';
return url.toString();
}

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

if (!navigator.clipboard) {
output.textContent = 'Clipboard API not supported on this browser';
return;
}

try {
const text = await navigator.clipboard.readText();
const content = text.trim();
if (!content.startsWith('http')) {
output.textContent = 'URL not found in 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.'
} catch (error) {
console.error('Failed to read clipboard:', error);
output.textContent = 'Error reading clipboard!';
}
});
</script>
</body>

</html>

0 comments on commit 2e3f09f

Please sign in to comment.