I am trying to copy an image to clipboard using JavaScript. I have tried these solutions but get the error:
Uncaught (in promise) DOMException: Document is not focused.
There is this article which shows:
try {
const imgURL = '/images/generic/file.png';
const data = await fetch(imgURL);
const blob = await data.blob();
await navigator.clipboard.write([
new ClipboardItem({
[blob.type]: blob
})
]);
console.log('Image copied.');
} catch(e) {
console.error(e, e.message);
}
But this gives the error:
VM2303:12 TypeError: Failed to fetch "Failed to fetch"
...along with CORS warning.
I have also tried converting an image data URI into a blob and using:
navigator.clipboard.write([
new ClipboardItem({
'image/png': blob
})
])
...but this also gives the following error:
Uncaught (in promise) DOMException: Document is not focused
Note that I only need to do this with local images uploaded client-side (has a data URI as source). So everything is on the same server and requires no back-end.