-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
181 lines (157 loc) · 7.44 KB
/
popup.js
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
169
170
171
172
173
174
175
176
177
178
179
180
181
document.addEventListener('DOMContentLoaded', function() {
// Get form elements
const bookTitleInput = document.getElementById('bookTitle');
const authorNameInput = document.getElementById('authorName');
// Get button elements
const clearUrlsBtn = document.getElementById('clearUrlsBtn');
const downloadUrlsBtn = document.getElementById('downloadUrlsBtn');
const captureAudioBtn = document.getElementById('captureAudioBtn'); // New button
const statusDiv = document.getElementById('status');
const captureStatusDiv = document.getElementById('captureStatus'); // Changed to div
const progressDiv = document.querySelector('.progress');
const downloadCountSpan = document.getElementById('downloadCount');
const totalCountSpan = document.getElementById('totalCount');
const downloadProgress = document.getElementById('downloadProgress');
// Load saved metadata if available
chrome.storage.local.get(['bookTitle', 'authorName'], function(result) {
if (result.bookTitle) bookTitleInput.value = result.bookTitle;
if (result.authorName) authorNameInput.value = result.authorName;
});
// Save metadata when changed
bookTitleInput.addEventListener('change', saveMetadata);
authorNameInput.addEventListener('change', saveMetadata);
function saveMetadata() {
chrome.storage.local.set({
'bookTitle': bookTitleInput.value,
'authorName': authorNameInput.value
});
}
// Check current capture status and URL count
updateCaptureStatus();
function updateCaptureStatus() {
chrome.storage.local.get(['capturedURLs'], function(result) {
const count = result.capturedURLs ? result.capturedURLs.length : 0;
captureStatusDiv.textContent = `Monitoring for media URLs (${count} captured)`; // Use div
// Disable download button if fields are empty or no URLs captured
const metadataComplete = bookTitleInput.value.trim() !== '' && authorNameInput.value.trim() !== '';
downloadUrlsBtn.disabled = count === 0 || !metadataComplete;
if (count === 0) {
statusDiv.textContent = "Capture some media URLs first";
} else if (!metadataComplete) {
statusDiv.textContent = "Please fill in the book title and author";
} else {
statusDiv.textContent = "Ready to download";
}
});
}
// Update status when input fields change
bookTitleInput.addEventListener('input', updateCaptureStatus);
authorNameInput.addEventListener('input', updateCaptureStatus);
// Add click event listener for Clear URLs button
clearUrlsBtn.addEventListener('click', function() {
// Send message to the background script to clear URLs
chrome.runtime.sendMessage({action: "clearURLs"}, function(response) {
if (response) {
statusDiv.textContent = `${response.status}`;
updateCaptureStatus();
} else {
statusDiv.textContent = "Error: Could not communicate with extension background process";
}
});
});
// Add click event listener for Download URLs button
downloadUrlsBtn.addEventListener('click', function() {
// Save metadata before downloading
saveMetadata();
const bookTitle = bookTitleInput.value.trim();
const authorName = authorNameInput.value.trim();
if (!bookTitle || !authorName) {
statusDiv.textContent = "Please enter book title and author name";
return;
}
chrome.storage.local.get(['capturedURLs'], async function(result) {
if (!result.capturedURLs || result.capturedURLs.length === 0) {
statusDiv.textContent = "No URLs to download";
return;
}
const urls = result.capturedURLs;
downloadUrlsBtn.disabled = true;
statusDiv.textContent = `Starting download of ${urls.length} files...`;
// Show progress
progressDiv.style.display = 'block';
totalCountSpan.textContent = urls.length;
downloadProgress.max = urls.length;
try {
for (let i = 0; i < urls.length; i++) {
const trackNumber = i + 1;
downloadCountSpan.textContent = trackNumber;
downloadProgress.value = trackNumber;
// Prepare metadata
const metadata = {
title: `${bookTitle} - Part ${trackNumber}`,
artist: authorName,
author: authorName,
trackNumber: trackNumber,
album: bookTitle // Add album title
};
// Generate filename
const filename = `${bookTitle} - Part ${trackNumber}.mp3`;
// Download file with ID3 tags
await downloadFile(urls[i], filename, metadata);
// Update status
statusDiv.textContent = `Downloaded ${trackNumber}/${urls.length} files`;
}
statusDiv.textContent = `Successfully downloaded all ${urls.length} files!`;
} catch (error) {
statusDiv.textContent = `Error downloading: ${error.message}`;
console.error("Download error:", error);
} finally {
downloadUrlsBtn.disabled = false;
}
});
});
// Function to download a file with ID3 tags
async function downloadFile(url, filename, metadata) {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage(
{
action: "downloadURL",
url: url,
filename: filename,
metadata: metadata
},
function(response) {
if (response && response.success) {
resolve();
} else {
reject(new Error(response ? response.error : "Unknown error"));
}
}
);
});
}
// Listen for messages from the background script (for author field update)
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action === "updateAuthorField") {
authorNameInput.value = '';
updateCaptureStatus(); // Refresh the capture status
}
//** Listen for updateURLCount messages from the background script**
else if (request.action === "updateURLCount") {
const count = request.urlCount;
captureStatusDiv.textContent = `Monitoring for media URLs (${count} captured)`;
downloadUrlsBtn.disabled = count === 0 || (bookTitleInput.value.trim() === '' || authorNameInput.value.trim() === ''); //Update based on the new count.
}
});
//Add click event listener for Capture Audio button
captureAudioBtn.addEventListener('click', function() {
// Send message to the background script to trigger capture
chrome.runtime.sendMessage({action: "captureAudio"}, function(response) {
if (response && response.status) {
statusDiv.textContent = response.status;
} else {
statusDiv.textContent = "Error triggering audio capture.";
}
});
});
});