-
Notifications
You must be signed in to change notification settings - Fork 2
/
popup.js
134 lines (115 loc) · 4.24 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
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
document.addEventListener("DOMContentLoaded", async function () {
var enableCheckbox = document.getElementById("enableCheckbox");
var actionRadios = document.querySelectorAll('input[name="actionRadio"]');
var intervalInput = document.getElementById("intervalInput");
var saveButton = document.getElementById("saveButton");
var statusMessage = document.getElementById("statusMessage");
var sitesTableBody = document.getElementById("sitesTableBody");
try {
// Request initial settings from the background script
var appData = await getSettings();
if (typeof appData === "undefined") {
throw new Error("Settings are undefined");
}
if (appData.settings)
chrome.tabs.query(
{ active: true, currentWindow: true },
async function (tabs) {
var tab = tabs[0];
var url = new URL(tab.url);
var domain = url.hostname;
var currentSiteData = appData.settings.filter(
(x) => x.siteHost == domain
);
if (currentSiteData.length > 0) {
var currentTabSettings = currentSiteData[0];
enableCheckbox.checked = currentTabSettings.enabled || false;
intervalInput.value = currentTabSettings.interval || 60;
Array.from(actionRadios).forEach(function (radio) {
radio.checked =
radio.value === (currentTabSettings.action || "Keep Active");
});
}
}
);
populateSitesTable(appData.settings || []);
} catch (error) {
// Handle error retrieving settings
console.error(error);
showMessage(`Error retrieving settings: ${error.message}`, "error");
}
saveButton.addEventListener("click", async function () {
var sitename = "";
chrome.tabs.query(
{ active: true, currentWindow: true },
async function (tabs) {
var tab = tabs[0];
var url = new URL(tab.url);
var domain = url.hostname;
var settings = {
siteHost: domain,
enabled: enableCheckbox.checked,
action: document.querySelector('input[name="actionRadio"]:checked')
.value,
interval: parseInt(intervalInput.value),
timerId: 0,
};
try {
const response = await saveSettings(settings);
if (!response || typeof response.success === "undefined") {
throw new Error("Error saving settings");
}
showMessage(response.message, "success");
setTimeout(function () {
clearMessage();
}, 3000);
const updatedSettings = await getSettings();
populateSitesTable(updatedSettings.settings || []);
} catch (error) {
console.error(error);
showMessage(`Error saving settings: ${error.message}`, "error");
}
}
);
});
function showMessage(message, className) {
statusMessage.textContent = message;
statusMessage.className = className;
}
function clearMessage() {
statusMessage.textContent = "";
statusMessage.className = "";
}
function populateSitesTable(data) {
sitesTableBody.innerHTML = "";
data.forEach(function (site) {
var row = document.createElement("tr");
var siteCell = document.createElement("td");
siteCell.textContent = site.siteHost;
row.appendChild(siteCell);
var actionCell = document.createElement("td");
actionCell.textContent = site.action;
row.appendChild(actionCell);
var intervalCell = document.createElement("td");
intervalCell.textContent = site.interval;
row.appendChild(intervalCell);
var timerIdCell = document.createElement("td");
timerIdCell.textContent = site.timerId;
row.appendChild(timerIdCell);
var enableIdCell = document.createElement("td");
enableIdCell.textContent = site.enabled?"Active":"In Active"
row.appendChild(enableIdCell);
sitesTableBody.appendChild(row);
});
}
async function getSettings() {
return new Promise((resolve) => {
chrome.runtime.sendMessage({ action: "getSettings" }, resolve);
});
}
async function saveSettings(settings) {
return new Promise((resolve) => {
chrome.runtime.sendMessage({ action: "saveSettings", settings }, resolve);
});
}
});