I'm using javascript Clipboard API to copy an image to the clipboard. It works in Chrome and Edge but not in Safari in spite of official documentation of Safari says that it's supported.
Check the documentation: https://webkit.org/blog/10855/
In this example (not my real code), write() throws an error:
document.getElementById("copy").addEventListener("click", async function() {
const response = await fetch('https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png');
const blob = await response.blob();
navigator.clipboard.write([new ClipboardItem({ "image/png": blob })])
.then(function () { console.log('copied'); })
.catch(function (error) { console.log(error); });
});
The given error is:
NotAllowedError: The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission.
It's because of your await call before calling clipboard.write. Apple doesn't allow this. You have to pass your promise directly into ClipboardItem like this:
document.getElementById("copy").addEventListener("click", async function() {
const makeImagePromise = async () => {
const response = await fetch('https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png');
return await response.blob();
}
navigator.clipboard.write([new ClipboardItem({ "image/png": makeImagePromise() })])
.then(function () { console.log('copied'); })
.catch(function (error) { console.log(error); });
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With