Skip to content

Instantly share code, notes, and snippets.

@neopunisher
Last active August 29, 2018 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neopunisher/a6b2a19c19eaf1e1cae74d08b844ed73 to your computer and use it in GitHub Desktop.
Save neopunisher/a6b2a19c19eaf1e1cae74d08b844ed73 to your computer and use it in GitHub Desktop.
Inject default webtorrent
(function(url, inj) { // https://webtorrent.io/docs
inj(url).then(function() {
var client = new WebTorrent()
client.on('torrent', function (torrent) {
console.log('clientorrent:', torrent)
})
client.on('error', function (err) {
console.log('clienterr:', err)
})
var torrentId = 'magnet:?xt=urn:btih:08ada5a7a6183aae1e09d831df6748d566095a10&dn=Sintel&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fsintel.torrent'
client.add(torrentId, function (torrent) {
torrent.on('infoHash', function (ihash) {
console.log('torrentinfohash:', ihash)
})
torrent.on('metadata', function (metadata) {
console.log('torrentmetadata:', metadata)
})
torrent.on('ready', function (rdy) {
console.log('torrentready:', rdy)
})
torrent.on('warning', function (err) {
console.log('torrentwarn:', err)
})
torrent.on('error', function (err) {
console.log('torrenterr:', err)
})
torrent.on('done', function(fin){
console.log('torrent finished downloading', fin)
})
torrent.on('download', function (bytes) {
console.log('just downloaded: ' + bytes)
console.log('total downloaded: ' + torrent.downloaded)
console.log('download speed: ' + torrent.downloadSpeed)
console.log('progress: ' + torrent.progress)
})
torrent.on('upload', function (bytes) {
console.log('torrentuploaded:', bytes)
})
torrent.on('wire', function (wire, addr) {
console.log('connected to peer with address ' + addr, wire)
})
torrent.on('noPeers', function (announceType) {
console.log('torrentnopeers:', announceType)
})
// Torrents can contain many files. Let's use the .mp4 file
var file = torrent.files.find(function (file) {
return file.name.endsWith('.mp4')
})
// Display the file by adding it to the DOM. Supports video, audio, image, etc. files
file.appendTo('body')
console.log(torrent)
})
})
})('https://cdn.jsdelivr.net/webtorrent/latest/webtorrent.min.js', (src, head = true) => new Promise(function(c, d) {
var a = document.createElement("script");
a.src = src;
a.type = "text/javascript";
a.onload = c;
a.onerror = d;
document[head ? 'head' : 'body'].appendChild(a)
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment