-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
224 lines (194 loc) · 8.92 KB
/
Copy pathpopup.js
File metadata and controls
224 lines (194 loc) · 8.92 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// DeepRead 深度阅读助手 - 弹出界面脚本
// 添加调试信息
console.log('DeepRead popup script loaded!');
// 向当前标签页发送消息
function sendMessageToContentScript(action) {
console.log('Sending message to content script:', action);
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
if (tabs && tabs.length > 0) {
console.log('Active tab found:', tabs[0].id);
chrome.tabs.sendMessage(tabs[0].id, {action: action}, function(response) {
console.log('Response received:', response);
if (chrome.runtime.lastError) {
console.error('Error sending message:', chrome.runtime.lastError);
updateStatus('发送消息错误: ' + chrome.runtime.lastError.message);
} else if (response) {
updateStatus(response.message || '操作成功');
}
});
} else {
console.error('No active tab found');
updateStatus('未找到活动标签页');
}
});
}
const THINKING_LEVELS = ['OFF', 'MINIMAL', 'LOW', 'MEDIUM', 'HIGH'];
const THINKING_VALUE_TO_LEVEL = {
MINIMAL: 1,
LOW: 2,
MEDIUM: 3,
HIGH: 4,
};
const THINKING_LEVEL_TO_VALUE = {
0: '',
1: 'MINIMAL',
2: 'LOW',
3: 'MEDIUM',
4: 'HIGH',
};
const MEM0_ENABLED_KEY = 'deepread_mem0_enabled';
const MEM0_API_KEY_KEY = 'deepread_mem0_api_key';
const MEM0_USER_ID_KEY = 'deepread_mem0_user_id';
const MEM0_AGENT_ID_KEY = 'deepread_mem0_agent_id';
function setThinkingLabel(v) {
const n = Number(v);
const idx = Number.isFinite(n) ? Math.max(0, Math.min(4, Math.round(n))) : 0;
const el = document.getElementById('dr-popup-thinking-label');
if (el) el.textContent = THINKING_LEVELS[idx];
}
async function loadSettingsToPopup() {
try {
const res = await chrome.storage.sync.get([
'deepread_api_key',
'deepread_model',
'deepread_thinking_level',
'deepread_feishu_webhook_url',
MEM0_ENABLED_KEY,
MEM0_API_KEY_KEY,
MEM0_USER_ID_KEY,
MEM0_AGENT_ID_KEY,
]);
const apiKey = res && res.deepread_api_key ? String(res.deepread_api_key) : '';
const model = res && res.deepread_model ? String(res.deepread_model) : 'gemini-flash-lite-latest';
const thinkingRaw = res && typeof res.deepread_thinking_level !== 'undefined' ? String(res.deepread_thinking_level || '').trim() : '';
const feishu = res && res.deepread_feishu_webhook_url ? String(res.deepread_feishu_webhook_url) : '';
const mem0Enabled = !!(res && res[MEM0_ENABLED_KEY]);
const mem0ApiKey = res && res[MEM0_API_KEY_KEY] ? String(res[MEM0_API_KEY_KEY]) : '';
const mem0UserId = res && res[MEM0_USER_ID_KEY] ? String(res[MEM0_USER_ID_KEY]) : '';
const mem0AgentId = res && res[MEM0_AGENT_ID_KEY] ? String(res[MEM0_AGENT_ID_KEY]) : '';
const apiInput = document.getElementById('dr-popup-api-key');
if (apiInput) apiInput.value = apiKey;
const modelSel = document.getElementById('dr-popup-model');
const modelCustom = document.getElementById('dr-popup-model-custom');
if (modelSel) {
const builtins = ['gemini-flash-latest', 'gemini-flash-lite-latest', 'gemini-3.1-flash-lite-preview'];
if (builtins.includes(model)) {
modelSel.value = model;
if (modelCustom) {
modelCustom.style.display = 'none';
modelCustom.value = '';
}
} else {
modelSel.value = 'custom';
if (modelCustom) {
modelCustom.style.display = 'block';
modelCustom.value = model;
}
}
}
const thinking = document.getElementById('dr-popup-thinking');
if (thinking) {
const v = THINKING_VALUE_TO_LEVEL[thinkingRaw] || 0;
thinking.value = String(v);
setThinkingLabel(v);
}
const feishuInput = document.getElementById('dr-popup-feishu');
if (feishuInput) feishuInput.value = feishu;
const mem0EnabledEl = document.getElementById('dr-popup-mem0-enabled');
if (mem0EnabledEl) mem0EnabledEl.checked = mem0Enabled;
const mem0ApiKeyEl = document.getElementById('dr-popup-mem0-api-key');
if (mem0ApiKeyEl) mem0ApiKeyEl.value = mem0ApiKey;
const mem0UserIdEl = document.getElementById('dr-popup-mem0-user-id');
if (mem0UserIdEl) mem0UserIdEl.value = mem0UserId;
const mem0AgentIdEl = document.getElementById('dr-popup-mem0-agent-id');
if (mem0AgentIdEl) mem0AgentIdEl.value = mem0AgentId;
} catch (e) {
// ignore
}
}
async function saveSettingsFromPopup() {
const apiKey = String(document.getElementById('dr-popup-api-key')?.value || '').trim();
const modelSel = String(document.getElementById('dr-popup-model')?.value || '').trim();
const modelCustom = String(document.getElementById('dr-popup-model-custom')?.value || '').trim();
const thinkingLevel = Number(document.getElementById('dr-popup-thinking')?.value || 0);
const feishu = String(document.getElementById('dr-popup-feishu')?.value || '').trim();
const mem0Enabled = !!document.getElementById('dr-popup-mem0-enabled')?.checked;
const mem0ApiKey = String(document.getElementById('dr-popup-mem0-api-key')?.value || '').trim();
const mem0UserId = String(document.getElementById('dr-popup-mem0-user-id')?.value || '').trim();
const mem0AgentId = String(document.getElementById('dr-popup-mem0-agent-id')?.value || '').trim();
const modelId = modelSel === 'custom' ? modelCustom : modelSel;
const thinkingValue = THINKING_LEVEL_TO_VALUE[Math.max(0, Math.min(4, Math.round(Number.isFinite(thinkingLevel) ? thinkingLevel : 0)))] || '';
await chrome.storage.sync.set({
deepread_api_key: apiKey,
deepread_model: modelId,
deepread_thinking_level: thinkingValue,
deepread_feishu_webhook_url: feishu,
[MEM0_ENABLED_KEY]: mem0Enabled,
[MEM0_API_KEY_KEY]: mem0ApiKey,
[MEM0_USER_ID_KEY]: mem0UserId,
[MEM0_AGENT_ID_KEY]: mem0AgentId,
});
}
// 更新状态信息
function updateStatus(message) {
document.getElementById('status').textContent = '状态: ' + message;
}
document.addEventListener('DOMContentLoaded', function() {
// 开始深度阅读(如果按钮存在)
const startReadingBtn = document.getElementById('startReading');
if (startReadingBtn) {
startReadingBtn.addEventListener('click', function() {
sendMessageToContentScript('startReading');
updateStatus('正在分析页面内容...');
// 模拟分析完成后的状态更新
setTimeout(function() {
updateStatus('分析完成!请在页面上点击带下划线的概念词汇获取解读。');
}, 2000);
});
}
// 显示/隐藏面板(如果按钮存在)
const togglePanelBtn = document.getElementById('togglePanel');
if (togglePanelBtn) {
togglePanelBtn.addEventListener('click', function() {
sendMessageToContentScript('togglePanel');
updateStatus('面板显示状态已切换。');
});
};
document.getElementById('dr-popup-model').addEventListener('change', function() {
const v = String(this.value || '');
const custom = document.getElementById('dr-popup-model-custom');
if (!custom) return;
custom.style.display = v === 'custom' ? 'block' : 'none';
});
document.getElementById('dr-popup-thinking').addEventListener('input', function() {
setThinkingLabel(this.value);
});
document.getElementById('dr-popup-save').addEventListener('click', async function() {
try {
await saveSettingsFromPopup();
updateStatus('设置已保存');
} catch (e) {
updateStatus('保存失败: ' + String(e && e.message ? e.message : e));
}
});
document.getElementById('dr-popup-open-sidepanel').addEventListener('click', function() {
chrome.tabs.query({ active: true, currentWindow: true }, async (tabs) => {
try {
const tab = tabs && tabs[0];
if (!tab || typeof tab.id !== 'number') {
updateStatus('未找到活动标签页');
return;
}
if (chrome.sidePanel && chrome.sidePanel.open) {
await chrome.sidePanel.open({ tabId: tab.id });
updateStatus('已打开侧栏');
} else {
updateStatus('当前浏览器不支持 side panel');
}
} catch (e) {
updateStatus('打开侧栏失败: ' + String(e && e.message ? e.message : e));
}
});
});
loadSettingsToPopup();
});