Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Clipboard.read — asynchronous clipboard reading

The navigator.clipboard.read() method reads clipboard items and their MIME representations. It requires a secure context and browser authorization to access the clipboard.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

navigator.clipboard.read()
navigator.clipboard.read({ unsanitized: ["text/html"] })

Clipboard.read, called as navigator.clipboard.read(), returns a Promise for an array of ClipboardItem objects, not text. item.types lists MIME types; await item.getType(type) provides a Blob. By contrast, readText() reads text as a string.

This is a Web API, not an ECMAScript feature. It requires a secure context and an active, focused document. Invoke it following user interaction; a click does not guarantee access. Denial can cause NotAllowedError. Detecting the method does not establish permission; Node 24 does not prove browser support.

The option { unsanitized: ["text/html"] } requests unsanitized HTML but does not guarantee that result. Treat clipboard data as untrusted; do not insert it directly through innerHTML. The example displays text through textContent.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Clipboard.read without clipboard access</title>
</head>
<body>
  <p id="result" role="status"></p>
  <script>
    const frame = document.createElement('iframe');
    frame.title = 'Clipboard.read permission test';
    frame.hidden = true;
    frame.allow = "clipboard-read 'none'";
    frame.srcdoc = `<!doctype html>
      <html lang="en"><head><meta charset="utf-8">
      <title>Clipboard.read test</title></head><body>
      <script>
        (async () => {
          const blocked = 'Clipboard.read(): unavailable or denied';
          const clipboard = navigator.clipboard;
          const policy = document.featurePolicy;
          // Only call the native API if the policy recognizes and denies clipboard-read.
          if (typeof clipboard?.read !== 'function' ||
              !policy?.features?.().includes('clipboard-read') ||
              policy.allowsFeature?.('clipboard-read') !== false) {
            parent.postMessage(blocked, '*');
            return;
          }
          try {
            await clipboard.read();
            parent.postMessage('Clipboard.read(): unexpectedly allowed', '*');
          } catch (error) {
            parent.postMessage(error.name === 'NotAllowedError' ? blocked :
              'Clipboard.read(): ' + error.name, '*');
          }
        })();
      <\/script></body></html>`;
    window.addEventListener('message', (event) => {
      if (event.source === frame.contentWindow) {
        document.querySelector('#result').textContent = event.data;
      }
    });
    document.body.append(frame);
  </script>
</body>
</html>

Result

Clipboard.read(): unavailable or denied

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources