Skip to content

fix: copy buttons silently fail on insecure origins (clipboard fallback)#24

Open
Mubashirrrr wants to merge 1 commit into
revfactory:mainfrom
Mubashirrrr:fix/clipboard-insecure-context-fallback
Open

fix: copy buttons silently fail on insecure origins (clipboard fallback)#24
Mubashirrrr wants to merge 1 commit into
revfactory:mainfrom
Mubashirrrr:fix/clipboard-insecure-context-fallback

Conversation

@Mubashirrrr
Copy link
Copy Markdown

What

The "copy" buttons on the landing page (copyPrompt / copyCode) silently do nothing when index.html is opened in an insecure context — most commonly via file:// (e.g. cloning the repo and opening the page locally).

Why

navigator.clipboard.writeText(text).then(() => { ... });

navigator.clipboard is only defined in secure contexts (https: or localhost). Over file:// (or plain http://) it's undefined, so navigator.clipboard.writeText(...) throws TypeError synchronously: the click handler aborts, no text is copied, and the "Copied!" feedback never shows. There's also no .catch, so the rejection path is unhandled.

This works on the production GitHub Pages site (https), but breaks for anyone previewing the page locally.

Fix

Route both handlers through a copyToClipboard() helper that uses the async Clipboard API in secure contexts and falls back to a hidden-<textarea> + document.execCommand('copy') otherwise.

Verification

Static page (no test harness). node --check on the page's script passes; the only remaining navigator.clipboard.writeText is inside the helper, guarded by navigator.clipboard && window.isSecureContext.

🤖 Generated with Claude Code

…lback)

copyPrompt() and copyCode() call navigator.clipboard.writeText() directly. In an
insecure context — most commonly the page opened via file:// — navigator.clipboard
is undefined, so the call throws synchronously: the copy button does nothing and
shows no feedback (and there was no .catch on the returned promise either).

Route both through a copyToClipboard() helper that uses the async Clipboard API
in secure contexts and falls back to a hidden-textarea execCommand('copy')
otherwise.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.

1 participant