Skip to content

Commit 737f380

Browse files
committed
access navigator from window; move call writeText to a helper function and trap exception
1 parent a80c945 commit 737f380

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/js/07-copy-page-spec.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
;(function () {
22
'use strict'
33

4+
if (!window.navigator.clipboard) return
5+
6+
var HEADING_RX = /H[2-6]/
7+
48
var pageSpec = (document.querySelector('head meta[name=page-spec]') || {}).content
59
var editPageLink = document.querySelector('.toolbar .edit-this-page a')
610
if (!(pageSpec && editPageLink)) return
711
if (editPageLink) editPageLink.addEventListener('click', onEditPageLinkClick)
812
;[].slice.call(document.querySelectorAll('.doc a.anchor')).forEach(function (anchor) {
9-
if (/H[2-6]/.test(anchor.parentNode.tagName)) anchor.addEventListener('click', onSectionAnchorClick.bind(anchor))
13+
if (HEADING_RX.test(anchor.parentNode.tagName)) anchor.addEventListener('click', onSectionAnchorClick.bind(anchor))
1014
})
1115

1216
function onEditPageLinkClick (e) {
13-
if (e.altKey) navigator.clipboard.writeText(pageSpec)
17+
if (e.altKey) writeToClipboard(pageSpec)
1418
}
1519

1620
function onSectionAnchorClick (e) {
1721
if (e.altKey) {
1822
e.preventDefault()
19-
navigator.clipboard.writeText(pageSpec + decodeFragment(this.hash))
23+
writeToClipboard(pageSpec + decodeFragment(this.hash))
2024
}
2125
}
2226

2327
function decodeFragment (hash) {
2428
return ~hash.indexOf('%') ? decodeURIComponent(hash) : hash
2529
}
30+
31+
function writeToClipboard (text) {
32+
window.navigator.clipboard.writeText(text).then(function () {}, function () {})
33+
}
2634
})()

0 commit comments

Comments
 (0)