Created
March 5, 2021 08:17
-
-
Save sabesansathananthan/1b1fcc4c94679d0851bfef51b539a5c0 to your computer and use it in GitHub Desktop.
Clipboard Operation in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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