-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
72 lines (60 loc) · 1.93 KB
/
Copy pathpopup.js
File metadata and controls
72 lines (60 loc) · 1.93 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
import { Whitelist } from './whitelist.js';
const whitelist = new Whitelist();
function getSiteParameter(url) {
const match = url.match(/[?&]site=([^&]+)/);
return match ? match[1] : null;
}
async function initializePopup() {
await whitelist.load();
renderWhitelist();
setupEventListeners();
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible') {
document.getElementById('newSite').focus();
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
const currentTab = tabs[0];
if (currentTab) {
document.getElementById('newSite').value = getSiteParameter(currentTab.url);
}
});
}
});
}
function setupEventListeners() {
document.getElementById('addSite').addEventListener('click', addNewSite);
document.getElementById('newSite').addEventListener('keypress', (e) => {
if (e.key === 'Enter') addNewSite();
});
}
async function addNewSite() {
const input = document.getElementById('newSite');
const site = input.value.trim();
if (site) {
const added = await whitelist.add(site);
if (added) {
input.value = '';
renderWhitelist();
chrome.action.setBadgeText({ text: '' });
}
}
}
function renderWhitelist() {
const container = document.getElementById('whitelistContainer');
container.innerHTML = '';
whitelist.sites.forEach(site => {
const item = document.createElement('div');
item.className = 'site-item';
const siteText = document.createElement('span');
siteText.textContent = site;
const removeButton = document.createElement('button');
removeButton.textContent = '删除';
removeButton.onclick = async () => {
await whitelist.remove(site);
renderWhitelist();
};
item.appendChild(siteText);
item.appendChild(removeButton);
container.appendChild(item);
});
}
document.addEventListener('DOMContentLoaded', initializePopup);