Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sabesansathananthan/1b1fcc4c94679d0851bfef51b539a5c0 to your computer and use it in GitHub Desktop.
Save sabesansathananthan/1b1fcc4c94679d0851bfef51b539a5c0 to your computer and use it in GitHub Desktop.
Clipboard Operation in JavaScript
const clipboardItems = [];
document.addEventListener("copy", async (e) => {
e.preventDefault();
try {
let clipboardItems = [];
for (const item of e.clipboardData.items) {
if (!item.type.startsWith("image/")) {
continue;
}
clipboardItems.push(
new ClipboardItem({
[item.type]: item,
})
);
await navigator.clipboard.write(clipboardItems);
console.log("Image copied.");
}
} catch (err) {
console.error(err.name, err.message);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment