-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.js
51 lines (46 loc) · 1.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const ZComponent = require('zcomponent')
const WebTorrent = require('webtorrent')
const magnetURL = require('magnet-uri')
const client = new WebTorrent()
const getTorrent = (magnet, fn) => {
let decoded = magnetURL(magnet)
for (let torrent of client.torrents) {
if (torrent.infoHash === decoded.infoHash) {
// TODO: make sure all trackers and web seeds are added
return fn(torrent)
}
}
client.add(magnet, fn)
}
class WebTorrentElement extends ZComponent {
set file (file) {
this._file = file
}
set src (magnet) {
getTorrent(magnet, torrent => {
torrent.files.forEach(file => {
if (this._file) {
if (file.name === this._file || file.path === this._file) {
file.appendTo(this)
}
} else {
file.appendTo(this)
}
})
})
}
get shadow () {
return `
<style>
:host {
padding: 0 0 0 0;
margin: 0 0 0 0;
}
</style>
<slot>
</slot>
`
}
}
module.exports = WebTorrentElement
window.customElements.define('web-torrent', WebTorrentElement)