background.js aktualisiert
This commit is contained in:
193
background.js
193
background.js
@@ -1,99 +1,96 @@
|
|||||||
// background.js
|
const downloadsMap = new Map(); // downloadId -> { tabId, filename }
|
||||||
// Service worker / background script: startet chrome.downloads.download und sendet Fortschritte an die Content-Tab.
|
|
||||||
|
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
|
||||||
const downloadsMap = new Map(); // downloadId -> { tabId, filename }
|
if (!msg || !msg.action) {
|
||||||
|
sendResponse({ status: "unknown" });
|
||||||
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
|
return;
|
||||||
if (!msg || !msg.action) {
|
}
|
||||||
sendResponse({ status: "unknown" });
|
|
||||||
return;
|
if (msg.action === "videoProgressCompleted") {
|
||||||
}
|
console.log('Video completed:', msg.videoId, 'name:', msg.name);
|
||||||
|
sendResponse({ status: "success" });
|
||||||
if (msg.action === "videoProgressCompleted") {
|
return;
|
||||||
console.log('Video completed:', msg.videoId, 'name:', msg.name);
|
}
|
||||||
sendResponse({ status: "success" });
|
|
||||||
return;
|
if (msg.action === "startBackgroundDownload") {
|
||||||
}
|
const url = msg.url;
|
||||||
|
let filename = msg.filename || ('download_' + Date.now());
|
||||||
if (msg.action === "startBackgroundDownload") {
|
|
||||||
const url = msg.url;
|
if (!url) {
|
||||||
let filename = msg.filename || ('download_' + Date.now());
|
sendResponse({ status: "error", message: "No URL provided" });
|
||||||
|
return;
|
||||||
if (!url) {
|
}
|
||||||
sendResponse({ status: "error", message: "No URL provided" });
|
|
||||||
return;
|
try {
|
||||||
}
|
chrome.downloads.download(
|
||||||
|
{
|
||||||
try {
|
url: url,
|
||||||
chrome.downloads.download(
|
filename: filename,
|
||||||
{
|
conflictAction: "uniquify",
|
||||||
url: url,
|
saveAs: false
|
||||||
filename: filename,
|
},
|
||||||
conflictAction: "uniquify",
|
downloadId => {
|
||||||
saveAs: false
|
if (chrome.runtime.lastError) {
|
||||||
},
|
console.error('chrome.downloads.download error:', chrome.runtime.lastError.message);
|
||||||
downloadId => {
|
sendResponse({ status: "error", message: chrome.runtime.lastError.message });
|
||||||
if (chrome.runtime.lastError) {
|
} else {
|
||||||
console.error('chrome.downloads.download error:', chrome.runtime.lastError.message);
|
const tabId = (sender && sender.tab && sender.tab.id) ? sender.tab.id : null;
|
||||||
sendResponse({ status: "error", message: chrome.runtime.lastError.message });
|
downloadsMap.set(downloadId, { tabId, filename });
|
||||||
} else {
|
sendResponse({ status: "started", downloadId });
|
||||||
const tabId = (sender && sender.tab && sender.tab.id) ? sender.tab.id : null;
|
}
|
||||||
downloadsMap.set(downloadId, { tabId, filename });
|
}
|
||||||
sendResponse({ status: "started", downloadId });
|
);
|
||||||
}
|
return true;
|
||||||
}
|
} catch (e) {
|
||||||
);
|
console.error('Exception starting download', e);
|
||||||
return true;
|
sendResponse({ status: "error", message: String(e) });
|
||||||
} catch (e) {
|
return;
|
||||||
console.error('Exception starting download', e);
|
}
|
||||||
sendResponse({ status: "error", message: String(e) });
|
}
|
||||||
return;
|
|
||||||
}
|
sendResponse({ status: "ok" });
|
||||||
}
|
});
|
||||||
|
|
||||||
sendResponse({ status: "ok" });
|
// Listen for download changes and forward progress/state to the tab
|
||||||
});
|
chrome.downloads.onChanged.addListener(delta => {
|
||||||
|
const info = downloadsMap.get(delta.id);
|
||||||
// Listen for download changes and forward progress/state to the tab
|
if (!info || !info.tabId) return;
|
||||||
chrome.downloads.onChanged.addListener(delta => {
|
|
||||||
const info = downloadsMap.get(delta.id);
|
if (delta.bytesReceived) {
|
||||||
if (!info || !info.tabId) return;
|
chrome.downloads.search({ id: delta.id }, items => {
|
||||||
|
const item = items && items[0];
|
||||||
if (delta.bytesReceived) {
|
if (!item) return;
|
||||||
chrome.downloads.search({ id: delta.id }, items => {
|
|
||||||
const item = items && items[0];
|
const received = item.bytesReceived || 0;
|
||||||
if (!item) return;
|
const total = item.totalBytes || 0;
|
||||||
|
const percent = total > 0 ? Math.round(100 * received / total) : null;
|
||||||
const received = item.bytesReceived || 0;
|
|
||||||
const total = item.totalBytes || 0;
|
chrome.tabs.sendMessage(info.tabId, {
|
||||||
const percent = total > 0 ? Math.round(100 * received / total) : null;
|
action: 'downloadProgress',
|
||||||
|
downloadId: delta.id,
|
||||||
chrome.tabs.sendMessage(info.tabId, {
|
bytesReceived: received,
|
||||||
action: 'downloadProgress',
|
totalBytes: total,
|
||||||
downloadId: delta.id,
|
percent
|
||||||
bytesReceived: received,
|
}, () => { /* ignore response */ });
|
||||||
totalBytes: total,
|
});
|
||||||
percent
|
}
|
||||||
}, () => { /* ignore response */ });
|
|
||||||
});
|
if (delta.state && delta.state.current === 'complete') {
|
||||||
}
|
chrome.tabs.sendMessage(info.tabId, {
|
||||||
|
action: 'downloadComplete',
|
||||||
if (delta.state && delta.state.current === 'complete') {
|
downloadId: delta.id,
|
||||||
chrome.tabs.sendMessage(info.tabId, {
|
filename: info.filename
|
||||||
action: 'downloadComplete',
|
}, () => { /* ignore response */ });
|
||||||
downloadId: delta.id,
|
downloadsMap.delete(delta.id);
|
||||||
filename: info.filename
|
}
|
||||||
}, () => { /* ignore response */ });
|
|
||||||
downloadsMap.delete(delta.id);
|
if (delta.state && delta.state.current === 'interrupted') {
|
||||||
}
|
chrome.tabs.sendMessage(info.tabId, {
|
||||||
|
action: 'downloadFailed',
|
||||||
if (delta.state && delta.state.current === 'interrupted') {
|
downloadId: delta.id,
|
||||||
chrome.tabs.sendMessage(info.tabId, {
|
filename: info.filename,
|
||||||
action: 'downloadFailed',
|
reason: delta.state && delta.state.current
|
||||||
downloadId: delta.id,
|
}, () => { /* ignore response */ });
|
||||||
filename: info.filename,
|
downloadsMap.delete(delta.id);
|
||||||
reason: delta.state && delta.state.current
|
}
|
||||||
}, () => { /* ignore response */ });
|
|
||||||
downloadsMap.delete(delta.id);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user