-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrenderer.js
More file actions
113 lines (95 loc) · 3.33 KB
/
Copy pathrenderer.js
File metadata and controls
113 lines (95 loc) · 3.33 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
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
const { ipcRenderer } = require('electron');
const urlInput = document.getElementById('urlInput');
const browseBtn = document.getElementById('browseBtn');
const vidBtn = document.getElementById('vidBtn');
const audBtn = document.getElementById('audBtn');
const statusText = document.getElementById('statusText');
const pathDisplay = document.getElementById('pathDisplay');
const qualityModal = document.getElementById('qualityModal');
const qualityBtns = document.querySelectorAll('.quality-btn');
const cancelBtn = document.getElementById('cancelBtn');
let selectedPath = '';
let currentProgress = 0;
browseBtn.addEventListener('click', async () => {
const path = await ipcRenderer.invoke('select-folder');
if (path) {
selectedPath = path;
const display = path.length > 35 ? '...' + path.slice(-30) : path;
pathDisplay.innerText = `Save to: ${display}`;
}
});
function triggerNotification(title, body) {
new Notification(title, { body: body, icon: 'assets/icon.png' });
}
// 1. Audio Click -> Start Immediately
audBtn.addEventListener('click', () => initiateDownload(true, null));
// 2. Video Click -> Open Modal
vidBtn.addEventListener('click', () => {
if (!validateInput()) return;
qualityModal.style.display = 'flex';
});
// 3. Quality Selected -> Start Download
qualityBtns.forEach(btn => {
btn.addEventListener('click', (e) => {
const quality = e.target.getAttribute('data-q');
qualityModal.style.display = 'none';
initiateDownload(false, quality);
});
});
cancelBtn.addEventListener('click', () => {
qualityModal.style.display = 'none';
});
function validateInput() {
const url = urlInput.value.trim();
if (!url) {
statusText.innerText = "Error: Input is empty.";
shakeInput();
return false;
}
return true;
}
function initiateDownload(isAudio, quality) {
if (!validateInput()) return;
const url = urlInput.value.trim();
toggleInterface(false);
currentProgress = 0;
// Show user what is happening
if (isAudio) {
statusText.innerText = "Processing Audio...";
} else {
statusText.innerText = `Merging Video (${quality})...`;
}
statusText.style.color = "#000";
ipcRenderer.send('start-download', { url, isAudio, savePath: selectedPath, quality });
}
function toggleInterface(enabled) {
vidBtn.disabled = !enabled;
audBtn.disabled = !enabled;
urlInput.disabled = !enabled;
browseBtn.disabled = !enabled;
}
ipcRenderer.on('progress-update', (event, percent) => {
if (percent > currentProgress) {
currentProgress = percent;
statusText.innerText = `Downloading... ${percent}%`;
}
});
ipcRenderer.on('download-complete', (event, response) => {
toggleInterface(true);
if (response.status === 'success') {
statusText.innerText = "Download Complete.";
statusText.style.color = "#000";
triggerNotification("YT-Ripper", "File saved successfully.");
urlInput.value = "";
} else {
// Show specific error message (like missing ffmpeg) if available
statusText.innerText = response.message || "Error: Download Failed.";
statusText.style.color = "red";
}
});
function shakeInput() {
urlInput.style.borderColor = "red";
setTimeout(() => {
urlInput.style.borderColor = "#000";
}, 500);
}