-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.js
More file actions
495 lines (430 loc) · 17.4 KB
/
Copy pathsettings.js
File metadata and controls
495 lines (430 loc) · 17.4 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/**
* 智能表单数据模拟助手 - 设置页面逻辑
*/
class SettingsPage {
constructor() {
this.currentSettings = null;
this.init();
}
/**
* 初始化设置页面
*/
init() {
this.bindEvents();
this.loadCurrentSettings();
// 延迟更新UI,确保设置完全加载后再更新界面
setTimeout(() => {
this.updateUI();
// 强制同步模式选择器状态
this.forceSyncModeSelector();
}, 100);
}
/**
* 绑定事件监听器
*/
bindEvents() {
// 保存按钮
document.getElementById('saveBtn').addEventListener('click', () => {
this.saveSettings();
});
// 重置按钮
document.getElementById('resetBtn').addEventListener('click', () => {
this.resetToDefaults();
});
// 数据模式切换事件
const dataModeRadios = document.getElementsByName('dataMode');
dataModeRadios.forEach(radio => {
radio.addEventListener('change', () => {
this.updateRecommendationBadge();
});
});
// 范围滑块事件
this.bindRangeEvents();
// 复选框联动
this.bindCheckboxDependencies();
}
/**
* 绑定范围滑块事件
*/
bindRangeEvents() {
const ranges = [
{ id: 'intervalMin', valueId: 'intervalMinValue', suffix: 'ms' },
{ id: 'intervalMax', valueId: 'intervalMaxValue', suffix: 'ms' },
{ id: 'typingSpeed', valueId: 'typingSpeedValue', suffix: ' 字符/秒' }
];
ranges.forEach(range => {
const slider = document.getElementById(range.id);
const valueSpan = document.getElementById(range.valueId);
if (slider && valueSpan) {
slider.addEventListener('input', () => {
valueSpan.textContent = slider.value + range.suffix;
});
}
});
}
/**
* 绑定复选框依赖关系
*/
bindCheckboxDependencies() {
// 填充间隔启用状态影响范围滑块
const enableInterval = document.getElementById('enableInterval');
const intervalMin = document.getElementById('intervalMin');
const intervalMax = document.getElementById('intervalMax');
if (enableInterval && intervalMin && intervalMax) {
enableInterval.addEventListener('change', () => {
const disabled = !enableInterval.checked;
intervalMin.disabled = disabled;
intervalMax.disabled = disabled;
});
}
// 自然填充启用状态影响相关设置
const enableNaturalMode = document.getElementById('enableNaturalMode');
const typingSpeed = document.getElementById('typingSpeed');
const enableRandomPauses = document.getElementById('enableRandomPauses');
if (enableNaturalMode && typingSpeed && enableRandomPauses) {
enableNaturalMode.addEventListener('change', () => {
const disabled = !enableNaturalMode.checked;
typingSpeed.disabled = disabled;
enableRandomPauses.disabled = disabled;
});
}
}
/**
* 加载当前设置
*/
loadCurrentSettings() {
// 默认设置
this.currentSettings = {
dataMode: 'basic',
fillInterval: {
min: 100,
max: 500,
enabled: true
},
naturalMode: {
enabled: true,
humanLike: true,
typingSpeed: 50,
randomPauses: true
},
autoDetectPlaceholder: true,
showFloatingWidget: true,
enableContextMenu: true
};
// 尝试从存储加载设置
try {
const stored = localStorage.getItem('smartFormFillerSettings');
if (stored) {
const parsed = JSON.parse(stored);
// 深度合并设置,确保所有嵌套对象都被正确合并
this.currentSettings = this.deepMerge(this.currentSettings, parsed);
// console.log('从localStorage加载的设置:', this.currentSettings);
} else {
// localStorage为空时,尝试从chrome.storage.local恢复设置(解决插件重装时的模式同步问题)
if (typeof chrome !== 'undefined' && chrome.storage && chrome.storage.local) {
chrome.storage.local.get(['smartFormFillerSettings'], (result) => {
if (result.smartFormFillerSettings) {
// console.log('从chrome.storage.local恢复设置:', result.smartFormFillerSettings);
// 深度合并设置
this.currentSettings = this.deepMerge(this.currentSettings, result.smartFormFillerSettings);
// 同步回localStorage
localStorage.setItem('smartFormFillerSettings', JSON.stringify(result.smartFormFillerSettings));
// 更新UI
this.updateUI();
}
});
}
}
} catch (error) {
console.error('加载设置失败:', error);
}
}
/**
* 深度合并对象
*/
deepMerge(target, source) {
const result = { ...target };
for (const key in source) {
if (source.hasOwnProperty(key)) {
if (source[key] && typeof source[key] === 'object' && !Array.isArray(source[key])) {
// 如果是对象,递归合并
result[key] = this.deepMerge(result[key] || {}, source[key]);
} else {
// 否则直接赋值
result[key] = source[key];
}
}
}
return result;
}
/**
* 强制同步模式选择器状态
* 解决插件重装后模式显示不一致的问题
*/
forceSyncModeSelector() {
const dataModeRadios = document.getElementsByName('dataMode');
const currentMode = this.currentSettings.dataMode;
// console.log('强制同步模式选择器,当前模式:', currentMode);
dataModeRadios.forEach(radio => {
radio.checked = radio.value === currentMode;
// console.log(` 选项 ${radio.value}: ${radio.checked ? '选中' : '未选中'}`);
});
// 更新推荐标签显示
this.updateRecommendationBadge();
}
/**
* 更新UI显示
*/
updateUI() {
// 数据模式
const dataModeRadios = document.getElementsByName('dataMode');
dataModeRadios.forEach(radio => {
radio.checked = radio.value === this.currentSettings.dataMode;
});
// 更新推荐标签显示
this.updateRecommendationBadge();
// 填充间隔
document.getElementById('enableInterval').checked = this.currentSettings.fillInterval.enabled;
document.getElementById('intervalMin').value = this.currentSettings.fillInterval.min;
document.getElementById('intervalMax').value = this.currentSettings.fillInterval.max;
document.getElementById('intervalMinValue').textContent = this.currentSettings.fillInterval.min + 'ms';
document.getElementById('intervalMaxValue').textContent = this.currentSettings.fillInterval.max + 'ms';
// 自然填充模式
document.getElementById('enableNaturalMode').checked = this.currentSettings.naturalMode.enabled;
document.getElementById('typingSpeed').value = this.currentSettings.naturalMode.typingSpeed;
document.getElementById('typingSpeedValue').textContent = this.currentSettings.naturalMode.typingSpeed + ' 字符/秒';
document.getElementById('enableRandomPauses').checked = this.currentSettings.naturalMode.randomPauses;
// 其他设置
document.getElementById('autoDetectPlaceholder').checked = this.currentSettings.autoDetectPlaceholder;
document.getElementById('showFloatingWidget').checked = this.currentSettings.showFloatingWidget;
document.getElementById('enableContextMenu').checked = this.currentSettings.enableContextMenu;
// 更新控件状态
this.updateControlStates();
}
/**
* 更新推荐标签显示
*/
updateRecommendationBadge() {
// 获取所有推荐标签
const basicRadio = document.querySelector('input[name="dataMode"][value="basic"]');
const advancedRadio = document.querySelector('input[name="dataMode"][value="advanced"]');
if (!basicRadio || !advancedRadio) return;
const basicBadge = basicRadio.parentNode.querySelector('.feature-badge');
const advancedBadge = advancedRadio.parentNode.querySelector('.feature-badge');
// 清除所有推荐标签
if (basicBadge) basicBadge.style.display = 'none';
if (advancedBadge) advancedBadge.style.display = 'none';
// 根据当前选中的模式显示推荐标签
if (basicRadio.checked && basicBadge) {
basicBadge.style.display = 'inline-block';
basicBadge.textContent = '当前选中';
} else if (advancedRadio.checked && advancedBadge) {
advancedBadge.style.display = 'inline-block';
advancedBadge.textContent = '当前选中';
}
}
/**
* 更新控件状态
*/
updateControlStates() {
// 填充间隔控件
const enableInterval = document.getElementById('enableInterval');
const intervalMin = document.getElementById('intervalMin');
const intervalMax = document.getElementById('intervalMax');
if (enableInterval && intervalMin && intervalMax) {
const disabled = !enableInterval.checked;
intervalMin.disabled = disabled;
intervalMax.disabled = disabled;
}
// 自然填充控件
const enableNaturalMode = document.getElementById('enableNaturalMode');
const typingSpeed = document.getElementById('typingSpeed');
const enableRandomPauses = document.getElementById('enableRandomPauses');
if (enableNaturalMode && typingSpeed && enableRandomPauses) {
const disabled = !enableNaturalMode.checked;
typingSpeed.disabled = disabled;
enableRandomPauses.disabled = disabled;
}
}
/**
* 从UI获取当前设置
*/
getSettingsFromUI() {
return {
dataMode: document.querySelector('input[name="dataMode"]:checked')?.value || 'basic',
fillInterval: {
min: parseInt(document.getElementById('intervalMin').value) || 100,
max: parseInt(document.getElementById('intervalMax').value) || 500,
enabled: document.getElementById('enableInterval').checked
},
naturalMode: {
enabled: document.getElementById('enableNaturalMode').checked,
humanLike: document.getElementById('enableNaturalMode').checked,
typingSpeed: parseInt(document.getElementById('typingSpeed').value) || 50,
randomPauses: document.getElementById('enableRandomPauses').checked
},
autoDetectPlaceholder: document.getElementById('autoDetectPlaceholder').checked,
showFloatingWidget: document.getElementById('showFloatingWidget').checked,
enableContextMenu: document.getElementById('enableContextMenu').checked
};
}
/**
* 保存设置
*/
saveSettings() {
try {
const newSettings = this.getSettingsFromUI();
// 验证设置
if (newSettings.fillInterval.min >= newSettings.fillInterval.max) {
this.showStatus('最小间隔时间不能大于等于最大间隔时间', 'error');
return;
}
// 保存到localStorage
localStorage.setItem('smartFormFillerSettings', JSON.stringify(newSettings));
// 立即更新当前设置
this.currentSettings = newSettings;
// 发送消息到所有活动标签页的内容脚本
this.notifyContentScripts(newSettings);
// 同时保存到chrome.storage.local作为备用方案
this.saveToChromeStorage(newSettings);
// 显示成功消息
this.showStatus('✅ 设置已保存成功!', 'success');
// 5秒后隐藏状态消息,给用户更长时间查看
setTimeout(() => {
this.hideStatus();
}, 5000);
} catch (error) {
console.error('保存设置失败:', error);
this.showStatus('保存设置失败: ' + error.message, 'error');
}
}
/**
* 通知内容脚本设置已更改
*/
notifyContentScripts(settings) {
if (typeof chrome !== 'undefined' && chrome.tabs && chrome.tabs.query) {
// 查询所有活动标签页
chrome.tabs.query({}, (tabs) => {
tabs.forEach(tab => {
if (tab.id) {
try {
chrome.tabs.sendMessage(tab.id, {
action: "SETTINGS_CHANGED",
settings: settings
}).catch(error => {
// 忽略连接错误(内容脚本可能未加载)
if (!error.message.includes('Receiving end does not exist')) {
console.warn('发送消息到标签页失败:', tab.id, error);
}
});
} catch (error) {
// 忽略发送消息时的错误
console.warn('无法发送消息到标签页:', tab.id, error);
}
}
});
});
}
}
/**
* 保存设置到chrome.storage.local
*/
saveToChromeStorage(settings) {
if (typeof chrome !== 'undefined' && chrome.storage && chrome.storage.local) {
chrome.storage.local.set({ smartFormFillerSettings: settings }).catch(error => {
console.warn('保存到chrome.storage.local失败:', error);
});
}
}
/**
* 重置为默认设置
*/
resetToDefaults() {
if (confirm('确定要恢复默认设置吗?')) {
this.currentSettings = {
dataMode: 'basic',
fillInterval: {
min: 100,
max: 500,
enabled: true
},
naturalMode: {
enabled: true,
humanLike: true,
typingSpeed: 50,
randomPauses: true
},
autoDetectPlaceholder: true,
showFloatingWidget: true,
enableContextMenu: true
};
localStorage.removeItem('smartFormFillerSettings');
this.updateUI();
this.showStatus('已恢复默认设置', 'success');
setTimeout(() => {
this.hideStatus();
}, 3000);
}
}
/**
* 显示状态消息
*/
showStatus(message, type = 'success') {
const statusEl = document.getElementById('status');
const statusText = document.getElementById('statusText');
if (statusEl && statusText) {
statusText.textContent = message;
statusEl.className = 'status ' + type;
statusEl.style.display = 'block';
}
}
/**
* 隐藏状态消息
*/
hideStatus() {
const statusEl = document.getElementById('status');
if (statusEl) {
statusEl.style.display = 'none';
}
}
/**
* 导出设置(已隐藏)
*/
/*
exportSettings() {
const settings = this.getSettingsFromUI();
const dataStr = JSON.stringify(settings, null, 2);
const dataBlob = new Blob([dataStr], { type: 'application/json' });
const url = URL.createObjectURL(dataBlob);
const link = document.createElement('a');
link.href = url;
link.download = 'smart-form-filler-settings.json';
link.click();
URL.revokeObjectURL(url);
}
*/
/**
* 导入设置(已隐藏)
*/
/*
importSettings(file) {
const reader = new FileReader();
reader.onload = (e) => {
try {
const imported = JSON.parse(e.target.result);
this.currentSettings = { ...this.currentSettings, ...imported };
localStorage.setItem('smartFormFillerSettings', JSON.stringify(this.currentSettings));
this.updateUI();
this.showStatus('设置导入成功', 'success');
} catch (error) {
this.showStatus('导入设置失败: ' + error.message, 'error');
}
};
reader.readAsText(file);
}
*/
}
// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', () => {
new SettingsPage();
});