-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathoptions.mjs
61 lines (54 loc) · 2.05 KB
/
options.mjs
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
import provinces from "./provinces.mjs";
document.addEventListener("DOMContentLoaded", function () {
const provinceList = document.getElementById("provinceList");
const checkboxes = [];
let selectedCount = 0;
// Clear all data in chrome.storage.local
chrome.storage.local.clear();
// Event listener for checkbox changes
function handleCheckboxChange() {
const isChecked = this.checked;
const code = this.getAttribute("name");
if (isChecked) {
selectedCount++;
} else {
selectedCount--;
// Remove the province from storage if unchecked
chrome.storage.local.remove(code, function () {
console.log(`Removed ${code} from storage.`);
});
}
if (selectedCount >= 2) {
// Disable other checkboxes if two provinces are selected
checkboxes.forEach(function (checkbox) {
if (!checkbox.checked) {
checkbox.disabled = true;
}
});
} else {
// Enable all checkboxes if less than two provinces are selected
checkboxes.forEach(function (checkbox) {
checkbox.disabled = false;
});
}
// Update storage with the selected province status
chrome.storage.local.set({ [code]: isChecked }, function () {
console.log(`Province ${code} status saved.`);
});
}
// Render checkboxes for provinces
provinces.forEach(function (province) {
const item = document.createElement("div");
item.classList.add("flex", "items-center", "py-1");
item.innerHTML = `
<label class="flex items-center cursor-pointer">
<input type="checkbox" name="${province.code}" class="form-checkbox mr-2 checked:bg-gray-900 checked:border-transparent checked:ring-2 checked:ring-offset-2 checked:ring-blue-500 h-6 w-6 ">
<span>${province.name}</span>
</label>
`;
provinceList.appendChild(item);
const checkbox = item.querySelector(".form-checkbox");
checkboxes.push(checkbox);
checkbox.addEventListener("change", handleCheckboxChange);
});
});