-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
56 lines (52 loc) · 2.19 KB
/
popup.js
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
document.addEventListener('DOMContentLoaded', () => {
const monitorSwitch = document.getElementById('monitorSwitch');
const domainListDiv = document.getElementById('domainList');
const domainListBody = document.getElementById('domainListBody');
const loadingDiv = document.getElementById('loading');
// Get the current state from storage
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const domain = new URL(tabs[0].url).hostname;
chrome.storage.local.get(['domainSettings'], (result) => {
monitorSwitch.checked = result.domainSettings && result.domainSettings[domain];
});
});
monitorSwitch.addEventListener('change', () => {
const monitoring = monitorSwitch.checked;
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.runtime.sendMessage({ message: 'toggleMonitoring', monitoring: monitoring }, () => {
if (monitoring) {
domainListBody.innerHTML = '';
domainListDiv.style.display = 'none';
loadingDiv.style.display = 'block';
} else {
domainListDiv.style.display = 'none';
}
});
});
});
// Listen for messages from the background script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message === 'domainList') {
loadingDiv.style.display = 'none';
domainListDiv.style.display = 'block';
domainListBody.innerHTML = '';
request.domains.forEach((item, index) => {
const row = document.createElement('tr');
const numberCell = document.createElement('td');
numberCell.textContent = index + 1;
const domainCell = document.createElement('td');
domainCell.textContent = item.domain;
const ipCell = document.createElement('td');
ipCell.textContent = item.ip;
row.appendChild(numberCell);
row.appendChild(domainCell);
row.appendChild(ipCell);
domainListBody.appendChild(row);
});
}
});
// Request the current domain list when the popup is opened
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.runtime.sendMessage({ message: 'getDomainList', tabId: tabs[0].id });
});
});