Skip to content

Commit

Permalink
fix: text file web paste
Browse files Browse the repository at this point in the history
  • Loading branch information
ozwaldorf committed Oct 14, 2024
1 parent dcdb28d commit ca90edf
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,30 @@
}}
</style>
<script nonce="{nonce}">
// Listen for paste events and upload them, setting the page's text to the list of links
document.addEventListener('paste', async (event) => {{
const preElement = document.querySelector('pre');
const responses = await Promise.all([...event.clipboardData.items]
.filter(item => item.kind === 'file' || item.kind === 'string')
.map(async item => {{
const file = item.getAsFile();
const uploadUrl = `/${{file.name}}`;
const file = item.getAsFile() || item.getAsString();
if (!file)
return `Error reading from clipboard`;
const uploadUrl = `/${{typeof file === "object" ? file.name : "" }}`;
try {{
const response = await fetch(uploadUrl, {{
method: 'PUT',
body: file
}});
const responseBody = await response.text();
return response.ok
? `<a href="${{responseBody}}" target="_blank">${{responseBody}}</a>`
: `Failed to upload "${{file.name}}": ${{response.statusText}}`;
? `<a href="${{responseBody}}">${{responseBody}}</a>`
: `Failed to upload "${{uploadUrl}}": ${{response.statusText}}`;
}} catch (error) {{
return `Error uploading "${{file.name}}": ${{error.message}}`;
return `Error uploading "${{uploadUrl}}": ${{error.message}}`;
}}
}})
);
preElement.innerHTML = responses.join('\n');
document.querySelector('pre').innerHTML = responses.join('\n');
}});
</script>
</head>
Expand Down

0 comments on commit ca90edf

Please sign in to comment.