git ssb

1+

Daan Patchwork / patchwork



Tree: 4087ed91f7e663172d051c9afe142fa7b7669f1e

Files: 4087ed91f7e663172d051c9afe142fa7b7669f1e / lib / catch-links.js

1015 bytesRaw
1
2module.exports = function (root, cb) {
3 root.addEventListener('click', (ev) => {
4 if (ev.altKey || ev.ctrlKey || ev.metaKey || ev.shiftKey || ev.defaultPrevented) {
5 return true
6 }
7
8 let anchor = null
9 for (let n = ev.target; n.parentNode; n = n.parentNode) {
10 if (n.nodeName === 'A') {
11 anchor = n
12 break
13 }
14 }
15 if (!anchor) return true
16
17 let href = anchor.getAttribute('href')
18
19 if (href) {
20 if (href.startsWith('#')) {
21 try {
22 href = decodeURIComponent(href)
23 } catch (e) {
24 // Safely ignore error because href isn't URI-encoded.
25 }
26 }
27 let isUrl
28 let url
29
30 try {
31 url = new URL(href)
32 isUrl = true
33 } catch (e) {
34 isUrl = false
35 }
36
37 if (isUrl && (url.host || url.protocol === 'magnet:')) {
38 cb(href, true)
39 } else if (href !== '#') {
40 cb(href, false, anchor.anchor)
41 }
42 }
43
44 ev.preventDefault()
45 ev.stopPropagation()
46 })
47}
48

Built with git-ssb-web