forked from MizuhaHimuraki/github-name-mapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
477 lines (406 loc) · 15.4 KB
/
Copy pathoptions.js
File metadata and controls
477 lines (406 loc) · 15.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
// GitHub Name Mapper - Options Page Script
document.addEventListener('DOMContentLoaded', init);
let currentConfig = null;
async function init() {
await loadConfig();
setupNavigation();
setupEventListeners();
loadLocalRules();
loadDataTable();
}
// 加载配置
async function loadConfig() {
const result = await sendMessage({ action: 'getConfig' });
if (result.success) {
currentConfig = result.data;
updateUI();
}
}
// 更新 UI 显示
function updateUI() {
// 启用开关
document.getElementById('enableToggle').checked = currentConfig.enabled;
updateStatusBadge(currentConfig.enabled);
// 功能开关
document.getElementById('featureReplaceToggle').checked = currentConfig.featureReplace !== false;
document.getElementById('featureHighlightToggle').checked = currentConfig.featureHighlight !== false;
document.getElementById('featureAvatarHighlightToggle').checked = currentConfig.featureAvatarHighlight !== false;
document.getElementById('featureMentionToggle').checked = currentConfig.featureMention !== false;
document.getElementById('debugToggle').checked = currentConfig.debug || false;
// 子开关禁用状态
updateSubSwitches(currentConfig.enabled);
// JSON URL
document.getElementById('jsonUrl').value = currentConfig.jsonUrl || '';
// 自动更新
document.getElementById('autoUpdate').checked = currentConfig.autoUpdate;
// 统计信息
document.getElementById('remoteCount').textContent = currentConfig.developers?.length || 0;
document.getElementById('localCount').textContent = currentConfig.localRules?.length || 0;
document.getElementById('localRuleBadge').textContent = currentConfig.localRules?.length || 0;
// 总数
const total = (currentConfig.developers?.length || 0) + (currentConfig.localRules?.length || 0);
document.getElementById('totalBadge').textContent = total;
// 最后更新时间
if (currentConfig.lastUpdate) {
document.getElementById('lastUpdate').textContent = formatDate(new Date(currentConfig.lastUpdate));
}
}
// 更新状态徽章
function updateStatusBadge(enabled) {
const badge = document.getElementById('statusBadge');
if (enabled) {
badge.classList.remove('disabled');
badge.querySelector('.text').textContent = '已启用';
} else {
badge.classList.add('disabled');
badge.querySelector('.text').textContent = '已禁用';
}
}
// 更新子开关状态
function updateSubSwitches(enabled) {
const subSwitches = document.getElementById('subSwitches');
if (subSwitches) {
subSwitches.style.opacity = enabled ? '1' : '0.5';
subSwitches.style.pointerEvents = enabled ? 'auto' : 'none';
}
}
// 设置导航
function setupNavigation() {
const navItems = document.querySelectorAll('.nav-item');
navItems.forEach(item => {
item.addEventListener('click', (e) => {
e.preventDefault();
const sectionId = item.getAttribute('data-section');
// 更新导航状态
navItems.forEach(nav => nav.classList.remove('active'));
item.classList.add('active');
// 切换内容区域
document.querySelectorAll('.section').forEach(section => {
section.classList.remove('active');
});
document.getElementById(sectionId).classList.add('active');
// 如果切换到数据预览,刷新数据
if (sectionId === 'data') {
loadDataTable();
}
});
});
// 处理 URL hash
if (window.location.hash) {
const targetNav = document.querySelector(`.nav-item[data-section="${window.location.hash.slice(1)}"]`);
if (targetNav) {
targetNav.click();
}
}
}
// 设置事件监听
function setupEventListeners() {
// 启用开关(总开关)
document.getElementById('enableToggle').addEventListener('change', async (e) => {
currentConfig.enabled = e.target.checked;
await sendMessage({ action: 'toggle', enabled: e.target.checked });
updateStatusBadge(e.target.checked);
updateSubSwitches(e.target.checked);
showToast(e.target.checked ? '已启用插件' : '已禁用插件', 'success');
});
// 识别用户开关
document.getElementById('featureReplaceToggle').addEventListener('change', async (e) => {
currentConfig.featureReplace = e.target.checked;
await sendMessage({ action: 'saveConfig', config: currentConfig });
showToast(e.target.checked ? '已开启用户识别' : '已关闭用户识别', 'success');
});
// 文字高亮开关
document.getElementById('featureHighlightToggle').addEventListener('change', async (e) => {
currentConfig.featureHighlight = e.target.checked;
await sendMessage({ action: 'saveConfig', config: currentConfig });
showToast(e.target.checked ? '已开启文字高亮' : '已关闭文字高亮', 'success');
});
// 头像高亮开关
document.getElementById('featureAvatarHighlightToggle').addEventListener('change', async (e) => {
currentConfig.featureAvatarHighlight = e.target.checked;
await sendMessage({ action: 'saveConfig', config: currentConfig });
showToast(e.target.checked ? '已开启头像高亮' : '已关闭头像高亮', 'success');
});
// @@ 快速补全开关
document.getElementById('featureMentionToggle').addEventListener('change', async (e) => {
currentConfig.featureMention = e.target.checked;
await sendMessage({ action: 'saveConfig', config: currentConfig });
showToast(e.target.checked ? '已开启 @@ 快速补全' : '已关闭 @@ 快速补全', 'success');
});
// 自动更新开关
document.getElementById('autoUpdate').addEventListener('change', async (e) => {
currentConfig.autoUpdate = e.target.checked;
await sendMessage({ action: 'saveConfig', config: currentConfig });
showToast(e.target.checked ? '已开启每日自动更新' : '已关闭自动更新', 'success');
});
// 调试模式开关
document.getElementById('debugToggle').addEventListener('change', async (e) => {
currentConfig.debug = e.target.checked;
await sendMessage({ action: 'saveConfig', config: currentConfig });
showToast(e.target.checked ? '已开启调试模式,刷新 GitHub 页面后按 F12 查看' : '已关闭调试模式', 'success');
});
// 加载数据按钮
document.getElementById('fetchBtn').addEventListener('click', async () => {
const url = document.getElementById('jsonUrl').value.trim();
if (!url) {
showToast('请输入用户数据源地址', 'error');
return;
}
// 保存 URL
currentConfig.jsonUrl = url;
await sendMessage({ action: 'saveConfig', config: currentConfig });
// 加载数据
const btn = document.getElementById('fetchBtn');
btn.classList.add('loading');
btn.disabled = true;
const result = await sendMessage({ action: 'fetchData', url });
btn.classList.remove('loading');
btn.disabled = false;
if (result.success) {
showToast(`成功加载 ${result.count} 条规则`, 'success');
await loadConfig();
loadDataTable();
} else {
showToast(result.error || '加载失败', 'error');
}
});
// 添加本地规则表单
document.getElementById('addRuleForm').addEventListener('submit', async (e) => {
e.preventDefault();
const rule = {
domain: document.getElementById('ruleDomain').value.trim(),
nick: document.getElementById('ruleNick').value.trim(),
github_name: document.getElementById('ruleGithub').value.trim(),
github_acc: document.getElementById('ruleEmail').value.trim()
};
if (!rule.nick || !rule.github_name) {
showToast('请填写必填项', 'error');
return;
}
await sendMessage({ action: 'addLocalRule', rule });
showToast('规则添加成功', 'success');
// 清空表单
e.target.reset();
// 刷新列表
await loadConfig();
loadLocalRules();
loadDataTable();
});
// 导入本地规则
const importBtn = document.getElementById('importLocalRulesBtn');
const importFileInput = document.getElementById('localRulesImportFile');
if (importBtn && importFileInput) {
importBtn.addEventListener('click', () => {
importFileInput.click();
});
importFileInput.addEventListener('change', async () => {
const file = importFileInput.files && importFileInput.files[0];
// 允许重复选择同一个文件
importFileInput.value = '';
if (!file) return;
try {
const text = await file.text();
const json = JSON.parse(text);
const rules = extractRulesFromImportJson(json);
if (!Array.isArray(rules) || rules.length === 0) {
showToast('导入失败:文件中未找到可用的本地规则', 'error');
return;
}
const result = await sendMessage({ action: 'importLocalRules', rules });
if (!result.success) {
showToast(result.error || '导入失败', 'error');
return;
}
await loadConfig();
loadLocalRules();
loadDataTable();
const imported = result.importedCount ?? 0;
const total = (currentConfig.localRules || []).length;
showToast(`已导入 ${imported} 条本地规则(当前共 ${total} 条)`, 'success');
} catch (err) {
showToast(`导入失败:${err.message || err}`, 'error');
}
});
}
// 导出本地规则
const exportBtn = document.getElementById('exportLocalRulesBtn');
if (exportBtn) {
exportBtn.addEventListener('click', async () => {
const rules = (currentConfig && currentConfig.localRules) ? currentConfig.localRules : [];
if (!rules || rules.length === 0) {
showToast('暂无本地规则可导出', 'error');
return;
}
const exportData = {
localRules: rules.map(r => ({
domain: (r.domain || '').toString(),
nick: (r.nick || '').toString(),
github_name: (r.github_name || '').toString(),
github_acc: (r.github_acc || '').toString()
}))
};
const filename = `github-name-mapper-local-rules-${formatDateForFilename(new Date())}.json`;
downloadJson(exportData, filename);
showToast('已开始导出', 'success');
});
}
// 搜索
document.getElementById('searchInput').addEventListener('input', (e) => {
const keyword = e.target.value.toLowerCase();
filterDataTable(keyword);
});
}
function extractRulesFromImportJson(json) {
if (!json) return [];
if (Array.isArray(json)) return json;
if (Array.isArray(json.localRules)) return json.localRules;
if (Array.isArray(json.rules)) return json.rules;
if (Array.isArray(json.developers)) return json.developers;
if (json.data && Array.isArray(json.data.list)) return json.data.list;
return [];
}
function formatDateForFilename(date) {
const yyyy = date.getFullYear();
const mm = String(date.getMonth() + 1).padStart(2, '0');
const dd = String(date.getDate()).padStart(2, '0');
return `${yyyy}${mm}${dd}`;
}
function downloadJson(data, filename) {
const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
a.remove();
setTimeout(() => URL.revokeObjectURL(url), 0);
}
// 加载本地规则列表
function loadLocalRules() {
const container = document.getElementById('localRulesList');
const rules = currentConfig.localRules || [];
if (rules.length === 0) {
container.innerHTML = `
<div class="empty-state">
<svg width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
<path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/>
<circle cx="9" cy="7" r="4"/>
<path d="M23 21v-2a4 4 0 0 0-3-3.87"/>
<path d="M16 3.13a4 4 0 0 1 0 7.75"/>
</svg>
<p>暂无本地规则</p>
<span>使用上方表单添加自定义映射规则</span>
</div>
`;
return;
}
container.innerHTML = rules.map((rule, index) => `
<div class="rule-item" data-index="${index}">
<div class="rule-info">
<div class="rule-main">
<span class="github">${escapeHtml(rule.github_name)}</span>
<span class="nick">(${escapeHtml(rule.nick)})</span>
</div>
<div class="rule-sub">
域账号: ${escapeHtml(rule.domain || '-')} | 邮箱: ${escapeHtml(rule.github_acc || '-')}
</div>
</div>
<button class="btn btn-danger delete-rule" data-index="${index}">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="3 6 5 6 21 6"/>
<path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/>
</svg>
</button>
</div>
`).join('');
// 绑定删除事件
container.querySelectorAll('.delete-rule').forEach(btn => {
btn.addEventListener('click', async () => {
const index = parseInt(btn.getAttribute('data-index'));
await sendMessage({ action: 'removeLocalRule', index });
showToast('规则已删除', 'success');
await loadConfig();
loadLocalRules();
loadDataTable();
});
});
}
// 加载数据表格
function loadDataTable() {
const tbody = document.getElementById('dataTableBody');
const emptyState = document.getElementById('emptyData');
const developers = currentConfig.developers || [];
const localRules = currentConfig.localRules || [];
const allData = [
...localRules.map(r => ({ ...r, isLocal: true })),
...developers.map(r => ({ ...r, isLocal: false }))
];
if (allData.length === 0) {
tbody.innerHTML = '';
emptyState.style.display = 'flex';
document.querySelector('.table-container').style.display = 'none';
return;
}
emptyState.style.display = 'none';
document.querySelector('.table-container').style.display = 'block';
tbody.innerHTML = allData.map(dev => `
<tr data-searchable="${(dev.domain + dev.nick + dev.github_name + dev.github_acc).toLowerCase()}">
<td>${escapeHtml(dev.domain || '-')}</td>
<td>${escapeHtml(dev.nick || '-')}</td>
<td>${escapeHtml(dev.github_name || '-')}</td>
<td>${escapeHtml(dev.github_acc || '-')}</td>
<td>
<span class="source-badge ${dev.isLocal ? 'local' : 'remote'}">
${dev.isLocal ? '本地' : '远程'}
</span>
</td>
</tr>
`).join('');
}
// 过滤数据表格
function filterDataTable(keyword) {
const rows = document.querySelectorAll('#dataTableBody tr');
rows.forEach(row => {
const searchable = row.getAttribute('data-searchable');
if (keyword === '' || searchable.includes(keyword)) {
row.style.display = '';
} else {
row.style.display = 'none';
}
});
}
// 发送消息
function sendMessage(message) {
return new Promise((resolve) => {
chrome.runtime.sendMessage(message, (response) => {
resolve(response || { success: false });
});
});
}
// 格式化日期
function formatDate(date) {
return date.toLocaleString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit'
});
}
// HTML 转义
function escapeHtml(str) {
if (!str) return '';
const div = document.createElement('div');
div.textContent = str;
return div.innerHTML;
}
// 显示 Toast
function showToast(message, type = 'success') {
const toast = document.getElementById('toast');
toast.querySelector('.toast-message').textContent = message;
toast.className = `toast ${type} show`;
setTimeout(() => {
toast.classList.remove('show');
}, 3000);
}