git ssb

2+

mixmix / ticktack



Tree: e4aaa311b7efe7bf2540be819c77348f258eb18d

Files: e4aaa311b7efe7bf2540be819c77348f258eb18d / app / async / catch-link-click.js

1230 bytesRaw
1const nest = require('depnest')
2const Url = require('url')
3const { isMsg } = require('ssb-ref')
4
5exports.gives = nest('app.async.catchLinkClick')
6
7exports.needs = nest({
8 'sbot.async.get': 'first'
9})
10
11exports.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
51
52

Built with git-ssb-web