r3hab commited on
Commit
e76a197
·
verified ·
1 Parent(s): f5ba7f3

Create 2.html

Browse files
Files changed (1) hide show
  1. 2.html +162 -0
2.html ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <title>WebTorrent video player</title>
6
+ <style>
7
+ #output video {
8
+ width: 100%;
9
+ }
10
+ #progressBar {
11
+ height: 5px;
12
+ width: 0%;
13
+ background-color: #35b44f;
14
+ transition: width .4s ease-in-out;
15
+ }
16
+ body.is-seed .show-seed {
17
+ display: inline;
18
+ }
19
+ body.is-seed .show-leech {
20
+ display: none;
21
+ }
22
+ .show-seed {
23
+ display: none;
24
+ }
25
+ #status code {
26
+ font-size: 90%;
27
+ font-weight: 700;
28
+ margin-left: 3px;
29
+ margin-right: 3px;
30
+ border-bottom: 1px dashed rgba(255,255,255,0.3);
31
+ }
32
+
33
+ .is-seed {
34
+ background-color: #154820;
35
+ transition: .5s .5s background-color ease-in-out;
36
+ }
37
+ body {
38
+ background-color: #2a3749;
39
+ margin: 0;
40
+ height: 100%;
41
+ }
42
+ #status {
43
+ color: #fff;
44
+ font-size: 17px;
45
+ padding: 5px;
46
+ }
47
+ a:link, a:visited {
48
+ color: #30a247;
49
+ text-decoration: none;
50
+ }
51
+ </style>
52
+ </head>
53
+ <body>
54
+ <div>
55
+ <div id="progressBar"></div>
56
+ <video id="output" controls></video>
57
+ </div>
58
+ <!-- Statistics -->
59
+ <div id="status">
60
+ <div>
61
+ <span class="show-leech">Downloading </span>
62
+ <span class="show-seed">Seeding </span>
63
+ <code>
64
+ <a id="torrentLink" href="https://webtorrent.io/torrents/sintel.torrent">sintel.torrent</a>
65
+ </code>
66
+ <span class="show-leech"> from </span>
67
+ <span class="show-seed"> to </span>
68
+ <code id="numPeers">0 peers</code>.
69
+ </div>
70
+ <div>
71
+ <code id="downloaded"></code>
72
+ of <code id="total"></code>
73
+ — <span id="remaining"></span><br/>
74
+ &#x2198;<code id="downloadSpeed">0 b/s</code>
75
+ / &#x2197;<code id="uploadSpeed">0 b/s</code>
76
+ </div>
77
+ </div>
78
+
79
+ <!-- Moment.js for time formatting -->
80
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
81
+
82
+ <script type="module">
83
+ // Updated WebTorrent import using proper ESM syntax
84
+ import WebTorrent from 'https://cdn.skypack.dev/webtorrent'
85
+
86
+ const torrentId = 'https://webtorrent.io/torrents/sintel.torrent'
87
+ const client = new WebTorrent()
88
+
89
+ // HTML elements
90
+ const $body = document.body
91
+ const $progressBar = document.querySelector('#progressBar')
92
+ const $numPeers = document.querySelector('#numPeers')
93
+ const $downloaded = document.querySelector('#downloaded')
94
+ const $total = document.querySelector('#total')
95
+ const $remaining = document.querySelector('#remaining')
96
+ const $uploadSpeed = document.querySelector('#uploadSpeed')
97
+ const $downloadSpeed = document.querySelector('#downloadSpeed')
98
+
99
+ // Service Worker registration
100
+ navigator.serviceWorker.register('https://cdn.jsdelivr.net/npm/webtorrent@latest/sw.min.js', { scope: './' })
101
+ .then(reg => {
102
+ const worker = reg.active || reg.waiting || reg.installing
103
+ function checkState(worker) {
104
+ if (worker.state === 'activated') {
105
+ client.createServer({ controller: reg })
106
+ download()
107
+ }
108
+ }
109
+ if (!checkState(worker)) {
110
+ worker.addEventListener('statechange', ({ target }) => checkState(target))
111
+ }
112
+ })
113
+
114
+ function download() {
115
+ client.add(torrentId, torrent => {
116
+ const file = torrent.files.find(file => file.name.endsWith('.mp4'))
117
+ file.streamTo(document.querySelector('#output'))
118
+
119
+ torrent.on('done', onDone)
120
+ setInterval(onProgress, 500)
121
+ onProgress()
122
+
123
+ function onProgress() {
124
+ $numPeers.innerHTML = torrent.numPeers + (torrent.numPeers === 1 ? ' peer' : ' peers')
125
+ const percent = Math.round(torrent.progress * 100 * 100) / 100
126
+ $progressBar.style.width = percent + '%'
127
+ $downloaded.innerHTML = prettyBytes(torrent.downloaded)
128
+ $total.innerHTML = prettyBytes(torrent.length)
129
+
130
+ let remaining
131
+ if (torrent.done) {
132
+ remaining = 'Done.'
133
+ } else {
134
+ remaining = moment.duration(torrent.timeRemaining / 1000, 'seconds').humanize()
135
+ remaining = remaining[0].toUpperCase() + remaining.substring(1) + ' remaining.'
136
+ }
137
+ $remaining.innerHTML = remaining
138
+
139
+ $downloadSpeed.innerHTML = prettyBytes(torrent.downloadSpeed) + '/s'
140
+ $uploadSpeed.innerHTML = prettyBytes(torrent.uploadSpeed) + '/s'
141
+ }
142
+
143
+ function onDone() {
144
+ $body.className += ' is-seed'
145
+ onProgress()
146
+ }
147
+ })
148
+ }
149
+
150
+ function prettyBytes(num) {
151
+ const units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
152
+ const neg = num < 0
153
+ if (neg) num = -num
154
+ if (num < 1) return (neg ? '-' : '') + num + ' B'
155
+ const exponent = Math.min(Math.floor(Math.log(num) / Math.log(1000)), units.length - 1)
156
+ const unit = units[exponent]
157
+ num = Number((num / Math.pow(1000, exponent)).toFixed(2))
158
+ return (neg ? '-' : '') + num + ' ' + unit
159
+ }
160
+ </script>
161
+ </body>
162
+ </html>