Skip to content

Commit c0c40f6

Browse files
committed
Add feature detection & check for errors
1 parent f067c15 commit c0c40f6

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

python_docs_theme/static/copybutton.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,23 @@ const loadCopyButton = () => {
3333
const makeOnButtonClick = () => {
3434
let timeout = null
3535
// define the behavior of the button when it's clicked
36-
return event => {
36+
return async event => {
37+
// check if the clipboard is available
38+
if (!navigator.clipboard || !navigator.clipboard.writeText) {
39+
return;
40+
}
41+
3742
clearTimeout(timeout)
3843
const buttonEl = event.currentTarget
3944
const codeEl = buttonEl.nextElementSibling
40-
navigator.clipboard.writeText(getCopyableText(codeEl))
45+
46+
try {
47+
await navigator.clipboard.writeText(getCopyableText(codeEl))
48+
} catch (e) {
49+
console.error(e.message)
50+
return
51+
}
52+
4153
buttonEl.innerText = _("Copied!")
4254
timeout = setTimeout(() => {
4355
buttonEl.innerText = _("Copy")

0 commit comments

Comments
 (0)