[Webkit-unassigned] [Bug 202136] Feature Request: Implement the Clipboard API and related events

bugzilla-daemon at webkit.org bugzilla-daemon at webkit.org
Wed Oct 2 01:17:05 PDT 2019


https://bugs.webkit.org/show_bug.cgi?id=202136

--- Comment #2 from Thomas Steiner <tomac at google.com> ---
In the simplest shape, this is about programmatically copying and pasting text with the Async Clipboard API:

```js
async function copyPageURL() {
  try {
    await navigator.clipboard.writeText(location.href);
    console.log('Page URL copied to clipboard');
  } catch (err) {
    console.error('Failed to copy: ', err);
  }
}

async function getClipboardText() {
  try {
    const text = await navigator.clipboard.readText();
    console.log('Clipboard contents: ', text);
  } catch (err) {
    console.error('Failed to read clipboard contents: ', err);
  }
}
```


In the advanced form, this is about copying and pasting binary data, like PNG images:

```js
try {
  const imgURL = 'https://developers.google.com/web/updates/images/generic/file.png';
  const data = await fetch(imgURL);
  const blob = await data.blob();
  await navigator.clipboard.write([
    new ClipboardItem(Object.defineProperty({}, blob.type, {
      value: blob,
      enumerable: true
    }))
  ]);
  console.log('Image copied.');
} catch(e) {
  console.error(e, e.message);
}

async function getClipboardContents() {
  try {
    const clipboardItems = await navigator.clipboard.read();
    for (const clipboardItem of clipboardItems) {
      try {
        for (const type of clipboardItem.types) {
          const blob = await clipboardItem.getType(type);
          console.log(URL.createObjectURL(blob));
        }
      } catch (e) {
        console.error(e, e.message);
      }
    }
  } catch (e) {
    console.error(e, e.message);
  }
}
```

On Chrome, we re-encode images for security reasons and currently only support PNG images, but eventually want to broaden this to more image formats, and ultimately anything (but it's still in the farther away future).

-- 
You are receiving this mail because:
You are the assignee for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-unassigned/attachments/20191002/a5bbd604/attachment.html>


More information about the webkit-unassigned mailing list