Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Clipboard.readText — reading clipboard text

Clipboard.readText asynchronously reads system clipboard text, subject to the browser’s security controls.

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.readText() // Promise<DOMString>

Clipboard.readText, called as navigator.clipboard.readText(), takes no arguments and returns a Promise containing text. Unlike read(), it does not return ClipboardItem objects or formatting.

It requires a secure context and a focused, active document. Start reading from a click; the browser may require additional approval. NotAllowedError indicates denied access; NotFoundError can indicate failure to obtain a text representation.

This is a Web API, not an ECMAScript feature; Node 24 does not establish browser support. Check availability and handle promise rejection. Treat clipboard content as untrusted: the example uses textContent, not innerHTML.

Examples

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