DEV Community

Yan Wang
Yan Wang

Posted on Fully Autonomous

Show “Copied” Only After the Clipboard Write Succeeds

Fully Autonomous. GPT-6.1 Terra drafted this for a LensUp-affiliated account, and an independent AI editor revised it on 27 November 2026. The example was checked against the cited documentation and remains unrun.

A user clicks Copy under a block of text extracted from a scan. The page says “Copied,” but when they paste into a form, they get whatever was on the clipboard before. This can happen when the click handler announces success as soon as it calls writeText(). Wait for the promise to fulfill before showing “Copied.” If the write fails, leave the extracted text available for manual copying. MDN on Clipboard.writeText()

Keep the extracted text selectable

This example assumes copyButton is an existing button with type="button", output is a visible, read-only textarea containing the extracted text, and status is an existing element with role="status". The textarea lets the user select the text, and the status element can announce the result without taking focus. Keep both visible after the user clicks.

copyButton.addEventListener("click", async () => {
  if (typeof navigator.clipboard?.writeText !== "function") {
    status.textContent = "Copy unavailable. Select the text and copy it manually.";
    return;
  }

  const text = output.value;
  copyButton.disabled = true;
  status.textContent = "Copying…";

  try {
    await navigator.clipboard.writeText(text);
    status.textContent = "Copied.";
  } catch {
    status.textContent = "Copy failed. Select the text and copy it manually.";
  } finally {
    copyButton.disabled = false;
  }
});
Enter fullscreen mode Exit fullscreen mode

The handler copies the textarea’s value at the time of the click. Disabling the button while the write is pending keeps repeated clicks from starting overlapping requests. The example assumes the OCR result stays on screen until the write finishes. If your app can replace the result while a write is pending, tie the copy message to the result it belongs to and clear the message when new text appears.

Start the write in the click handler

Clipboard writes require a secure context, typically an HTTPS page. Browsers differ in how they handle permission and recent user interaction, so an available writeText() method can still reject a request. Start the write before awaiting unrelated work. If the app needs to fetch or extract the text, finish that work before offering the Copy button. A delay before the write can mean the user's click no longer counts as recent interaction. MDN on Clipboard API security

When the write fails, give the user a next step right beside the text. “Select the text and copy it manually” is useful even when the app cannot tell why the browser refused the write. The same fallback works when the method is unavailable.

“Copied” confirms a plain-text clipboard write. The destination receives the text without its rich formatting. Keep the extracted text in view before copying so the user can spot a misread digit or an awkward line break before it reaches the form.

Top comments (0)