Skip to content

Fix navigator.clipboard.writeText in @electron/remote MenuItem handlers#2754

Merged
bengotow merged 3 commits into
masterfrom
claude/awesome-ritchie-5wldca
Jun 19, 2026
Merged

Fix navigator.clipboard.writeText in @electron/remote MenuItem handlers#2754
bengotow merged 3 commits into
masterfrom
claude/awesome-ritchie-5wldca

Conversation

@bengotow

@bengotow bengotow commented Jun 18, 2026

Copy link
Copy Markdown
Collaborator

Sentry Issue

MAILSPRING-CLIENT-2R — 16 users affected, 30 events, ongoing since May 2026

Root Cause

When a user right-clicks on a contact name/email in the message header, a context menu appears with a "Copy email address" item. Clicking it called navigator.clipboard.writeText(contact.email), which reported Error: Unknown error to Sentry.

The context menu's click handler is invoked via @electron/remote IPC — the main process calls back into the renderer via IPC after the menu item is selected. At that point:

  1. The rendering window no longer has focus (it lost focus when the OS-native context menu appeared)
  2. navigator.clipboard.writeText() is a Web Clipboard API that requires both a transient user gesture and an active, focused browsing context
  3. When called through an IPC callback, neither condition is guaranteed, causing the browser to reject the write with "Unknown error"

Fix

Replace navigator.clipboard.writeText() with @electron/remote's clipboard.writeText(), which routes through Electron's main-process clipboard API and works regardless of window focus. As a bonus, the call is now synchronous so there's no unhandled promise rejection.

This pattern was present in five files — all fixed in this PR:

  • app/internal_packages/message-list/lib/message-participants.tsx — Copy contact email (original Sentry report)
  • app/internal_packages/main-calendar/lib/core/event-attendees-input.tsx — Copy event attendee email
  • app/src/components/participants-text-field.tsx — Copy participant email
  • app/src/components/evented-iframe.tsx — Copy Email Address / Copy Link Address / Copy selected text
  • app/internal_packages/thread-list/lib/thread-list-context-menu.ts — Copy mailbox permalink

🤖 Generated with Claude Code

https://claude.ai/code/session_01GqXVeX65b7daqB5rU4jTuJ

…-2R)

navigator.clipboard.writeText() requires the window to be focused and
a user gesture in the current browsing context. When the menu item's
click handler is invoked via @electron/remote IPC, the browsing context
has changed and the Web Clipboard API throws 'Unknown error'.

Use @electron/remote's clipboard module instead, which routes through
Electron's main-process clipboard API and works regardless of window
focus or browsing context.

Fixes MAILSPRING-CLIENT-2R

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GqXVeX65b7daqB5rU4jTuJ
@indent-staging

indent-staging Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor
PR Summary

Fixes Sentry issue MAILSPRING-CLIENT-2R where @electron/remote MenuItem click handlers calling navigator.clipboard.writeText failed with "Unknown error" because the Web Clipboard API requires window focus + user gesture in the current browsing context, which is lost when the click is dispatched through @electron/remote IPC. The fix routes clipboard writes through @electron/remote's clipboard module instead (Electron's main-process clipboard, synchronous, no focus/gesture preconditions). Other navigator.clipboard.writeText call sites that are driven by renderer-side DOM/button clicks or keymap commands are intentionally left alone since they aren't affected.

  • app/internal_packages/message-list/lib/message-participants.tsx: destructure clipboard from @electron/remote and call clipboard.writeText(contact.email) in the participant context menu.
  • app/internal_packages/main-calendar/lib/core/event-attendees-input.tsx: same migration for the "Copy " attendee menu item.
  • app/src/components/participants-text-field.tsx: same migration for the "Copy " participant menu item.
  • app/src/components/evented-iframe.tsx: same migration for the three iframe menu items (Copy Email Address, Copy Link Address, Copy text).
  • app/internal_packages/thread-list/lib/thread-list-context-menu.ts: same migration for the "Copy mailbox permalink" thread context menu item.
  • app/internal_packages/message-list/lib/message-controls.tsx: same migration for the "Copy Debug Info to Clipboard" action in the message actions menu.

Issues

All clear! No issues remaining. 🎉

2 issues already resolved
  • message-controls.tsx has the same @electron/remote MenuItem → navigator.clipboard.writeText pattern but was not migrated, so "Copy Debug Info to Clipboard" in the message actions menu will still hit MAILSPRING-CLIENT-2R. _onShowActionsMenu wires { click: this._onCopyToClipboard } (lines 117-133), and _onCopyToClipboard calls navigator.clipboard.writeText(data) (line 183) — swap to the clipboard from @electron/remote like the other files in this commit. (fixed by commit 3a101d3)
  • Same navigator.clipboard.writeText pattern inside @electron/remote MenuItem click handlers still exists in event-attendees-input.tsx, participants-text-field.tsx, evented-iframe.tsx (Copy Email/Link Address), and thread-list/lib/thread-list-context-menu.ts — they are likely susceptible to the same MAILSPRING-CLIENT-2R failure mode and could be migrated to clipboard.writeText in a follow-up. (fixed by commit b9d5085)

CI Checks

All CI checks passed on 3a101d3.

Custom Rules 3 rules evaluated, 3 passed, 0 failed

Passing This is a longer title to see what happens when they are too long to fit
Passing B
Passing Ben Rule

View all rules

The same focus/browsing-context bug fixed in MAILSPRING-CLIENT-2R existed
in four other files that call navigator.clipboard.writeText() from
@electron/remote MenuItem click callbacks. Use @electron/remote's
clipboard module instead, which is synchronous and works regardless of
window focus.

Affected files:
- event-attendees-input.tsx (Copy participant email)
- participants-text-field.tsx (Copy participant email)
- evented-iframe.tsx (Copy Email Address / Copy Link Address / Copy text)
- thread-list-context-menu.ts (Copy mailbox permalink)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GqXVeX65b7daqB5rU4jTuJ
@bengotow bengotow changed the title Fix clipboard write error in contact context menu (Sentry MAILSPRING-CLIENT-2R) Fix navigator.clipboard.writeText in @electron/remote MenuItem handlers Jun 18, 2026
Same @electron/remote IPC focus/gesture issue fixed in the rest of the PR.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GqXVeX65b7daqB5rU4jTuJ
@bengotow bengotow merged commit bbc6adb into master Jun 19, 2026
2 checks passed
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.

2 participants