I am trying to use navigator.clipboard.write(blob)
to copy a DOMString to the clipboard. I am able to use clipboard.writeText('text')
and have it copy, but I am needing text/html.
Example that is failing:
const copy = async () => {
await navigator.permissions.query({name: "clipboard-write"}).then(result => {
if (result.state == "granted" || result.state == "prompt") {
const data = new Blob(['<div>test</div>'], {type : 'text/html'})
navigator.clipboard.write(data);
}
})}
When I run it, I get the following error:
Uncaught (in promise) TypeError: Failed to execute 'write' on 'Clipboard': Iterator getter is not callable.
I have also tried changing text/html to text/plain, which I thought would make it function the same as writeText but did not.
I then attempted wrapping the blob in a new ClipboardItem which I found from a another question:
const data = new Blob(['<div>test</div>'], {type: 'text/html'})
const item = new ClipboardItem({'text/html': data});
navigator.clipboard.write(item);
Any guidance would be appreciated.
The navigator clipboard browser API is used to write various data to the clipboard and also read those data from clipboard. The clipboard is a temporary space to store data and used within or in between applications. These are all async APIs and returns a promise. This is used to develop cut, copy and paste functionalities in apps.
The Clipboard method write () writes arbitrary data, such as images, to the clipboard. This can be used to implement cut and copy functionality. Before you can write to the clipboard, you need to use the Permissions API to get the "clipboard-write" permission.
The Clipboard API can be used to implement cut, copy, and paste features within a web application. Use of the asynchronous clipboard read and write methods requires that the user grant the web site or app permission to access the clipboard.
If the clipboard is empty or doesn't contain text, the "cliptext" element's contents are cleared. This happens because readText () returns an empty string if the clipboard is empty or doesn't contain text.
According to https://www.w3.org/TR/clipboard-apis/#clipboard-interface, Clipboard.write
takes a sequence of ClipboardItem
s. Have you tried something like this instead?
navigator.clipboard.write([item]);
However, note that Chrome (79.0.3945.2/Canary, at the time of writing) does not seem to support writing “text/html” via the clipboard API yet — only “text/plain” and “image/png”.
I spent SO many hours on this and FINALLY figured it out! This works on Chrome, but have no clue if on other browsers. It is all about the event.clipboardData.setData. Hope this helps!
var textString = 'This is plain text';
var htmlString = `<p>${textString}
new line here</p><p>new paragraph</p>`;
var clipboardDataEvt = event.clipboardData;
clipboardDataEvt.setData('text/plain', textString);
clipboardDataEvt.setData('text/html', htmlString);
Make sure it is wrapped within a event listener for the clipboard like Cut, Copy, or Paste as I believe that is what gives you access to the clipboardData event with something like:
document.addEventListener('cut', function (e){});
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