1
+ function prettyBytes ( num ) {
2
+ const units = [ 'B' , 'kB' , 'MB' , 'GB' , 'TB' , 'PB' , 'EB' , 'ZB' , 'YB' ] ;
3
+ const neg = num < 0 ;
4
+ if ( neg ) num = - num ;
5
+ if ( num < 1 ) return ( neg ? '-' : '' ) + num + ' B' ;
6
+ const exponent = Math . min ( Math . floor ( Math . log ( num ) / Math . log ( 1000 ) ) , units . length - 1 ) ;
7
+ const unit = units [ exponent ] ;
8
+ num = Number ( ( num / Math . pow ( 1000 , exponent ) ) . toFixed ( 2 ) ) ;
9
+ return ( neg ? '-' : '' ) + num + ' ' + unit ;
10
+ }
11
+
1
12
function logError ( err ) {
2
13
console . log ( err . message ) ;
3
14
}
@@ -20,13 +31,14 @@ function createClient() {
20
31
} ) ;
21
32
client . on ( 'error' , logError ) ;
22
33
client . on ( 'torrent' , function ( torrent ) {
23
- document . getElementById ( 'note' ) . textContent = `Keep this tab open and active while transfering " ${ torrent . name } " .` ;
34
+ document . getElementById ( 'note' ) . innerHTML = `Keep this tab active while <code id="status"> ${ window . location . hash ? 'leeching' : 'seeding' } </code> <sup> ${ torrent . files . length } </sup> file(s) .` ;
24
35
} ) ;
25
36
26
37
return client ;
27
38
}
28
39
29
- function copyLink ( btn ) {
40
+ function copyLink ( event ) {
41
+ const btn = event . target ;
30
42
navigator . clipboard . writeText ( btn . previousElementSibling . value ) ;
31
43
btn . textContent = 'Copied' ;
32
44
setTimeout ( ( ) => { btn . textContent = 'Copy' ; } , 1337 ) ;
@@ -39,30 +51,30 @@ function downloadTorrent(infohash) {
39
51
40
52
function addTorrent ( torrent ) {
41
53
torrent . on ( 'error' , logError ) ;
42
- torrent . on ( 'download' , function ( bytes ) {
54
+ torrent . on ( 'download' , function ( ) {
43
55
throttle ( function ( ) {
44
56
updateSpeed ( torrent ) ;
45
57
} , 1000 ) ;
46
58
} ) ;
47
- torrent . on ( 'upload' , function ( bytes ) {
59
+ torrent . on ( 'upload' , function ( ) {
48
60
throttle ( function ( ) {
49
61
updateSpeed ( torrent ) ;
50
62
} , 1000 ) ;
51
63
} ) ;
52
64
torrent . on ( 'done' , ( ) => {
53
65
updateSpeed ( torrent ) ;
54
- document . getElementById ( 'status' ) . textContent = 'done ' ;
66
+ document . getElementById ( 'status' ) . textContent = 'seeding ' ;
55
67
} ) ;
56
68
const torrentIds = torrent . magnetURI . split ( '&' ) ;
57
69
const torId = torrentIds [ 0 ] . split ( ':' ) ;
58
70
const torHash = torId [ 3 ] ;
59
- const torrentLog = `<label for="link">Share link: </label>
60
- <input type="text" id="link" name="link" value="https://${ window . location . host } /#${ torHash } " readonly>
61
- <button class ="copy" onclick="copyLink(this) ">Copy</button>` ;
62
- log ( 'log ' , torrentLog ) ;
63
- const filesLog = `<label class="files-label"><sup> ${ torrent . files . length } </sup> file(s) <code id="status"> ${ window . location . hash ? 'downloading' : 'seeding' } </code></label>
64
- <ul class="file-list">
65
- </ul>` ;
71
+ const torrentShare = `<label for="link">Share link</label>
72
+ <input type="text" id="link" name="link" value="https://${ window . location . host } /#${ torHash } " readonly>
73
+ <button id ="copy-btn ">Copy</button>` ;
74
+ log ( 'share ' , torrentShare ) ;
75
+ const filesLog = `
76
+ <ul class="file-list">
77
+ </ul>` ;
66
78
log ( 'files' , filesLog ) ;
67
79
torrent . files . forEach ( file => {
68
80
// Stream the file in the browser
@@ -74,31 +86,32 @@ function addTorrent(torrent) {
74
86
logError ( err ) ;
75
87
return ;
76
88
}
77
- const a = document . createElement ( 'a' ) ;
78
- a . href = url ;
79
- a . textContent = file . name + ` (${ prettierBytes ( file . length ) } )` ;
80
- a . download = file . name ;
81
- const link = `<li><a href="${ url } " download="${ file . name } " onclick="this.classList.add('visited')">${ file . name } <span class="file-size">${ prettierBytes ( file . length ) } ↓</span></a></li>` ;
82
- document . getElementsByClassName ( 'file-list' ) [ 0 ] . insertAdjacentHTML ( 'beforeEnd' , link ) ;
89
+ const li = document . createElement ( 'li' ) ;
90
+ const link = document . createElement ( 'a' ) ;
91
+ link . href = url ;
92
+ link . textContent = file . name + ` (${ prettyBytes ( file . length ) } ) ↓` ;
93
+ link . download = file . name ;
94
+ li . appendChild ( link ) ;
95
+ document . querySelector ( '.file-list' ) . appendChild ( li ) ;
83
96
} ) ;
84
97
} ) ;
98
+ document . getElementById ( 'copy-btn' ) . addEventListener ( 'click' , copyLink ) ;
85
99
}
86
100
87
101
function updateSpeed ( torrent ) {
88
102
const progress = ( 100 * torrent . progress ) . toFixed ( 0 ) ;
89
103
const speed = `
90
- <div class="transfer-info">
91
- ${ window . location . hash ? `<div class="progress">
92
- <div class="progress-bar" role="progressbar" aria-label="Current download progress" style="width: ${ progress } %;" aria-valuenow="${ progress } " aria-valuemin="0" aria-valuemax="100">${ progress } %</div>
93
- </div>` : '' }
94
- <div>Peers: ${ torrent . numPeers } </div>
95
- <div>Download speed: ${ prettierBytes ( torrent . downloadSpeed ) } /s</div>
96
- <div>Upload speed: ${ prettierBytes ( torrent . uploadSpeed ) } /s</div>
97
- ${ window . location . hash ? `<div class="text-truncate">Remaining: ${ torrent . done ? 'done' : convertMS ( torrent . timeRemaining ) } </div>` : '' }
98
- </div>
99
- `
104
+ <div class="progress">
105
+ <div class="progress-bar" role="progressbar" aria-label="Current download progress" style="width: ${ progress } %;" aria-valuenow="${ progress } " aria-valuemin="0" aria-valuemax="100">${ progress } %</div>
106
+ </div>
107
+ <div>Peers: ${ torrent . numPeers } </div>
108
+ <div>Download speed: ${ prettyBytes ( torrent . downloadSpeed ) } /s</div>
109
+ <div>Upload speed: ${ prettyBytes ( torrent . uploadSpeed ) } /s</div>
110
+ ${ window . location . hash ? `<div class="text-truncate">Remaining: ${ torrent . done ? 'done' : convertMS ( torrent . timeRemaining ) } </div>` : '' }
111
+ `
100
112
const speedInfo = document . getElementById ( 'speed' ) ;
101
113
speedInfo . innerHTML = speed ;
114
+ speedInfo . hidden = false ;
102
115
}
103
116
104
117
function convertMS ( ms ) {
@@ -128,8 +141,10 @@ function convertMS(ms) {
128
141
return ret ;
129
142
}
130
143
131
- function log ( id , element ) {
132
- document . getElementById ( id ) . insertAdjacentHTML ( 'afterBegin' , element ) ;
144
+ function log ( id , item ) {
145
+ const element = document . getElementById ( id ) ;
146
+ element . insertAdjacentHTML ( 'afterBegin' , item ) ;
147
+ element . hidden = false ;
133
148
}
134
149
135
150
const announce = [
@@ -163,22 +178,17 @@ window.addEventListener('DOMContentLoaded', function() {
163
178
downloadTorrent ( hash ) ;
164
179
}
165
180
else {
166
- noteElement . textContent = 'Please select file(s) to start seeding.' ;
167
181
upElement . classList . add ( 'show' ) ;
168
182
}
169
183
}
170
184
else {
171
185
noteElement . textContent = 'Sorry, WebRTC is not supported in your browser.' ;
172
186
}
173
- noteElement . classList . add ( 'show' ) ;
174
187
175
- uploadElement ( document . getElementById ( 'upload' ) , ( err , results ) => {
176
- if ( err ) {
177
- logError ( err ) ;
178
- return ;
179
- }
180
- const files = results . map ( result => result . file ) ;
181
- if ( files . length ) {
188
+ const fileInput = document . getElementById ( 'upload' ) ;
189
+ fileInput . addEventListener ( 'change' , ( ) => {
190
+ const files = fileInput . files ;
191
+ if ( files . length ) {
182
192
upElement . remove ( ) ;
183
193
noteElement . textContent = 'File hashing in progress, please wait...' ;
184
194
const client = createClient ( ) ;
0 commit comments