diff --git a/README.md b/README.md index b32bfe8..ecbd482 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,10 @@ X GeoBlock is a Manifest V3 browser extension for Chromium and Firefox. While yo - Processes posts already on the page and posts loaded by infinite scrolling. - Performs authenticated About-page requests in the extension service worker. -- Caches account results locally for a configurable period (seven days by default). +- Caches account results locally without expiration until you clear the cache. - Coalesces duplicate lookups and spaces network requests to reduce load on X. - Skips followed accounts. -- Provides a configurable country blacklist, an enable switch, and optional hidden-post placeholders. +- Provides a searchable country and region checklist with flags, an enable switch, and optional hidden-post placeholders. - Stores all settings and lookup results in browser extension storage. No data is sent to third parties. ## Install from source in Chromium @@ -18,7 +18,7 @@ X GeoBlock is a Manifest V3 browser extension for Chromium and Firefox. While yo 2. Enable **Developer mode**. 3. Select **Load unpacked** and choose this repository directory. 4. Sign in to `https://x.com` and browse normally. -5. Open the extension settings to review the default blacklist. +5. Open the extension settings and select the countries you want to block. No country is selected by default. No build step or dependency installation is required. @@ -47,7 +47,7 @@ After editing a file, reload the extension and refresh the X tab. See [Architect X does not publish a stable API for the country shown on the About page. The extension parses X's HTML and embedded page data, so a markup change can temporarily prevent detection. An unknown country leaves the post visible. Requests use your existing X session and are subject to X's rate limits. -The default blacklist is an editable starting point: Belarus, China, Iran, North Korea, and Russia. Inclusion is a local user preference and does not assert anything about individual account holders. +The location picker contains the ISO 3166-1 countries, plus Kosovo, using English CLDR display names. It also includes the broader English region labels exposed by X, such as `West Asia`, alongside country labels such as `United Kingdom`. Inclusion is a local user preference and does not assert anything about individual account holders. ## Permissions diff --git a/background.js b/background.js index 5994628..6e7f57b 100644 --- a/background.js +++ b/background.js @@ -45,14 +45,12 @@ async function lookupUserInternal(username, key) { const stored = await extensionApi.storage.local.get(CACHE_KEY); const cache = stored[CACHE_KEY] || {}; const cached = cache[key]; - const ttl = Math.max(1, Number(settings.cacheTtlHours) || 168) * 60 * 60 * 1000; - if (cached && Date.now() - cached.checkedAt < ttl) { + if (cached) { return decision(cached, settings, true); } const result = await enqueue(() => fetchAbout(username)); cache[key] = { ...result, checkedAt: Date.now() }; - pruneCache(cache); await extensionApi.storage.local.set({ [CACHE_KEY]: cache }); return decision(cache[key], settings, false); } @@ -105,10 +103,3 @@ async function ensureOffscreenDocument() { justification: "Parse the account About page without opening a visible tab." }); } - -function pruneCache(cache) { - const entries = Object.entries(cache); - if (entries.length <= 2000) return; - entries.sort((a, b) => (b[1].checkedAt || 0) - (a[1].checkedAt || 0)); - for (const [key] of entries.slice(1500)) delete cache[key]; -} diff --git a/docs/architecture.md b/docs/architecture.md index 57529b4..56c4427 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -9,7 +9,7 @@ 5. The worker caches the result and compares the country with the configured blacklist. 6. The content script hides the post only when the country matches and the account is not followed. -Failed or ambiguous lookups fail open: the post remains visible. The cache is limited to 2,000 entries and pruned to the 1,500 most recently checked entries when the limit is exceeded. +Failed or ambiguous lookups fail open: the post remains visible. Successful lookups remain cached without expiration. The user can clear the entire cache from the settings page if X changes an account's country or following relationship. ## Browser components diff --git a/docs/privacy.md b/docs/privacy.md index ca216f9..4f48b41 100644 --- a/docs/privacy.md +++ b/docs/privacy.md @@ -2,6 +2,6 @@ X GeoBlock does not collect analytics, transmit browsing data to third parties, or operate a remote server. -The extension reads public account About pages from `x.com` using the X session already present in your browser. It stores usernames, detected countries, following status, and lookup timestamps in local extension storage. Settings use synchronized extension storage when browser sync is enabled. You can clear the lookup cache at any time from the settings page or remove all extension data by uninstalling the extension. +The extension reads public account About pages from `x.com` using the X session already present in your browser. It stores usernames, detected countries, following status, and lookup timestamps in local extension storage without automatic expiration. Settings use synchronized extension storage when browser sync is enabled. You can clear the lookup cache at any time from the settings page or remove all extension data by uninstalling the extension. The extension reads post author links in X timelines only to decide whether to display those posts. It does not read or store post text. diff --git a/icons/icon.png b/icons/icon.png new file mode 100644 index 0000000..6da5909 Binary files /dev/null and b/icons/icon.png differ diff --git a/lib/config.js b/lib/config.js index b9efed9..16d02b2 100644 --- a/lib/config.js +++ b/lib/config.js @@ -3,8 +3,7 @@ const DEFAULT_SETTINGS = Object.freeze({ enabled: true, - blacklist: ["Belarus", "China", "Iran", "North Korea", "Russia"], - cacheTtlHours: 168, + blacklist: [], showPlaceholder: false }); @@ -16,8 +15,21 @@ "dprk": "north korea", "republic of korea": "south korea", "united states of america": "united states", + "u.s.": "united states", "usa": "united states", - "uk": "united kingdom" + "uk": "united kingdom", + "viet nam": "vietnam", + "bolivia, plurinational state of": "bolivia", + "venezuela, bolivarian republic of": "venezuela", + "tanzania, united republic of": "tanzania", + "syrian arab republic": "syria", + "lao people's democratic republic": "laos", + "moldova, republic of": "moldova", + "brunei darussalam": "brunei", + "czechia": "czech republic", + "turkiye": "turkey", + "cote d’ivoire": "ivory coast", + "cote d'ivoire": "ivory coast" }); function normalizeCountry(value) { diff --git a/lib/countries.js b/lib/countries.js new file mode 100644 index 0000000..583641a --- /dev/null +++ b/lib/countries.js @@ -0,0 +1,51 @@ +(function (root) { + "use strict"; + + // ISO 3166-1 alpha-2, plus XK for Kosovo (commonly exposed by CLDR services). + const REGION_CODES = (` +AD AE AF AG AI AL AM AO AQ AR AS AT AU AW AX AZ +BA BB BD BE BF BG BH BI BJ BL BM BN BO BQ BR BS BT BV BW BY BZ +CA CC CD CF CG CH CI CK CL CM CN CO CR CU CV CW CX CY CZ +DE DJ DK DM DO DZ EC EE EG EH ER ES ET FI FJ FK FM FO FR +GA GB GD GE GF GG GH GI GL GM GN GP GQ GR GS GT GU GW GY +HK HM HN HR HT HU ID IE IL IM IN IO IQ IR IS IT JE JM JO JP +KE KG KH KI KM KN KP KR KW KY KZ LA LB LC LI LK LR LS LT LU LV LY +MA MC MD ME MF MG MH MK ML MM MN MO MP MQ MR MS MT MU MV MW MX MY MZ +NA NC NE NF NG NI NL NO NP NR NU NZ OM PA PE PF PG PH PK PL PM PN PR PS PT PW PY +QA RE RO RS RU RW SA SB SC SD SE SG SH SI SJ SK SL SM SN SO SR SS ST SV SX SY SZ +TC TD TF TG TH TJ TK TL TM TN TO TR TT TV TW TZ UA UG UM US UY UZ +VA VC VE VG VI VN VU WF WS XK YE YT ZA ZM ZW +`).trim().split(/\s+/); + + const displayNames = new Intl.DisplayNames(["en"], { type: "region" }); + + // X sometimes exposes a broad region instead of an ISO country. Keep these + // labels verbatim because they are compared with the About-page value. + const X_REGIONS = Object.freeze([ + ["Caribbean", "🌎"], ["Central Africa", "🌍"], ["Central America", "🌎"], + ["Central Asia", "🌏"], ["East Africa", "🌍"], ["East Asia", "🌏"], + ["Eastern Europe", "🌍"], ["North Africa", "🌍"], ["North America", "🌎"], + ["Northern Europe", "🌍"], ["Oceania", "🌏"], ["South America", "🌎"], + ["South Asia", "🌏"], ["Southeast Asia", "🌏"], ["Southern Africa", "🌍"], + ["Southern Europe", "🌍"], ["West Africa", "🌍"], ["West Asia", "🌏"], + ["Western Europe", "🌍"] + ].map(([name, flag]) => Object.freeze({ code: `X-${name}`, name, flag, type: "region" }))); + + function countryName(code) { + return displayNames.of(code) || code; + } + + function countryFlag(code) { + if (!/^[A-Z]{2}$/.test(code)) return "🌐"; + return String.fromCodePoint(...code.split("").map((letter) => 127397 + letter.charCodeAt(0))); + } + + const COUNTRIES = Object.freeze(REGION_CODES + .map((code) => Object.freeze({ code, name: countryName(code), flag: countryFlag(code), type: "country" })) + .sort((a, b) => a.name.localeCompare(b.name, "en"))); + const LOCATIONS = Object.freeze([...X_REGIONS, ...COUNTRIES]); + + const api = { REGION_CODES, X_REGIONS, COUNTRIES, LOCATIONS, countryName, countryFlag }; + root.XGeoCountries = api; + if (typeof module !== "undefined") module.exports = api; +})(typeof globalThis !== "undefined" ? globalThis : this); diff --git a/manifest.json b/manifest.json index 43292d9..3bdc4af 100644 --- a/manifest.json +++ b/manifest.json @@ -3,6 +3,12 @@ "name": "X GeoBlock", "version": "1.0.1", "description": "Hide posts from X accounts based in countries you choose.", + "icons": { + "16": "icons/icon.png", + "32": "icons/icon.png", + "48": "icons/icon.png", + "128": "icons/icon.png" + }, "permissions": [ "storage", "offscreen" @@ -30,7 +36,11 @@ "options_page": "options/options.html", "action": { "default_title": "X GeoBlock", - "default_popup": "popup/popup.html" + "default_popup": "popup/popup.html", + "default_icon": { + "16": "icons/icon.png", + "32": "icons/icon.png" + } }, "web_accessible_resources": [], "minimum_chrome_version": "109" diff --git a/options/options.css b/options/options.css index 3edceaf..ccbe0df 100644 --- a/options/options.css +++ b/options/options.css @@ -1,9 +1,12 @@ :root { color-scheme: light dark; font: 16px/1.5 system-ui, sans-serif; } body { margin: 0; background: #0f1419; color: #e7e9ea; } main { max-width: 650px; margin: 48px auto; padding: 0 24px; } -h1 { font-size: 32px; margin-bottom: 4px; }.lead,.hint { color: #8b98a5; }.hint { margin: -8px 0 8px; font-size: 14px; } -label:not(.check) { display: block; margin: 24px 0 8px; font-weight: 700; } -textarea,input[type=number] { box-sizing: border-box; width: 100%; border: 1px solid #536471; border-radius: 8px; background: #000; color: inherit; padding: 12px; font: inherit; } +.brand { display:flex; align-items:center; gap:16px; }.brand img { width:64px; height:64px; object-fit:contain; flex:0 0 auto; }h1 { font-size: 32px; margin:0 0 4px; }.lead,.hint { color: #8b98a5; }.lead { margin:0; }.hint { margin: 6px 0 8px; font-size: 14px; } +fieldset { border: 0; padding: 0; margin: 28px 0 0; } legend { font-size: 18px; font-weight: 700; padding: 0; } +input[type=search] { box-sizing: border-box; flex: 1; min-width: 180px; border: 1px solid #536471; border-radius: 999px; background: #000; color: inherit; padding: 10px 14px; font: inherit; } .check { display: block; margin: 22px 0; }.check input { margin-right: 8px; }.actions { display: flex; gap: 12px; margin-top: 28px; } button { border: 0; border-radius: 999px; background: #1d9bf0; color: #fff; padding: 10px 18px; font-weight: 700; cursor: pointer; } button.secondary { background: transparent; border: 1px solid #536471; }#status { min-height: 24px; color: #00ba7c; } +.country-tools { display: flex; gap: 12px; align-items: center; margin: 14px 0 8px; }.link-button { background: transparent; color: #1d9bf0; padding: 8px; white-space: nowrap; } +.selection-count { color: #8b98a5; margin: 8px 0; font-size: 14px; }.country-list { display: grid; grid-template-columns: repeat(auto-fill,minmax(190px,1fr)); gap: 6px; max-height: 430px; overflow: auto; border: 1px solid #2f3336; border-radius: 12px; padding: 10px; background: #000; } +.country-option { display: flex; align-items: center; gap: 9px; padding: 7px 8px; margin: 0; border-radius: 8px; cursor: pointer; }.country-option:hover { background: #16181c; }.country-option[hidden] { display: none; }.country-flag { font-size: 20px; line-height: 1; }.country-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } diff --git a/options/options.html b/options/options.html index 96ffb2c..f518b9a 100644 --- a/options/options.html +++ b/options/options.html @@ -1,15 +1,18 @@ X GeoBlock settings
-

X GeoBlock

Hide posts based on the country shown in an account's About page.

+

X GeoBlock

Hide posts based on the country shown in an account's About page.

- -

Enter one country per line, exactly as X displays it.

- - +
+ Blocked countries and regions +

X may show a country such as β€œUnited Kingdom” or a broader region such as β€œWest Asia”. Nothing is blocked by default.

+
+

+
+

-
+ diff --git a/options/options.js b/options/options.js index 18ac54e..89f2893 100644 --- a/options/options.js +++ b/options/options.js @@ -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."; diff --git a/package.json b/package.json index 79755d4..100e31e 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "description": "Browser extension that hides X posts based on account country.", "scripts": { "test": "node --test", - "check": "node --check background.js && node --check content.js && node --check lib/config.js && node --check lib/parser.js && node --check offscreen/offscreen.js && node --check options/options.js && node --check popup/popup.js && node --check scripts/build.mjs && node --check scripts/bump-version.mjs && node --test", + "check": "node --check background.js && node --check content.js && node --check lib/config.js && node --check lib/countries.js && node --check lib/parser.js && node --check offscreen/offscreen.js && node --check options/options.js && node --check popup/popup.js && node --check scripts/build.mjs && node --check scripts/bump-version.mjs && node --test", "build": "node scripts/build.mjs" }, "engines": { diff --git a/popup/popup.css b/popup/popup.css index 2145069..a7b788f 100644 --- a/popup/popup.css +++ b/popup/popup.css @@ -1 +1 @@ -:root { color-scheme: light dark; font: 14px/1.4 system-ui,sans-serif; } body { margin: 0; min-width: 280px; background: #0f1419; color: #e7e9ea; } main { padding: 18px; } h1 { font-size: 20px; margin: 0 0 16px; } label { display:block; } p { color:#8b98a5; } button { border:0; border-radius:999px; padding:8px 14px; background:#1d9bf0; color:white; font-weight:700; cursor:pointer; } +:root { color-scheme: light dark; font: 14px/1.4 system-ui,sans-serif; } body { margin: 0; min-width: 280px; background: #0f1419; color: #e7e9ea; } main { padding: 18px; } .brand { display:flex; align-items:center; gap:10px; margin-bottom:16px; }.brand img { width:36px; height:36px; object-fit:contain; }h1 { font-size: 20px; margin: 0; } label { display:block; } p { color:#8b98a5; } button { border:0; border-radius:999px; padding:8px 14px; background:#1d9bf0; color:white; font-weight:700; cursor:pointer; } diff --git a/popup/popup.html b/popup/popup.html index c6fb14b..49da54b 100644 --- a/popup/popup.html +++ b/popup/popup.html @@ -1,2 +1,2 @@ X GeoBlock -

X GeoBlock

+

X GeoBlock

diff --git a/scripts/build.mjs b/scripts/build.mjs index 86682c7..dad5a6b 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -8,7 +8,7 @@ const dist = join(root, "dist"); const baseManifest = JSON.parse(readFileSync(join(root, "manifest.json"), "utf8")); const version = baseManifest.version; const sharedFiles = [ - "background.js", "content.js", "content.css", "lib", "options", "popup" + "background.js", "content.js", "content.css", "icons", "lib", "options", "popup" ]; rmSync(dist, { recursive: true, force: true }); diff --git a/tests/parser.test.js b/tests/parser.test.js index 7f5e954..9f22fa8 100644 --- a/tests/parser.test.js +++ b/tests/parser.test.js @@ -2,6 +2,7 @@ const test = require("node:test"); const assert = require("node:assert/strict"); const { findUserRecord } = require("../lib/parser.js"); const { normalizeCountry, isBlacklisted } = require("../lib/config.js"); +const { COUNTRIES, LOCATIONS } = require("../lib/countries.js"); test("finds following state in nested X user data", () => { const source = { data: { user: { result: { legacy: { screen_name: "Alice", following: true, location: "Berlin" } } } } }; @@ -15,3 +16,10 @@ test("normalizes country aliases", () => { assert.equal(isBlacklisted("Russian Federation", ["Russia"]), true); assert.equal(isBlacklisted("Germany", ["Russia"]), false); }); +test("offers the full country checklist with unique values", () => { + assert.equal(COUNTRIES.length, 250); + assert.equal(new Set(COUNTRIES.map((country) => country.code)).size, 250); + assert.ok(COUNTRIES.some((country) => country.name === "Germany" && country.flag === "πŸ‡©πŸ‡ͺ")); + assert.ok(LOCATIONS.some((location) => location.name === "West Asia")); + assert.ok(LOCATIONS.some((location) => location.name === "United Kingdom" && location.flag === "πŸ‡¬πŸ‡§")); +});