fix: copy buttons silently fail on insecure origins (clipboard fallback)#24
Open
Mubashirrrr wants to merge 1 commit into
Open
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The "copy" buttons on the landing page (
copyPrompt/copyCode) silently do nothing whenindex.htmlis opened in an insecure context — most commonly viafile://(e.g. cloning the repo and opening the page locally).Why
navigator.clipboardis only defined in secure contexts (https:orlocalhost). Overfile://(or plainhttp://) it'sundefined, sonavigator.clipboard.writeText(...)throwsTypeErrorsynchronously: 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 --checkon the page's script passes; the only remainingnavigator.clipboard.writeTextis inside the helper, guarded bynavigator.clipboard && window.isSecureContext.🤖 Generated with Claude Code