Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
74 lines (63 loc) · 1.9 KB
/
Copy pathbackground.js
File metadata and controls
74 lines (63 loc) · 1.9 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
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (!message || !message.action) return;
if (message.action === 'deepread_get_tab_id') {
try {
const tabId = sender && sender.tab ? sender.tab.id : null;
sendResponse({ ok: true, tabId });
} catch (err) {
sendResponse({ ok: false, tabId: null, error: String(err && err.message ? err.message : err) });
}
return true;
}
if (message.action !== 'deepread_send_feishu_webhook') return;
(async () => {
try {
const { webhookUrl, payload } = message;
if (!webhookUrl) throw new Error('Missing webhookUrl');
const res = await fetch(webhookUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload || {})
});
if (!res.ok) {
throw new Error(`HTTP ${res.status}`);
}
sendResponse({ ok: true });
} catch (err) {
console.warn('DeepRead background: send feishu webhook failed:', err);
sendResponse({ ok: false, error: String(err && err.message ? err.message : err) });
}
})();
return true;
});
chrome.action.onClicked.addListener(async (tab) => {
try {
if (!tab || typeof tab.id !== 'number') return;
if (chrome.sidePanel && chrome.sidePanel.open) {
await chrome.sidePanel.open({ tabId: tab.id });
}
} catch (err) {
console.warn('DeepRead background: open side panel failed:', err);
}
});
chrome.tabs.onRemoved.addListener((tabId) => {
const key = `deepread_chat_history_tab_${tabId}`;
try {
if (chrome.storage && chrome.storage.session) {
chrome.storage.session.remove([key]).catch(() => {
// ignore
});
}
} catch (e) {
// ignore
}
try {
if (chrome.storage && chrome.storage.local) {
chrome.storage.local.remove([key]).catch(() => {
// ignore
});
}
} catch (e) {
// ignore
}
});