Skip to content

Commit

Permalink
feat(index.html): replace urls with links
Browse files Browse the repository at this point in the history
  • Loading branch information
ozwaldorf committed Oct 15, 2024
1 parent 256a56a commit bb0b513
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
max-width: 73ch;
margin: 0 auto;
}}
a {{ color: #78a9ff; }}
</style>
<script nonce="{nonce}">
// Upload a file and return the url
async function upload(data, name = "") {{
const uploadUrl = `/${{name}}`;
try {{
Expand All @@ -39,16 +41,16 @@
return `Error uploading "${{uploadUrl}}": ${{error.message}}`;
}}
}}

// Listen for ctrl/cmd + V
document.addEventListener('paste', async (event) => {{
const preElement = document.querySelector('pre');
preElement.innerHTML = "Uploading...";
// Handle text data
const text = event.clipboardData.getData("text");
if (text) {{
preElement.innerHTML = await upload(text);
return;
}}
// Or handle files
const responses = await Promise.all([...event.clipboardData.items]
.filter(item => item.kind === 'file')
.map(async item => {{
Expand All @@ -60,6 +62,13 @@
);
preElement.innerHTML = responses.join('\n');
}});


// Replace urls with links on page load
document.addEventListener('DOMContentLoaded', () => {{
const preElement = document.querySelector('pre');
preElement.innerHTML = preElement.innerHTML.replace(/((https:)[^\s]+[\w])/g, '<a href="$1" target="_blank">$1</a>');
}}, false);
</script>
</head>
<body>
Expand Down

0 comments on commit bb0b513

Please sign in to comment.