-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
150 lines (140 loc) · 4.25 KB
/
popup.js
File metadata and controls
150 lines (140 loc) · 4.25 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
// popup.js: 解析 CSV 并存储 username->info 的映射到 chrome.storage.local
function parseCSV(text) {
// 简单的 CSV 解析,支持用双引号包裹的字段和换行
const rows = [];
let cur = '';
let row = [];
let inQuotes = false;
for (let i = 0; i < text.length; i++) {
const ch = text[i];
const next = text[i+1];
if (ch === '"') {
if (inQuotes && next === '"') { // 双引号转义
cur += '"';
i++; // skip next
} else {
inQuotes = !inQuotes;
}
} else if (ch === ',' && !inQuotes) {
row.push(cur);
cur = '';
} else if ((ch === '\n' || ch === '\r') && !inQuotes) {
// 处理 CRLF 或 LF
if (cur !== '' || row.length > 0) {
row.push(cur);
rows.push(row);
row = [];
cur = '';
}
// skip possible \r\n by skipping if next is other newline
} else {
cur += ch;
}
}
if (cur !== '' || row.length > 0) {
row.push(cur);
rows.push(row);
}
return rows;
}
function rowsToMap(rows) {
if (!rows || rows.length === 0) return {};
const header = rows[0].map(h => h.trim());
const map = {};
for (let i = 1; i < rows.length; i++) {
const r = rows[i];
if (r.every(c => c === '')) continue; // skip empty
const obj = {};
for (let j = 0; j < header.length; j++) {
const key = header[j].toLowerCase();
const val = r[j] ? r[j].trim() : '';
obj[key] = val;
}
const username = obj['username'] || obj['login'] || obj['user'];
if (!username) continue;
map[username] = {
name: obj['name'] || '',
company: obj['company'] || '',
employee_id: obj['employee_id'] || obj['id'] || obj['employeeid'] || ''
};
}
return map;
}
const fileInput = document.getElementById('csvFile');
const saveBtn = document.getElementById('saveBtn');
const clearBtn = document.getElementById('clearBtn');
const preview = document.getElementById('preview');
const searchInput = document.getElementById('searchInput');
const searchResult = document.getElementById('searchResult');
let currentMap = {};
fileInput.addEventListener('change', async (e) => {
const file = e.target.files[0];
if (!file) return;
const text = await file.text();
const rows = parseCSV(text);
const map = rowsToMap(rows);
currentMap = map;
renderPreview(map);
});
saveBtn.addEventListener('click', () => {
if (!currentMap || Object.keys(currentMap).length === 0) {
alert('未解析到数据,请先上传 CSV。');
return;
}
chrome.storage.local.set({ userMap: currentMap }, () => {
alert('已保存 ' + Object.keys(currentMap).length + ' 条记录到扩展存储。');
});
});
clearBtn.addEventListener('click', () => {
if (!confirm('清除所有扩展内保存的数据?')) return;
chrome.storage.local.remove('userMap', () => {
currentMap = {};
preview.innerText = '';
alert('已清除。');
});
});
function renderPreview(map) {
preview.innerHTML = '';
const keys = Object.keys(map).slice(0, 20);
if (keys.length === 0) {
preview.innerText = '无数据';
return;
}
keys.forEach(k => {
const p = document.createElement('div');
p.className = 'preview-row';
const item = map[k];
p.innerHTML = `<strong>${k}</strong> — ${item.name || '-'} — ${item.company || '-'} — ${item.employee_id || '-'} `;
preview.appendChild(p);
});
}
searchInput.addEventListener('input', () => {
const q = searchInput.value.trim();
if (!q) { searchResult.innerText = ''; return; }
// first check currentMap, then storage
let found = currentMap[q];
if (found) {
renderSearch(found, q);
} else {
chrome.storage.local.get('userMap', data => {
const map = data.userMap || {};
if (map[q]) {
renderSearch(map[q], q);
} else {
searchResult.innerText = '未找到 ' + q;
}
});
}
});
function renderSearch(item, username) {
searchResult.innerHTML = `<div><strong>${username}</strong></div>
<div>姓名:${item.name || '-'}</div>
<div>公司:${item.company || '-'}</div>
<div>工号:${item.employee_id || '-'}</div>`;
}
// 初始化:加载存储里的数据到预览(只展示前 20)
chrome.storage.local.get('userMap', data => {
const map = data.userMap || {};
currentMap = map;
renderPreview(map);
});