-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
295 lines (256 loc) · 8.41 KB
/
popup.js
File metadata and controls
295 lines (256 loc) · 8.41 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
/**
* WebNotes - 弹出页面脚本
*/
let allAnnotations = [];
let currentTab = 'all';
let currentUrl = '';
// 初始化
document.addEventListener('DOMContentLoaded', async () => {
// 获取当前标签页URL
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
currentUrl = tab?.url || '';
// 加载标注
await loadAnnotations();
// 标签切换
document.querySelectorAll('.tab').forEach(tabBtn => {
tabBtn.addEventListener('click', () => {
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
tabBtn.classList.add('active');
currentTab = tabBtn.dataset.tab;
renderAnnotations();
});
});
// 导出按钮
document.getElementById('exportBtn').addEventListener('click', exportAnnotations);
// 清空按钮
document.getElementById('clearBtn').addEventListener('click', clearAnnotations);
});
/**
* 加载标注
*/
async function loadAnnotations() {
allAnnotations = await chrome.runtime.sendMessage({ action: 'getAllAnnotations' });
updateStats();
renderAnnotations();
}
/**
* 更新统计
*/
function updateStats() {
const total = allAnnotations.length;
const withNote = allAnnotations.filter(a => a.note).length;
const sites = new Set(allAnnotations.map(a => new URL(a.url).hostname)).size;
document.getElementById('totalCount').textContent = total;
document.getElementById('noteCount').textContent = withNote;
document.getElementById('siteCount').textContent = sites;
}
/**
* 渲染标注列表
*/
function renderAnnotations() {
const container = document.getElementById('annotationList');
let annotations = allAnnotations;
if (currentTab === 'current' && currentUrl) {
annotations = allAnnotations.filter(a => a.url === currentUrl);
}
// 按时间倒序
annotations = [...annotations].sort((a, b) =>
new Date(b.createdAt) - new Date(a.createdAt)
);
if (annotations.length === 0) {
container.innerHTML = `
<div class="empty">
<div class="empty-icon">📝</div>
<div>${currentTab === 'current' ? '当前页面暂无标注' : '暂无标注'}</div>
<div style="font-size:12px;margin-top:8px;">选中文本后右键或按 Ctrl+Shift+H 高亮</div>
</div>
`;
return;
}
container.innerHTML = annotations.map(a => `
<div class="annotation-item" data-id="${a.id}" data-url="${encodeURIComponent(a.url)}" data-text="${encodeURIComponent(a.text)}">
<div class="annotation-header">
<div class="annotation-site" title="${escapeHtml(a.title || a.url)}">
${getHostname(a.url)}
</div>
<div class="annotation-actions">
<button class="btn-open" title="打开页面">🔗</button>
<button class="btn-copy" title="复制文本">📋</button>
<button class="btn-delete" title="删除">🗑️</button>
</div>
</div>
<div class="annotation-text" style="background:${getHighlightColor(a.color)}">
${escapeHtml(a.text)}
</div>
${a.note ? `<div class="annotation-note">${escapeHtml(a.note)}</div>` : ''}
<div class="annotation-time">${formatTime(a.createdAt)}</div>
</div>
`).join('');
// 事件委托处理按钮点击
container.querySelectorAll('.btn-open').forEach(btn => {
btn.addEventListener('click', (e) => {
const item = e.target.closest('.annotation-item');
const url = decodeURIComponent(item.dataset.url);
chrome.tabs.create({ url });
});
});
container.querySelectorAll('.btn-copy').forEach(btn => {
btn.addEventListener('click', (e) => {
const item = e.target.closest('.annotation-item');
const text = decodeURIComponent(item.dataset.text);
navigator.clipboard.writeText(text).then(() => {
showToast('已复制');
});
});
});
container.querySelectorAll('.btn-delete').forEach(btn => {
btn.addEventListener('click', async (e) => {
if (!confirm('确定删除此标注?')) return;
const item = e.target.closest('.annotation-item');
const id = item.dataset.id;
await chrome.runtime.sendMessage({ action: 'deleteAnnotation', id });
allAnnotations = allAnnotations.filter(a => a.id !== id);
updateStats();
renderAnnotations();
showToast('已删除');
});
});
}
/**
* 获取主机名
*/
function getHostname(url) {
try {
return new URL(url).hostname;
} catch {
return url;
}
}
/**
* 获取高亮颜色
*/
function getHighlightColor(color) {
const colors = {
yellow: '#fff9c4',
green: '#c8e6c9',
blue: '#bbdefb',
pink: '#f8bbd9',
orange: '#ffe0b2'
};
return colors[color] || colors.yellow;
}
/**
* 格式化时间
*/
function formatTime(isoString) {
const date = new Date(isoString);
const now = new Date();
const diff = now - date;
if (diff < 60000) return '刚刚';
if (diff < 3600000) return `${Math.floor(diff / 60000)}分钟前`;
if (diff < 86400000) return `${Math.floor(diff / 3600000)}小时前`;
if (diff < 604800000) return `${Math.floor(diff / 86400000)}天前`;
return date.toLocaleDateString('zh-CN');
}
/**
* 打开页面
*/
function openPage(url) {
chrome.tabs.create({ url });
}
/**
* 复制文本
*/
function copyText(text) {
navigator.clipboard.writeText(text).then(() => {
showToast('已复制');
});
}
/**
* 删除标注
*/
async function deleteAnnotation(id) {
if (!confirm('确定删除此标注?')) return;
await chrome.runtime.sendMessage({ action: 'deleteAnnotation', id });
allAnnotations = allAnnotations.filter(a => a.id !== id);
updateStats();
renderAnnotations();
showToast('已删除');
}
/**
* 导出标注
*/
function exportAnnotations() {
if (allAnnotations.length === 0) {
showToast('暂无标注可导出');
return;
}
// 生成Markdown
let md = '# WebNotes 导出\n\n';
md += `导出时间: ${new Date().toLocaleString('zh-CN')}\n`;
md += `共 ${allAnnotations.length} 条标注\n\n---\n\n`;
// 按网站分组
const grouped = {};
allAnnotations.forEach(a => {
const host = getHostname(a.url);
if (!grouped[host]) grouped[host] = [];
grouped[host].push(a);
});
Object.entries(grouped).forEach(([host, items]) => {
md += `## ${host}\n\n`;
items.forEach(a => {
md += `> ${a.text}\n\n`;
if (a.note) md += `**笔记:** ${a.note}\n\n`;
md += `_${formatTime(a.createdAt)} - [原文链接](${a.url})_\n\n---\n\n`;
});
});
// 下载文件
const blob = new Blob([md], { type: 'text/markdown' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `webnotes-${new Date().toISOString().slice(0,10)}.md`;
a.click();
URL.revokeObjectURL(url);
showToast('已导出');
}
/**
* 清空标注
*/
async function clearAnnotations() {
if (allAnnotations.length === 0) {
showToast('暂无标注');
return;
}
if (!confirm(`确定清空所有 ${allAnnotations.length} 条标注?此操作不可恢复!`)) return;
await chrome.storage.local.set({ annotations: [] });
allAnnotations = [];
updateStats();
renderAnnotations();
showToast('已清空');
}
/**
* 显示提示
*/
function showToast(message) {
// 简单提示
const toast = document.createElement('div');
toast.style.cssText = 'position:fixed;bottom:60px;left:50%;transform:translateX(-50%);background:#333;color:#fff;padding:8px 16px;border-radius:4px;font-size:13px;z-index:9999;';
toast.textContent = message;
document.body.appendChild(toast);
setTimeout(() => toast.remove(), 1500);
}
/**
* HTML转义
*/
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
/**
* JS字符串转义
*/
function escapeJs(text) {
return text.replace(/'/g, "\\'").replace(/\n/g, '\\n');
}