Files: d56fe5e284a2a1b36f5b540a214aedd8c458bdcd / lib / catch-links.js
693 bytesRaw
1 | var Url = require('url') |
2 | |
3 | module.exports = function (root, cb) { |
4 | root.addEventListener('click', (ev) => { |
5 | if (ev.altKey || ev.ctrlKey || ev.metaKey || ev.shiftKey || ev.defaultPrevented) { |
6 | return true |
7 | } |
8 | |
9 | var anchor = null |
10 | for (var n = ev.target; n.parentNode; n = n.parentNode) { |
11 | if (n.nodeName === 'A') { |
12 | anchor = n |
13 | break |
14 | } |
15 | } |
16 | if (!anchor) return true |
17 | |
18 | var href = anchor.getAttribute('href') |
19 | |
20 | if (href) { |
21 | var url = Url.parse(href) |
22 | if (url.host) { |
23 | cb(href, true) |
24 | } else if (href !== '#') { |
25 | cb(href, false, anchor.anchor) |
26 | } |
27 | } |
28 | |
29 | ev.preventDefault() |
30 | ev.stopPropagation() |
31 | }) |
32 | } |
33 |
Built with git-ssb-web