-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
127 lines (112 loc) · 4.44 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
document.getElementById("addDomain").addEventListener("click", function () {
/**
* Gets the value of the domain input field.
* @returns {string} The value of the domain input field.
*/
const domain = document.getElementById("domainInput").value;
const path = document.getElementById("pathInput").value;
chrome.storage.local.get("domainRegistry", (data) => {
const registry = data.domainRegistry;
registry[domain] = path;
chrome.storage.local.set({ domainRegistry: registry });
});
});
document.getElementById("debugMode").addEventListener("change", function () {
/**
* Gets the value of the debug mode checkbox element.
* @returns {boolean} The value of the debug mode checkbox element.
*/
const isDebugMode = document.getElementById("debugMode").checked;
chrome.storage.local.set({ debugMode: isDebugMode });
});
// Get the current state of debugMode from Chrome storage
chrome.storage.local.get("debugMode", (data) => {
/**
* Gets the debug checkbox element from the DOM.
* @type {HTMLInputElement}
*/
const debugCheckbox = document.getElementById("debugMode");
debugCheckbox.checked = data.debugMode || false;
});
// Load the registry into the UI on popup open
chrome.storage.local.get("domainRegistry", (data) => {
const registry = data.domainRegistry || {};
const registryList = document.getElementById("registryList");
const registryTable = document.getElementById("registryTable");
registryList.innerHTML = ""; // Clear existing entries
for (const domain in registry) {
const tableRow = document.createElement("tr");
const domainCell1 = document.createElement("td");
const domainCell2 = document.createElement("td");
const form1 = document.createElement("form");
const form2 = document.createElement("form");
const input1 = document.createElement("input");
const input2 = document.createElement("input");
const deleteButton = document.createElement("button");
const submitButton = document.createElement("button");
const cancelButton = document.createElement("button");
input1.type = "text";
input1.value = domain;
input2.type = "text";
input2.value = registry[domain];
deleteButton.textContent = "Delete";
submitButton.textContent = "Submit";
submitButton.style.display = "none";
cancelButton.textContent = "Cancel";
cancelButton.style.display = "none";
form1.appendChild(input1);
form2.appendChild(input2);
domainCell1.appendChild(form1);
domainCell2.appendChild(form2);
input1.addEventListener("input", function () {
submitButton.style.display = "inline-block";
cancelButton.style.display = "inline-block";
});
input2.addEventListener("input", function () {
submitButton.style.display = "inline-block";
cancelButton.style.display = "inline-block";
});
deleteButton.addEventListener("click", function () {
delete registry[domain];
chrome.storage.local.set({ domainRegistry: registry }, function () {
// Update UI
tableRow.remove();
});
});
cancelButton.addEventListener("click", function () {
input1.value = domain;
input2.value = registry[domain];
input1.disabled = true;
input2.disabled = true;
submitButton.style.display = "inline-block";
cancelButton.style.display = "none";
});
submitButton.addEventListener("click", function () {
for (const tableRow of registryTable.rows) {
const domain = tableRow.cells[0].querySelector("input").value;
const newPath = tableRow.cells[1].querySelector("input").value;
if (domain && newPath) {
registry[domain] = newPath;
}
}
chrome.storage.local.set({ domainRegistry: registry }, function () {
// Update UI
submitButton.style.display = "inline-block";
cancelButton.style.display = "none";
});
});
const actionCell = document.createElement("td");
actionCell.style.width = "fit-content";
actionCell.appendChild(deleteButton);
actionCell.appendChild(submitButton);
actionCell.appendChild(cancelButton);
tableRow.appendChild(domainCell1);
tableRow.appendChild(domainCell2);
tableRow.appendChild(actionCell);
registryTable.appendChild(tableRow);
}
});
// Event listener for the "View Registry" button
document.getElementById("viewRegistry").addEventListener("click", function () {
chrome.tabs.create({ url: "popup.html" }); // Open the popup.html in a new tab
});