Files: 66dcda756a8e52b5f22cac3256eb987e6b3226b7 / app / async / catch-link-click.js
1228 bytesRaw
1 | const nest = require('depnest') |
2 | const Url = require('url') |
3 | const { isMsg } = require('ssb-ref') |
4 | |
5 | exports.gives = nest('app.async.catchLinkClick') |
6 | |
7 | exports.needs = nest({ |
8 | 'sbot.async.get': 'first' |
9 | }) |
10 | |
11 | exports.create = function (api) { |
12 | return nest('app.async.catchLinkClick', catchLinkClick) |
13 | |
14 | function catchLinkClick (root, cb) { |
15 | root.addEventListener('click', (ev) => { |
16 | if (ev.target.tagName === 'INPUT' && ev.target.type === 'file') return |
17 | if (ev.defaultPrevented) return // TODO check this is in the right place |
18 | ev.preventDefault() |
19 | ev.stopPropagation() |
20 | |
21 | var anchor = null |
22 | for (var n = ev.target; n.parentNode; n = n.parentNode) { |
23 | if (n.nodeName === 'A') { |
24 | anchor = n |
25 | break |
26 | } |
27 | } |
28 | if (!anchor) return true |
29 | |
30 | const href = anchor.getAttribute('href') |
31 | if (!href) return |
32 | |
33 | const url = Url.parse(href) |
34 | const opts = { |
35 | isExternal: !!url.host |
36 | } |
37 | |
38 | if (isMsg(href)) { |
39 | api.sbot.async.get(href, (err, data) => { |
40 | // NOTE the catchLinkClick cb has signature (link, opts) |
41 | cb(err || {key: href, value: data}, opts) |
42 | }) |
43 | return |
44 | } |
45 | |
46 | cb(href, opts) |
47 | }) |
48 | } |
49 | } |
50 |
Built with git-ssb-web