Skip to content

Improve copy page functionality with fallback and DOM content#24

Merged
chinmaymk merged 1 commit intomainfrom
claude/fix-docs-copy-button-yjLBx
Mar 11, 2026
Merged

Improve copy page functionality with fallback and DOM content#24
chinmaymk merged 1 commit intomainfrom
claude/fix-docs-copy-button-yjLBx

Conversation

@chinmaymk
Copy link
Copy Markdown
Owner

Summary

Enhanced the copy page button to be more robust by adding a fallback clipboard mechanism and implementing a graceful degradation strategy when fetching remote content fails.

Key Changes

  • Added fallback clipboard method: Implemented fallbackCopy() function using the legacy document.execCommand('copy') API for browsers that don't support the modern Clipboard API
  • Extracted clipboard logic: Created writeToClipboard() function that attempts to use the modern Clipboard API with automatic fallback to the legacy method
  • Improved error handling: Enhanced copyPage() to gracefully handle fetch failures by falling back to copying the page's DOM content (.vp-doc element text) or the current URL
  • Better content fallback chain: Implemented a three-tier fallback strategy:
    1. Try to fetch and copy the raw markdown content from GitHub
    2. If fetch fails, copy the rendered page content from the DOM
    3. If no content is available, copy the current page URL

Implementation Details

  • The fallback copy method creates a temporary textarea element, selects its content, executes the copy command, and cleans up after itself
  • Error handling is now more granular with specific checks for fetch success (res.ok) before processing the response
  • The DOM fallback extracts text content from the .vp-doc element and trims whitespace for cleaner copying

https://claude.ai/code/session_01K6L2NgGxXGWePYRUSi8qW2

Two bugs: (1) fetch response status wasn't checked, so a 404 from
GitHub raw would silently copy error HTML instead of markdown content,
and (2) navigator.clipboard.writeText can fail in non-secure contexts
with no fallback. Now checks res.ok, falls back to DOM text content
when fetch fails, and uses textarea+execCommand as a clipboard fallback.

https://claude.ai/code/session_01K6L2NgGxXGWePYRUSi8qW2
@chinmaymk chinmaymk merged commit 63d6684 into main Mar 11, 2026
1 check 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