mirror of
https://github.com/Dasemu/X-GeoBlock.git
synced 2026-11-26 23:36:11 +00:00
feat: add location picker and persistent cache
This commit is contained in:
1 parent
fcea6cf7b7
commit
3811ae9cfc
16 files changed
+163
-39
No files matched your search
+52
-6
@@ -2,25 +2,71 @@
|
||||
const extensionApi = globalThis.browser || chrome;
|
||||
const form = document.querySelector("#settings");
|
||||
const enabled = document.querySelector("#enabled");
|
||||
const blacklist = document.querySelector("#blacklist");
|
||||
const ttl = document.querySelector("#ttl");
|
||||
const placeholder = document.querySelector("#placeholder");
|
||||
const status = document.querySelector("#status");
|
||||
const countryList = document.querySelector("#country-list");
|
||||
const countrySearch = document.querySelector("#country-search");
|
||||
const selectionCount = document.querySelector("#selection-count");
|
||||
|
||||
renderCountries();
|
||||
|
||||
load();
|
||||
async function load() {
|
||||
const value = await extensionApi.storage.sync.get(XGeoConfig.DEFAULT_SETTINGS);
|
||||
enabled.checked = value.enabled;
|
||||
blacklist.value = value.blacklist.join("\n");
|
||||
ttl.value = value.cacheTtlHours;
|
||||
placeholder.checked = value.showPlaceholder;
|
||||
const selected = new Set(value.blacklist.map(XGeoConfig.normalizeCountry));
|
||||
countryList.querySelectorAll("input").forEach((input) => {
|
||||
input.checked = selected.has(XGeoConfig.normalizeCountry(input.value));
|
||||
});
|
||||
updateSelectionCount();
|
||||
}
|
||||
|
||||
function renderCountries() {
|
||||
const fragment = document.createDocumentFragment();
|
||||
for (const country of XGeoCountries.LOCATIONS) {
|
||||
const label = document.createElement("label");
|
||||
label.className = "country-option";
|
||||
label.dataset.search = `${country.name} ${country.code}`.toLowerCase();
|
||||
const input = document.createElement("input");
|
||||
input.type = "checkbox";
|
||||
input.value = country.name;
|
||||
const flag = document.createElement("span");
|
||||
flag.className = "country-flag";
|
||||
flag.textContent = country.flag;
|
||||
flag.setAttribute("aria-hidden", "true");
|
||||
const name = document.createElement("span");
|
||||
name.className = "country-name";
|
||||
name.textContent = country.name;
|
||||
label.append(input, flag, name);
|
||||
fragment.append(label);
|
||||
}
|
||||
countryList.append(fragment);
|
||||
countryList.addEventListener("change", updateSelectionCount);
|
||||
}
|
||||
|
||||
function updateSelectionCount() {
|
||||
const count = countryList.querySelectorAll("input:checked").length;
|
||||
selectionCount.textContent = `${count} ${count === 1 ? "location" : "locations"} selected`;
|
||||
}
|
||||
|
||||
countrySearch.addEventListener("input", () => {
|
||||
const query = countrySearch.value.trim().toLowerCase();
|
||||
countryList.querySelectorAll(".country-option").forEach((option) => {
|
||||
option.hidden = Boolean(query) && !option.dataset.search.includes(query);
|
||||
});
|
||||
});
|
||||
|
||||
document.querySelector("#clear-selection").addEventListener("click", () => {
|
||||
countryList.querySelectorAll("input:checked").forEach((input) => input.checked = false);
|
||||
updateSelectionCount();
|
||||
});
|
||||
|
||||
form.addEventListener("submit", async (event) => {
|
||||
event.preventDefault();
|
||||
await extensionApi.storage.sync.set({
|
||||
enabled: enabled.checked,
|
||||
blacklist: Array.from(new Set(blacklist.value.split(/\r?\n/).map((value) => value.trim()).filter(Boolean))),
|
||||
cacheTtlHours: Math.max(1, Number(ttl.value) || 168),
|
||||
blacklist: Array.from(countryList.querySelectorAll("input:checked"), (input) => input.value),
|
||||
showPlaceholder: placeholder.checked
|
||||
});
|
||||
status.textContent = "Settings saved.";
|
||||
|
||||
Reference in new issue
Block a user