diff --git a/background.js b/background.js index 9b8deca..9e5a489 100644 --- a/background.js +++ b/background.js @@ -6,8 +6,10 @@ const CACHE_KEY = "userCache"; const LOCAL_CACHE_TTL_MS = 30 * 24 * 60 * 60 * 1000; const activeRequests = new Map(); const pendingFrameLookups = new Map(); -let queueTail = Promise.resolve(); +const requestQueue = []; +let runningRequests = 0; let lastRequestAt = 0; +const MAX_CONCURRENT_REQUESTS = 3; extensionApi.runtime.onInstalled.addListener(async () => { const current = await extensionApi.storage.sync.get(XGeoConfig.DEFAULT_SETTINGS); @@ -131,14 +133,28 @@ function decision(record, settings, cached) { } function enqueue(task) { - const run = queueTail.then(async () => { - const delay = Math.max(0, 350 - (Date.now() - lastRequestAt)); - if (delay) await new Promise((resolve) => setTimeout(resolve, delay)); - lastRequestAt = Date.now(); - return task(); + return new Promise((resolve, reject) => { + requestQueue.push({ task, resolve, reject }); + drainQueue(); }); - queueTail = run.catch(() => {}); - return run; +} + +function drainQueue() { + while (runningRequests < MAX_CONCURRENT_REQUESTS && requestQueue.length) { + const entry = requestQueue.shift(); + runningRequests += 1; + const scheduledAt = Math.max(Date.now(), lastRequestAt + 350); + lastRequestAt = scheduledAt; + const delay = scheduledAt - Date.now(); + setTimeout(async () => { + try { entry.resolve(await entry.task()); } + catch (error) { entry.reject(error); } + finally { + runningRequests -= 1; + drainQueue(); + } + }, delay); + } } async function fetchAbout(username, log = () => {}, tabId = null) { diff --git a/content.js b/content.js index eb2b380..fe6f99e 100644 --- a/content.js +++ b/content.js @@ -110,6 +110,8 @@ async function processArticle(article) { } function getFollowingHint(article, username) { + const profileHint = getProfileFollowingHint(username); + if (profileHint !== null) return Promise.resolve(profileHint); return new Promise((resolve) => { let settled = false; const finish = () => { @@ -126,6 +128,19 @@ function getFollowingHint(article, username) { }); } +function getProfileFollowingHint(username) { + const profile = location.pathname.match(/^\/([A-Za-z0-9_]{1,15})(?:\/|$)/)?.[1]; + if (!profile || profile.toLowerCase() !== username.toLowerCase()) return null; + const column = document.querySelector('[data-testid="primaryColumn"]') || document; + if (column.querySelector('[data-testid$="-unfollow"]')) return true; + if (column.querySelector('[data-testid$="-follow"]')) return false; + const buttons = Array.from(column.querySelectorAll("button, [role=button]")); + const labels = buttons.slice(0, 30).map((button) => `${button.textContent || ""} ${button.getAttribute("aria-label") || ""}`.trim().toLowerCase()); + if (labels.some((label) => /^(following|siguiendo|abonné|abonniert|seguindo|seguiti)(\s|$)/.test(label))) return true; + if (labels.some((label) => /^(follow|seguir|s’abonner|folgen|seguir)(\s|$)/.test(label))) return false; + return null; +} + function getTweetIdentity(article) { const links = article.querySelectorAll('a[href*="/status/"]'); for (const link of links) { @@ -176,7 +191,15 @@ function inspectAboutFrame(frame, username) { const timer = setInterval(() => { if (!frame.isConnected) return clearInterval(timer); try { - const result = XGeoParser.parseAboutDocument(frame.contentDocument, username); + const frameDocument = frame.contentDocument; + if (!frameDocument?.documentElement) { + if (Date.now() - startedAt > 24000) { + clearInterval(timer); + debug("Hidden About frame document remained unavailable", { username }); + } + return; + } + const result = XGeoParser.parseAboutDocument(frameDocument, username); if (result.country) { clearInterval(timer); debug("Hidden About frame parsed", { username, country: result.country, following: result.following }); diff --git a/manifest.json b/manifest.json index e3a225e..ec6743c 100644 --- a/manifest.json +++ b/manifest.json @@ -37,6 +37,7 @@ "matches": [ "https://x.com/*" ], + "all_frames": true, "js": [ "lib/parser.js", "content.js"