-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
51 lines (46 loc) · 1.99 KB
/
Copy pathbackground.js
File metadata and controls
51 lines (46 loc) · 1.99 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
const HOST = 'com.cloudfile.local_agent';
const SETTINGS = { autoOpen: true };
async function sendNative(message) {
return chrome.runtime.sendNativeMessage(HOST, message);
}
async function updateState(state) {
await chrome.storage.local.set({ localSessionState: { ...state, at: Date.now() } });
}
function isSessionDownload(item) {
if (!item?.filename?.toLowerCase().endsWith('.cloudfile')) return false;
try {
const url = new URL(item.finalUrl || item.url);
// Hub creates the descriptor with a browser Blob so its download URL is
// usually blob:. The Agent validates the descriptor's server origin and
// one-time ticket before any network request, which is the security gate.
return url.protocol === 'https:' || url.protocol === 'http:' || url.protocol === 'blob:';
} catch {
return false;
}
}
chrome.downloads.onChanged.addListener(async (delta) => {
if (delta.state?.current !== 'complete') return;
try {
const { autoOpen } = await chrome.storage.local.get(SETTINGS);
const [item] = await chrome.downloads.search({ id: delta.id });
if (!isSessionDownload(item)) return;
if (!autoOpen) {
await updateState({ kind: 'paused', message: '已下载会话文件;自动打开已关闭。' });
return;
}
const result = await sendNative({ type: 'open_session_file', path: item.filename });
if (!result?.ok) throw new Error(result?.error || 'Agent rejected the session file.');
await updateState({ kind: 'ready', message: '已交给本地 Agent 打开。' });
} catch (error) {
await updateState({ kind: 'error', message: error.message || '无法交给本地 Agent。' });
console.warn('CloudFile Local Agent did not accept the session file.', error);
}
});
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
if (message?.type !== 'agent_status') return false;
sendNative({ type: 'status' }).then(sendResponse).catch((error) => sendResponse({
ok: false,
error: error.message,
}));
return true;
});