-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
332 lines (287 loc) · 11.1 KB
/
Copy pathpopup.js
File metadata and controls
332 lines (287 loc) · 11.1 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**
* 智能表单数据模拟助手 - 弹出窗口脚本
* 处理用户交互和与内容脚本的通信
*/
class PopupManager {
constructor() {
this.currentTab = null;
this.init();
}
/**
* 初始化弹出窗口
*/
async init() {
try {
// 获取当前活动标签页
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
this.currentTab = tabs[0];
// 绑定事件
this.bindEvents();
// 检查插件状态
this.checkPluginStatus();
// console.log('弹出窗口初始化完成');
} catch (error) {
console.error('弹出窗口初始化失败:', error);
this.showStatus('初始化失败', 'error');
}
}
/**
* 绑定按钮事件
*/
bindEvents() {
const quickFillBtn = document.getElementById('quickFill');
const repeatFillBtn = document.getElementById('repeatFill');
const openSettingsBtn = document.getElementById('openSettings');
// 快速填充按钮
quickFillBtn.addEventListener('click', () => {
this.quickFill();
});
// 重复填充按钮
repeatFillBtn.addEventListener('click', () => {
this.repeatFill();
});
// 打开设置按钮
openSettingsBtn.addEventListener('click', () => {
this.openSettings();
});
// 添加键盘快捷键支持
document.addEventListener('keydown', (e) => {
if (e.ctrlKey || e.metaKey) {
switch (e.key) {
case '1':
e.preventDefault();
this.quickFill();
break;
case '2':
e.preventDefault();
this.repeatFill();
break;
}
}
});
}
/**
* 快速填充当前页面
*/
async quickFill() {
if (!this.currentTab) {
this.showStatus('无法获取当前标签页', 'error');
return;
}
this.showStatus('正在填充表单...', 'loading');
try {
// 添加重试机制
let retryCount = 0;
const maxRetries = 3;
const sendMessageWithRetry = async () => {
try {
// 向内容脚本发送填充命令
const response = await chrome.tabs.sendMessage(this.currentTab.id, {
action: 'quickFillAll'
});
if (response && response.success) {
this.showStatus(`成功填充了 ${response.filledCount} 个表单元素`, 'success');
// 3秒后恢复就绪状态
setTimeout(() => {
this.showStatus('就绪', 'ready');
}, 3000);
return true;
} else {
this.showStatus('填充失败,请刷新页面重试', 'error');
return false;
}
} catch (error) {
retryCount++;
if (retryCount < maxRetries) {
// 等待500ms后重试
this.showStatus(`重试填充中... (${retryCount}/${maxRetries})`, 'loading');
await new Promise(resolve => setTimeout(resolve, 500));
return await sendMessageWithRetry();
} else {
// 重试次数用尽
console.error('快速填充失败:', error);
this.showStatus('填充失败:内容脚本未加载,请刷新页面', 'error');
return false;
}
}
};
await sendMessageWithRetry();
} catch (error) {
console.error('快速填充失败:', error);
this.showStatus('填充失败:内容脚本未加载', 'error');
}
}
/**
* 重复上次填充
*/
async repeatFill() {
if (!this.currentTab) {
this.showStatus('无法获取当前标签页', 'error');
return;
}
this.showStatus('正在重复填充...', 'loading');
try {
// 添加重试机制
let retryCount = 0;
const maxRetries = 3;
const sendMessageWithRetry = async () => {
try {
const response = await chrome.tabs.sendMessage(this.currentTab.id, {
action: 'repeatFill'
});
if (response && response.success) {
this.showStatus(`重复填充了 ${response.filledCount} 个表单元素`, 'success');
setTimeout(() => {
this.showStatus('就绪', 'ready');
}, 3000);
return true;
} else {
this.showStatus('重复填充失败,请先执行快速填充', 'error');
return false;
}
} catch (error) {
retryCount++;
if (retryCount < maxRetries) {
// 等待500ms后重试
this.showStatus(`重试重复填充中... (${retryCount}/${maxRetries})`, 'loading');
await new Promise(resolve => setTimeout(resolve, 500));
return await sendMessageWithRetry();
} else {
// 重试次数用尽
console.error('重复填充失败:', error);
this.showStatus('重复填充失败:内容脚本未加载,请刷新页面', 'error');
return false;
}
}
};
await sendMessageWithRetry();
} catch (error) {
console.error('重复填充失败:', error);
this.showStatus('重复填充失败', 'error');
}
}
/**
* 打开设置页面
*/
openSettings() {
// 在新标签页中打开自定义设置页面
chrome.tabs.create({
url: chrome.runtime.getURL('settings.html'),
active: true
});
}
/**
* 显示状态信息
*/
showStatus(message, type = 'ready') {
const statusText = document.getElementById('statusText');
const loading = document.getElementById('loading');
const status = document.getElementById('status');
statusText.textContent = message;
// 根据类型设置样式
status.className = 'status';
if (type === 'loading') {
status.classList.add('loading-active');
loading.classList.remove('hidden');
} else {
loading.classList.add('hidden');
if (type === 'error') {
status.style.background = 'rgba(244, 67, 54, 0.2)';
status.style.border = '1px solid rgba(244, 67, 54, 0.3)';
} else if (type === 'success') {
status.style.background = 'rgba(76, 175, 80, 0.2)';
status.style.border = '1px solid rgba(76, 175, 80, 0.3)';
}
}
}
/**
* 检查插件状态
*/
async checkPluginStatus() {
if (!this.currentTab) return;
try {
// 添加重试机制,最多重试3次
let retryCount = 0;
const maxRetries = 3;
const checkStatusWithRetry = async () => {
try {
// console.log(`尝试检查插件状态 (第${retryCount + 1}次)`);
// 尝试与内容脚本通信检查状态
const response = await chrome.tabs.sendMessage(this.currentTab.id, {
action: 'checkStatus'
});
// console.log('状态检查响应:', response);
if (response && response.ready) {
this.showStatus('就绪', 'ready');
return true;
} else {
this.showStatus('内容脚本未就绪', 'warning');
return false;
}
} catch (error) {
// console.log(`状态检查失败 (第${retryCount + 1}次):`, error);
retryCount++;
if (retryCount < maxRetries) {
// 等待500ms后重试
this.showStatus(`重试检查中... (${retryCount}/${maxRetries})`, 'loading');
await new Promise(resolve => setTimeout(resolve, 500));
return await checkStatusWithRetry();
} else {
// 重试次数用尽
// console.log('状态检查重试次数用尽');
this.showStatus('请在网页中刷新后使用', 'warning');
return false;
}
}
};
await checkStatusWithRetry();
} catch (error) {
// 通信失败,可能是内容脚本未加载
console.error('状态检查最终失败:', error);
this.showStatus('请在网页中刷新后使用', 'warning');
}
}
/**
* 获取插件版本信息
*/
getVersionInfo() {
const manifest = chrome.runtime.getManifest();
return {
version: manifest.version,
name: manifest.name,
description: manifest.description
};
}
}
// 监听来自内容脚本的消息
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
switch (request.action) {
case 'fillComplete':
// 填充完成通知
if (popupManager) {
popupManager.showStatus(`填充完成: ${request.message}`, 'success');
}
sendResponse({ received: true });
break;
case 'fillError':
// 填充错误通知
if (popupManager) {
popupManager.showStatus(`填充错误: ${request.message}`, 'error');
}
sendResponse({ received: true });
break;
}
return true;
});
// 页面加载完成后初始化
let popupManager;
document.addEventListener('DOMContentLoaded', () => {
popupManager = new PopupManager();
// 显示版本信息
const versionInfo = popupManager.getVersionInfo();
// console.log(`智能表单数据模拟助手 v${versionInfo.version} 已加载`);
});
// 处理窗口关闭前的清理
window.addEventListener('beforeunload', () => {
// 可以在这里添加清理逻辑
});