Webtor / index.html
caustino's picture
Update index.html
2bfd644 verified
Raw History Blame Contribute Delete
5.7 kB
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>