-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
83 lines (75 loc) · 2.64 KB
/
Copy pathscript.js
File metadata and controls
83 lines (75 loc) · 2.64 KB
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
function startDownload() {
const url = document.getElementById('urlInput').value.trim();
const format = document.getElementById('formatSelect').value;
const quality = document.getElementById('qualitySelect').value;
const status = document.getElementById('status');
const progressBar = document.getElementById('progressBar');
const downloadBtn = document.querySelector('button');
if (!url) {
status.textContent = "الرجاء إدخال رابط يوتيوب";
status.style.color = "#ff4b4b";
return;
}
progressBar.style.width = "0%";
status.textContent = "جاري التحضير للتحميل...";
status.style.color = "#fff";
downloadBtn.disabled = true;
downloadBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> جاري التحميل...';
fetch('/download', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ url, format, quality })
})
.then(response => {
if (!response.ok) {
return response.json().then(err => {
throw new Error(err.error || "فشل التحميل");
});
}
return response.blob();
})
.then(blob => {
const downloadUrl = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = downloadUrl;
a.download = `streamsnap_${format === 'mp3' ? 'audio' : 'video'}.${format}`;
document.body.appendChild(a);
a.click();
a.remove();
progressBar.style.width = "100%";
status.textContent = "تم التحميل بنجاح!";
status.style.color = "#4CAF50";
setTimeout(() => {
window.URL.revokeObjectURL(downloadUrl);
downloadBtn.disabled = false;
downloadBtn.innerHTML = '<i class="fas fa-download"></i> تحميل';
}, 100);
})
.catch(error => {
console.error('Download error:', error);
progressBar.style.width = "0%";
status.textContent = "خطأ: " + error.message;
status.style.color = "#ff4b4b";
downloadBtn.disabled = false;
downloadBtn.innerHTML = '<i class="fas fa-download"></i> تحميل';
});
}
function extractVideoID(url) {
const regExp = /^.*(youtu.be\/|v=|\/embed\/|\/shorts\/)([^#\&?]*).*/;
const match = url.match(regExp);
return match && match[2].length === 11 ? match[2] : null;
}
function updateThumbnail() {
const url = document.getElementById('urlInput').value.trim();
const videoId = extractVideoID(url);
const thumbnail = document.getElementById('videoThumbnail');
if (videoId) {
thumbnail.src = `https://img.youtube.com/vi/${videoId}/hqdefault.jpg`;
thumbnail.style.display = 'block';
} else {
thumbnail.src = '';
thumbnail.style.display = 'none';
}
}