fix: resolve account country from rendered about page

This commit is contained in:
Darío Sevilla committed 2026-10-15 07:42:06 +00:00
1 parent 3811ae9cfc
commit d6c31bdf95
5 files changed
+102 -17

No files matched your search

+72 -13
View File
@@ -4,6 +4,7 @@ const extensionApi = globalThis.browser || chrome;
const CACHE_KEY = "userCache";
const activeRequests = new Map();
const pendingTabLookups = new Map();
let queueTail = Promise.resolve();
let lastRequestAt = 0;
@@ -12,7 +13,7 @@ extensionApi.runtime.onInstalled.addListener(async () => {
await extensionApi.storage.sync.set(current);
});
extensionApi.runtime.onMessage.addListener((message, _sender, sendResponse) => {
extensionApi.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message?.type === "lookup-user") {
lookupUser(message.username).then(sendResponse).catch((error) => {
sendResponse({ ok: false, error: error.message });
@@ -23,9 +24,22 @@ extensionApi.runtime.onMessage.addListener((message, _sender, sendResponse) => {
extensionApi.storage.local.remove(CACHE_KEY).then(() => sendResponse({ ok: true }));
return true;
}
if (message?.type === "about-page-result" && Number.isInteger(sender?.tab?.id)) {
finishTabLookup(sender.tab.id, message);
sendResponse({ ok: true });
return false;
}
return false;
});
extensionApi.tabs.onRemoved.addListener((tabId) => {
const pending = pendingTabLookups.get(tabId);
if (!pending) return;
clearTimeout(pending.timer);
pendingTabLookups.delete(tabId);
pending.reject(new Error("The background About tab closed before X loaded the account data"));
});
function validUsername(username) {
return typeof username === "string" && /^[A-Za-z0-9_]{1,15}$/.test(username);
}
@@ -45,14 +59,20 @@ async function lookupUserInternal(username, key) {
const stored = await extensionApi.storage.local.get(CACHE_KEY);
const cache = stored[CACHE_KEY] || {};
const cached = cache[key];
if (cached) {
if (cached && (cached.country || cached.following === true)) {
return decision(cached, settings, true);
}
const result = await enqueue(() => fetchAbout(username));
cache[key] = { ...result, checkedAt: Date.now() };
await extensionApi.storage.local.set({ [CACHE_KEY]: cache });
return decision(cache[key], settings, false);
const record = { ...result, checkedAt: Date.now() };
if (record.country || record.following === true) {
cache[key] = record;
await extensionApi.storage.local.set({ [CACHE_KEY]: cache });
} else if (cached) {
delete cache[key];
await extensionApi.storage.local.set({ [CACHE_KEY]: cache });
}
return decision(record, settings, false);
}
function decision(record, settings, cached) {
@@ -77,14 +97,23 @@ function enqueue(task) {
}
async function fetchAbout(username) {
const response = await fetch(`https://x.com/${encodeURIComponent(username)}/about`, {
credentials: "include",
headers: { "Accept-Language": "en-US,en;q=0.9" },
redirect: "follow"
});
if (!response.ok) throw new Error(`X returned HTTP ${response.status}`);
const html = await response.text();
if (/\/i\/flow\/login/i.test(response.url)) throw new Error("X authentication is required");
try {
const response = await fetch(`https://x.com/${encodeURIComponent(username)}/about`, {
credentials: "include",
headers: { "Accept-Language": "en-US,en;q=0.9" },
redirect: "follow"
});
if (response.ok && !/\/i\/flow\/login/i.test(response.url)) {
const parsed = await parseFetchedHtml(await response.text(), username);
if (parsed?.country) return parsed;
}
} catch (_) {
// X frequently serves an incomplete SPA shell to extension fetches.
}
return lookupViaBackgroundTab(username);
}
async function parseFetchedHtml(html, username) {
if (typeof DOMParser !== "undefined" && typeof XGeoParser !== "undefined") {
const document = new DOMParser().parseFromString(html, "text/html");
return XGeoParser.parseAboutDocument(document, username);
@@ -93,6 +122,36 @@ async function fetchAbout(username) {
return extensionApi.runtime.sendMessage({ type: "parse-about-html", html, username });
}
async function lookupViaBackgroundTab(username) {
const tab = await extensionApi.tabs.create({
url: `https://x.com/${encodeURIComponent(username)}/about`,
active: false
});
if (!Number.isInteger(tab.id)) throw new Error("The browser did not create the background About tab");
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
pendingTabLookups.delete(tab.id);
extensionApi.tabs.remove(tab.id).catch(() => {});
reject(new Error(`Timed out while loading @${username}'s About page`));
}, 25000);
pendingTabLookups.set(tab.id, { username: username.toLowerCase(), resolve, reject, timer });
});
}
function finishTabLookup(tabId, message) {
const pending = pendingTabLookups.get(tabId);
if (!pending || String(message.username).toLowerCase() !== pending.username) return;
clearTimeout(pending.timer);
pendingTabLookups.delete(tabId);
pending.resolve({
country: message.country || null,
following: message.following === true,
profileLocation: message.profileLocation || null
});
extensionApi.tabs.remove(tabId).catch(() => {});
}
async function ensureOffscreenDocument() {
const url = extensionApi.runtime.getURL("offscreen/offscreen.html");
const clients = await self.clients.matchAll();