Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

navigator.clipboard.write : 'Clipboard': Iterator getter is not callable

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.

like image 718
armoredelephant Avatar asked Oct 09 '19 20:10

armoredelephant


People also ask

What is the navigator clipboard browser API used for?

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.

How do I write to the clipboard in JavaScript?

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.

How do I use the clipboard API?

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.

What happens when clipboard is empty or doesn't contain text?

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.


2 Answers

According to https://www.w3.org/TR/clipboard-apis/#clipboard-interface, Clipboard.write takes a sequence of ClipboardItems. 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”.

like image 179
xarant Avatar answered Nov 09 '22 23:11

xarant


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){});
like image 24
Murf Avatar answered Nov 09 '22 21:11

Murf