-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Re-allow clipboard copy on non-https sites #17118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
8c0ae0c
a0b3837
5b127b1
01b36b4
e001004
ca20ae2
e36864a
fe2b719
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,36 @@ function onError(btn) { | |
btn.dataset.content = btn.dataset.original; | ||
} | ||
|
||
/** Use the document.execCommand to copy the value to clipboard */ | ||
function fallbackCopyViaSelect(elem) { | ||
elem.select(); | ||
|
||
// if unsecure (not https), there is no navigator.clipboard, but we can still use document.execCommand to copy to clipboard | ||
// it's also fine if we don't test it exists because of the try statement | ||
return document.execCommand('copy'); | ||
} | ||
/** | ||
* Fallback to use if navigator.clipboard doesn't exist. | ||
* Achieved via creating a temporary textarea element, selecting the text, and using document.execCommand. | ||
*/ | ||
function fallbackCopyToClipboard(text) { | ||
const tempTextArea = document.createElement('textarea'); | ||
tempTextArea.value = text; | ||
|
||
// avoid scrolling | ||
tempTextArea.style.top = 0; | ||
tempTextArea.style.left = 0; | ||
tempTextArea.style.position = 'fixed'; | ||
|
||
document.body.appendChild(tempTextArea); | ||
|
||
const success = fallbackCopyViaSelect(tempTextArea); | ||
|
||
document.body.removeChild(tempTextArea); | ||
|
||
return success; | ||
} | ||
|
||
export default async function initClipboard() { | ||
for (const btn of document.querySelectorAll(selector) || []) { | ||
btn.addEventListener('click', async () => { | ||
|
@@ -28,8 +58,17 @@ export default async function initClipboard() { | |
if (!text) return; | ||
|
||
try { | ||
await navigator.clipboard.writeText(text); | ||
onSuccess(btn); | ||
if (navigator.clipboard && window.isSecureContext) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't I think I recall copy works on non-secure origin There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It checks the "clipboard-write" and "clipboard-read" permissions, however, On both firefox and chrome, the
Also,
The easiest way to resolve this confusion is to move the |
||
await navigator.clipboard.writeText(text); | ||
onSuccess(btn); | ||
} else { | ||
const success = btn.dataset.clipboardTarget ? fallbackCopyViaSelect(document.querySelector(btn.dataset.clipboardTarget)) : fallbackCopyToClipboard(text); | ||
if (success) { | ||
onSuccess(btn); | ||
} else { | ||
onError(btn); | ||
} | ||
} | ||
} catch { | ||
onError(btn); | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.