File size: 5,700 Bytes
2bfd644
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
html_content = """<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>WebTorrent Client</title>
    <script src='https://cdn.jsdelivr.net/npm/webtorrent@latest/webtorrent.min.js'></script>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f4f7f6;
            margin: 0;
            padding: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
            max-width: 600px;
            width: 100%;
        }
        h1 {
            color: #2c3e50;
            text-align: center;
            margin-top: 0;
        }
        .drop-zone {
            border: 2px dashed #3498db;
            padding: 30px;
            text-align: center;
            border-radius: 5px;
            cursor: pointer;
            background-color: #ecf0f1;
            transition: background 0.3s;
            margin-bottom: 20px;
        }
        .drop-zone:hover {
            background-color: #e2e7e9;
        }
        #status {
            font-weight: bold;
            margin-bottom: 10px;
            color: #7f8c8d;
        }
        .progress-bar {
            width: 100%;
            background-color: #e0e0e0;
            border-radius: 25px;
            overflow: hidden;
            margin-bottom: 20px;
            display: none;
        }
        .progress-fill {
            height: 20px;
            width: 0%;
            background-color: #2ecc71;
            transition: width 0.1s;
        }
        #file-list {
            list-style-type: none;
            padding: 0;
        }
        #file-list li {
            padding: 10px;
            background-color: #f9f9f9;
            border: 1px solid #eee;
            margin-bottom: 5px;
            border-radius: 3px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .download-btn {
            background-color: #3498db;
            color: white;
            border: none;
            padding: 5px 10px;
            border-radius: 3px;
            cursor: pointer;
            text-decoration: none;
        }
    </style>
</head>
<body>
    <div class='container'>
        <h1>HTML5 Torrent Downloader</h1>
        <div class='drop-zone' id='drop-zone'>
            Drop your <b>.torrent</b> file here, or click to upload.
            <input type='file' id='file-input' style='display: none;'>
        </div>
        <div id='status'>Waiting for torrent file...</div>
        <div class='progress-bar' id='progress-bar'>
            <div class='progress-fill' id='progress-fill'></div>
        </div>
        <ul id='file-list'></ul>
    </div>

    <script>
        const client = new WebTorrent();
        const dropZone = document.getElementById('drop-zone');
        const fileInput = document.getElementById('file-input');
        const statusText = document.getElementById('status');
        const progressBar = document.getElementById('progress-bar');
        const progressFill = document.getElementById('progress-fill');
        const fileList = document.getElementById('file-list');

        dropZone.addEventListener('click', () => fileInput.click());
        
        fileInput.addEventListener('change', (e) => {
            if (e.target.files.length) handleTorrent(e.target.files[0]);
        });

        dropZone.addEventListener('dragover', (e) => {
            e.preventDefault();
            dropZone.style.backgroundColor = '#d5dbdb';
        });

        dropZone.addEventListener('dragleave', () => {
            dropZone.style.backgroundColor = '#ecf0f1';
        });

        dropZone.addEventListener('drop', (e) => {
            e.preventDefault();
            dropZone.style.backgroundColor = '#ecf0f1';
            if (e.dataTransfer.files.length) handleTorrent(e.dataTransfer.files[0]);
        });

        function handleTorrent(file) {
            statusText.innerText = 'Starting download...';
            progressBar.style.display = 'block';
            
            client.add(file, (torrent) => {
                statusText.innerText = `Downloading: ${torrent.name}`;
                
                torrent.files.forEach((file) => {
                    const li = document.createElement('li');
                    li.innerHTML = `<span>${file.name}</span>`;
                    fileList.appendChild(li);

                    file.getBlobURL((err, url) => {
                        if (err) return;
                        const a = document.createElement('a');
                        a.href = url;
                        a.download = file.name;
                        a.innerText = 'Save to Device';
                        a.className = 'download-btn';
                        li.appendChild(a);
                    });
                });

                torrent.on('download', (bytes) => {
                    const progress = (torrent.progress * 100).toFixed(1);
                    progressFill.style.width = progress + '%';
                    statusText.innerText = `Progress: ${progress}% | Speed: ${(torrent.downloadSpeed / 1024 / 1024).toFixed(2)} MB/s`;
                });

                torrent.on('done', () => {
                    statusText.innerText = 'Download complete! You can now save individual files below.';
                });
            });
        }
    </script>
</body>
</html>