Skip to content

fix(webview): increase focus retry count for copyImage - #328569

Open
RITHIKKUMARAN wants to merge 1 commit into
microsoft:mainfrom
RITHIKKUMARAN:fix/webview-clipboard
Open

fix(webview): increase focus retry count for copyImage#328569
RITHIKKUMARAN wants to merge 1 commit into
microsoft:mainfrom
RITHIKKUMARAN:fix/webview-clipboard

Conversation

@RITHIKKUMARAN

Copy link
Copy Markdown

Fixes #199981

Description

This PR addresses the long-standing issue where users are unable to copy images to their system clipboard via the right-click context menu inside webviews (such as Markdown Previews and Notebook outputs).

Root Cause:
When the VS Code custom context menu is invoked via a right-click, the webview iframe loses document focus. Because navigator.clipboard.write() strictly requires an active, focused document, it fails with a NotAllowedError if invoked while the context menu still holds focus. The existing code attempted to wait for focus to return, but the retry limit was set to only 5 retries at very short intervals (100-250ms total), which is frequently insufficient for the UI to return focus to the webview.

Proposed Changes:

  • Increased the retries parameter in webviewPreloads.ts (Notebooks) from 5 to 50.
  • Increased the retries parameter in preview-src/index.ts (Markdown) from 5 to 50.

This gives the webview up to 1 - 2.5 seconds to regain focus before executing the clipboard write operation, completely mitigating the timing race condition without introducing any blocking delays.

How to test

  1. Open a Markdown file with an embedded image, or a Notebook with an image output.
  2. Right-click the image to open the context menu.
  3. Select "Copy Image".
  4. Paste the image into an external program (like Paint or a browser).
  5. The image should successfully paste instead of failing silently.

Copilot AI review requested due to automatic review settings August 1, 2026 16:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@RITHIKKUMARAN

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@RITHIKKUMARAN
RITHIKKUMARAN force-pushed the fix/webview-clipboard branch from afc7d83 to 47a4a9d Compare August 2, 2026 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to Copy from Webview View

2 participants