Add browser download confirmation and parameterized client startup
This commit is contained in:
1 parent
028fec64be
commit
3cf197a98c
25 files changed
+568
-46
No files matched your search
@@ -1,15 +1,20 @@
|
||||
import {DEFAULTS, httpUrl, eligible, linkReferrer, requestFor, handoff, recover} from './logic.mjs';
|
||||
const HOST = 'com.ttstd.ttdownloader';
|
||||
const observed = new Map(), busy = new Set();
|
||||
let recovery = null;
|
||||
let recovery = null, pollTimer = null;
|
||||
function pollConfirmation() {
|
||||
if (pollTimer) return;
|
||||
pollTimer = setTimeout(() => { pollTimer = null; restorePending(); }, 1500);
|
||||
}
|
||||
const config = async () => ({...DEFAULTS, ...(await chrome.storage.local.get('config')).config});
|
||||
async function status(result) {
|
||||
await chrome.storage.local.set({lastResult: {...result, time: Date.now()}});
|
||||
const waiting = result.status === 'awaiting_confirmation';
|
||||
const ok = ['accepted', 'ready'].includes(result.status);
|
||||
await chrome.action.setBadgeText({text: ok ? 'OK' : '!'});
|
||||
await chrome.action.setBadgeText({text: waiting ? '…' : ok ? 'OK' : '!'});
|
||||
await chrome.action.setBadgeBackgroundColor({color: ok ? '#247a48' : '#af4d00'});
|
||||
await chrome.action.setTitle({title: 'TTDownloader:' + result.message});
|
||||
if (!ok && result.status !== 'duplicate') chrome.notifications.create({type: 'basic', iconUrl: 'icon.png', title: 'TTDownloader 下载接管', message: result.message}, () => { void chrome.runtime.lastError; });
|
||||
if (!ok && !waiting && result.status !== 'duplicate') chrome.notifications.create({type: 'basic', iconUrl: 'icon.png', title: 'TTDownloader 下载接管', message: result.message}, () => { void chrome.runtime.lastError; });
|
||||
return result;
|
||||
}
|
||||
function native(request) {
|
||||
@@ -43,7 +48,12 @@ async function configure() {
|
||||
}
|
||||
async function restorePending() {
|
||||
if (recovery || busy.size) return recovery;
|
||||
recovery = (async () => { for (const outcome of await recover(chrome, native, (await config()).auto)) await status(outcome); })()
|
||||
recovery = (async () => {
|
||||
for (const outcome of await recover(chrome, native, (await config()).auto)) {
|
||||
await status(outcome);
|
||||
if (outcome.status === 'awaiting_confirmation') pollConfirmation();
|
||||
}
|
||||
})()
|
||||
.catch(error => status({status: 'error', message: '恢复下载状态失败:' + error.message})).finally(() => { recovery = null; });
|
||||
return recovery;
|
||||
}
|
||||
@@ -55,7 +65,10 @@ chrome.runtime.onStartup.addListener(() => { configure().catch(console.error); r
|
||||
chrome.alarms.onAlarm.addListener(alarm => { if (alarm.name === 'recover') restorePending(); });
|
||||
chrome.permissions.onAdded.addListener(() => configure().catch(console.error));
|
||||
chrome.permissions.onRemoved.addListener(() => configure().catch(console.error));
|
||||
chrome.storage.onChanged.addListener(changes => { if (changes.config) { configure().catch(console.error); restorePending(); } });
|
||||
chrome.storage.onChanged.addListener(changes => {
|
||||
if (changes.config) { configure().catch(console.error); restorePending(); }
|
||||
if (Object.entries(changes).some(([key, change]) => key.startsWith('pending_') && change.newValue?.phase === 'confirming')) pollConfirmation();
|
||||
});
|
||||
chrome.contextMenus.onClicked.addListener(async info => {
|
||||
if (info.menuItemId !== 'send') return;
|
||||
try { await status(await native(requestFor(info.linkUrl, linkReferrer(info.pageUrl, info.linkUrl)))); }
|
||||
@@ -68,7 +81,7 @@ chrome.downloads.onCreated.addListener(async item => {
|
||||
busy.add(item.id);
|
||||
try { await status(await handoff(chrome, item, requestFor(url, item.referrer, item.filename || ''), native)); }
|
||||
catch (error) { await status({status: 'error', message: '接管失败,请在下载页检查任务:' + error.message}); }
|
||||
finally { busy.delete(item.id); }
|
||||
finally { busy.delete(item.id); pollConfirmation(); }
|
||||
});
|
||||
chrome.runtime.onMessage.addListener((message, sender, respond) => {
|
||||
if (sender.id !== chrome.runtime.id || !sender.url?.startsWith(chrome.runtime.getURL(''))) return;
|
||||
|
||||
Reference in new issue
Block a user