-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
303 lines (265 loc) · 10.6 KB
/
popup.js
File metadata and controls
303 lines (265 loc) · 10.6 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
// popup.js
/**
* 存储获取到的所有链接
*/
let allLinks = [];
/**
* 初始化函数,设置事件监听器
*/
function init() {
const getLinksBtn = document.getElementById('getLinksBtn');
const closeLinksBtn = document.getElementById('closeLinksBtn');
const refreshLinksBtn = document.getElementById('refreshLinksBtn');
const statusDiv = document.getElementById('status');
const sameDomainCheckbox = document.getElementById('sameDomainCheckbox');
const filterInput = document.getElementById('filterInput');
const excludeInput = document.getElementById('excludeInput');
const delayInput = document.getElementById('delayInput');
const limitInput = document.getElementById('limitInput');
const toggleLinksBtn = document.getElementById('toggleLinksBtn');
const linksList = document.getElementById('linksList');
const currentDomainSpan = document.getElementById('currentDomain');
// 显示当前域名
async function displayCurrentDomain() {
try {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (tab) {
const currentDomain = new URL(tab.url).hostname;
currentDomainSpan.textContent = currentDomain;
} else {
currentDomainSpan.textContent = '无法获取域名';
}
} catch (error) {
console.error('获取域名失败:', error);
currentDomainSpan.textContent = '获取域名失败';
}
}
// 页面加载时显示当前域名
displayCurrentDomain();
// 页面加载完成后自动获取链接数量
async function displayLinkCount() {
try {
// 获取当前活动标签页
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!tab) {
statusDiv.textContent = '无法获取当前标签页';
return;
}
statusDiv.textContent = '正在获取链接数量...';
// 向content.js发送消息,请求获取链接
const links = await chrome.tabs.sendMessage(tab.id, { action: 'getLinks' });
// 存储获取到的链接
allLinks = links || [];
// 过滤链接
const filteredLinks = filterLinks(allLinks, tab.url);
if (allLinks.length > 0) {
statusDiv.innerHTML = `当前页面有 <b> ${allLinks.length} </b> 个链接,过滤后有 <b> ${filteredLinks.length} </b> 个`;
// 初始显示链接列表
displayLinksList(filteredLinks);
} else {
statusDiv.textContent = '当前页面未找到链接';
linksList.innerHTML = '<p style="margin: 5px 0; color: #666;">没有匹配的链接</p>';
}
} catch (error) {
console.error('获取链接失败:', error);
statusDiv.textContent = '获取链接数量失败';
}
}
/**
* 根据条件过滤链接
* @param {string[]} links - 要过滤的链接数组
* @param {string} currentUrl - 当前页面URL
* @returns {string[]} 过滤后的链接数组
*/
function filterLinks(links, currentUrl) {
if (!links || links.length === 0) return [];
const currentDomain = new URL(currentUrl).hostname;
const filterText = filterInput.value.toLowerCase();
const excludeKeywords = excludeInput.value.toLowerCase().split(',').map(keyword => keyword.trim()).filter(keyword => keyword !== '');
const sameDomainOnly = sameDomainCheckbox.checked;
return links.filter(link => {
const linkLower = link.toLowerCase();
// 检查是否为本域名
const linkDomain = new URL(link).hostname;
const isSameDomain = linkDomain === currentDomain;
// 如果只允许本域名且当前链接不是本域名,则过滤掉
if (sameDomainOnly && !isSameDomain) {
return false;
}
// 检查是否匹配筛选文本
if (filterText !== '' && !linkLower.includes(filterText)) {
return false;
}
// 检查是否包含排除关键字
if (excludeKeywords.length > 0 && excludeKeywords.some(keyword => linkLower.includes(keyword))) {
return false;
}
return true;
});
}
// 重新获取链接按钮点击事件
refreshLinksBtn.addEventListener('click', async () => {
await displayCurrentDomain();
displayLinkCount();
});
// 筛选条件变化事件
sameDomainCheckbox.addEventListener('change', async () => {
if (allLinks.length > 0) {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (tab) {
const filteredLinks = filterLinks(allLinks, tab.url);
statusDiv.innerHTML = `当前页面有 <b> ${allLinks.length} </b> 个链接,过滤后有 <b> ${filteredLinks.length} </b> 个`;
displayLinksList(filteredLinks);
}
}
});
filterInput.addEventListener('input', async () => {
if (allLinks.length > 0) {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (tab) {
const filteredLinks = filterLinks(allLinks, tab.url);
statusDiv.innerHTML = `当前页面有 <b> ${allLinks.length} </b> 个链接,过滤后有 <b> ${filteredLinks.length} </b> 个`;
displayLinksList(filteredLinks);
}
}
});
// 排除关键字输入变化事件
excludeInput.addEventListener('input', async () => {
if (allLinks.length > 0) {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (tab) {
const filteredLinks = filterLinks(allLinks, tab.url);
statusDiv.innerHTML = `当前页面有 <b> ${allLinks.length} </b> 个链接,过滤后有 <b> ${filteredLinks.length} </b> 个`;
displayLinksList(filteredLinks);
}
}
});
// 展开/收起链接列表按钮点击事件
toggleLinksBtn.addEventListener('click', toggleLinksList);
/**
* 显示匹配的链接列表
* @param {string[]} links - 要显示的链接数组
*/
function displayLinksList(links) {
if (!links || links.length === 0) {
linksList.innerHTML = '<p style="margin: 5px 0; color: #666;">没有匹配的链接</p>';
return;
}
const linksHTML = links.map((link, index) =>
`<div style="margin: 3px 0; padding: 3px; border-bottom: 1px solid #eee; word-break: break-all;">
<span style="font-weight: bold; margin-right: 5px;">${index + 1}.</span>
<a href="${link}" target="_blank" style="color: #1a0dab; text-decoration: none;">${link}</a>
</div>`
).join('');
linksList.innerHTML = linksHTML;
}
/**
* 切换链接列表的展开/收起状态
*/
function toggleLinksList() {
if (linksList.style.maxHeight === '0px' || linksList.style.maxHeight === '') {
// 展开列表
linksList.style.maxHeight = '200px'; // 设置一个合适的最大高度
toggleLinksBtn.innerHTML = '🔽 隐藏匹配的链接';
} else {
// 收起列表
linksList.style.maxHeight = '0';
toggleLinksBtn.innerHTML = '▶️ 显示匹配的链接';
}
}
// 调用函数显示链接数量
displayLinkCount();
// 批量关闭按钮点击事件
closeLinksBtn.addEventListener('click', async () => {
try {
// 发送消息到background.js,关闭之前打开的标签页
const response = await chrome.runtime.sendMessage({ action: 'closeOpenedTabs' });
if (response && response.success) {
statusDiv.textContent = '已关闭批量打开的标签页';
} else {
statusDiv.textContent = '关闭标签页失败';
}
} catch (error) {
console.error('关闭标签页失败:', error);
statusDiv.textContent = '关闭标签页失败,请重试';
}
});
// 批量打开按钮点击事件
getLinksBtn.addEventListener('click', async () => {
try {
// 获取当前活动标签页
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!tab) {
statusDiv.textContent = '无法获取当前标签页';
return;
}
if (allLinks.length === 0) {
statusDiv.textContent = '正在获取链接...';
// 如果没有链接,先获取链接
allLinks = await chrome.tabs.sendMessage(tab.id, { action: 'getLinks' }) || [];
}
// 使用已过滤的链接
const filteredLinks = filterLinks(allLinks, tab.url);
if (filteredLinks && filteredLinks.length > 0) {
// 获取打开方式
const windowMode = document.querySelector('input[name="openMethod"]:checked').value;
// 获取延迟时间
const delayTime = parseInt(delayInput.value) || 20;
// 获取数量限制
const limit = parseInt(limitInput.value) || 50;
// 检查是否超过用户设置的限制
if (filteredLinks.length > limit) {
// 只打开前limit个链接
const limitedLinks = filteredLinks.slice(0, limit);
// 检查是否超过50个链接
if (limitedLinks > 50) {
// 显示提示对话框
if (confirm(`您要打开 ${filteredLinks.length} 个链接,这可能会导致浏览器卡顿。\n\n选择打开方式:\n- 确定:打开全部链接\n- 取消:取消操作`)) {
// 打开全部链接
statusDiv.textContent = `正在打开 ${filteredLinks.length} 个链接...`;
sendOpenLinksMessage(filteredLinks, windowMode, delayTime);
} else {
// 取消操作,什么也不做
statusDiv.textContent = '操作已取消';
}
}else{
statusDiv.textContent = `正在打开 ${limitedLinks.length} 个链接...`;
sendOpenLinksMessage(limitedLinks, windowMode, delayTime);
}
} else {
// 直接打开链接
statusDiv.textContent = `正在打开 ${filteredLinks.length} 个链接...`;
sendOpenLinksMessage(filteredLinks, windowMode, delayTime);
}
} else {
statusDiv.textContent = '没有符合条件的链接';
}
} catch (error) {
console.error('获取链接失败:', error);
statusDiv.textContent = '获取链接失败,请重试';
}
});
/**
* 发送打开链接的消息到background.js
* @param {string[]} links - 要打开的链接数组
* @param {string} windowMode - 窗口模式('new'或'current')
* @param {number} delay - 延迟时间
*/
function sendOpenLinksMessage(links, windowMode, delay) {
if (windowMode === 'new') {
chrome.runtime.sendMessage({
action: 'openLinksInNewWindow',
links: links,
delay: delay
});
} else {
chrome.runtime.sendMessage({
action: 'openLinksInCurrentWindow',
links: links,
delay: delay
});
}
}
}
// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', init);