feature-web: allow pasting of valid URLs from clipboard

This commit is contained in:
Mahesh 2025-07-03 09:26:31 +05:45
parent 4d2c8b0a8c
commit f29b4f7cb5
2 changed files with 29 additions and 20 deletions

View File

@ -2,6 +2,8 @@ const allowedLinkTypes = new Set(["text/plain", "text/uri-list"]);
export const pasteLinkFromClipboard = async () => { export const pasteLinkFromClipboard = async () => {
const clipboard = await navigator.clipboard.read(); const clipboard = await navigator.clipboard.read();
let pastedText = null;
if (clipboard?.length) { if (clipboard?.length) {
const clipboardItem = clipboard[0]; const clipboardItem = clipboard[0];
@ -9,9 +11,20 @@ export const pasteLinkFromClipboard = async () => {
if (allowedLinkTypes.has(type)) { if (allowedLinkTypes.has(type)) {
const blob = await clipboardItem.getType(type); const blob = await clipboardItem.getType(type);
const blobText = await blob.text(); const blobText = await blob.text();
try {
new URL(blobText);
pastedText = blobText;
break;
} catch (e) {
console.warn(`Content '${blobText}' is not a valid URL.`);
}
return blobText; }
} }
if(pastedText){
return pastedText;
} }
} }
} }

View File

@ -9,13 +9,9 @@
"skipLibCheck": true, "skipLibCheck": true,
"sourceMap": true, "sourceMap": true,
"strict": true, "strict": true,
"module": "esnext",
"moduleResolution": "bundler", "moduleResolution": "bundler",
"types": ["turnstile-types"], "types": ["turnstile-types"],
"lib": ["WebWorker"] "lib": ["WebWorker"]
} }
// Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias
// except $lib which is handled by https://kit.svelte.dev/docs/configuration#files
//
// If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes
// from the referenced tsconfig.json - TypeScript does not merge them in
} }