mirror of
https://github.com/Dasemu/X-GeoBlock.git
synced 2026-11-26 23:36:11 +00:00
fix: complete rendered profile lookups
This commit is contained in:
1 parent
c31dfa14db
commit
276c0fc8b1
3 files changed
+49
-9
No files matched your search
+24
-8
@@ -6,8 +6,10 @@ const CACHE_KEY = "userCache";
|
|||||||
const LOCAL_CACHE_TTL_MS = 30 * 24 * 60 * 60 * 1000;
|
const LOCAL_CACHE_TTL_MS = 30 * 24 * 60 * 60 * 1000;
|
||||||
const activeRequests = new Map();
|
const activeRequests = new Map();
|
||||||
const pendingFrameLookups = new Map();
|
const pendingFrameLookups = new Map();
|
||||||
let queueTail = Promise.resolve();
|
const requestQueue = [];
|
||||||
|
let runningRequests = 0;
|
||||||
let lastRequestAt = 0;
|
let lastRequestAt = 0;
|
||||||
|
const MAX_CONCURRENT_REQUESTS = 3;
|
||||||
|
|
||||||
extensionApi.runtime.onInstalled.addListener(async () => {
|
extensionApi.runtime.onInstalled.addListener(async () => {
|
||||||
const current = await extensionApi.storage.sync.get(XGeoConfig.DEFAULT_SETTINGS);
|
const current = await extensionApi.storage.sync.get(XGeoConfig.DEFAULT_SETTINGS);
|
||||||
@@ -131,14 +133,28 @@ function decision(record, settings, cached) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function enqueue(task) {
|
function enqueue(task) {
|
||||||
const run = queueTail.then(async () => {
|
return new Promise((resolve, reject) => {
|
||||||
const delay = Math.max(0, 350 - (Date.now() - lastRequestAt));
|
requestQueue.push({ task, resolve, reject });
|
||||||
if (delay) await new Promise((resolve) => setTimeout(resolve, delay));
|
drainQueue();
|
||||||
lastRequestAt = Date.now();
|
|
||||||
return task();
|
|
||||||
});
|
});
|
||||||
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) {
|
async function fetchAbout(username, log = () => {}, tabId = null) {
|
||||||
|
|||||||
+24
-1
@@ -110,6 +110,8 @@ async function processArticle(article) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getFollowingHint(article, username) {
|
function getFollowingHint(article, username) {
|
||||||
|
const profileHint = getProfileFollowingHint(username);
|
||||||
|
if (profileHint !== null) return Promise.resolve(profileHint);
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
let settled = false;
|
let settled = false;
|
||||||
const finish = () => {
|
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) {
|
function getTweetIdentity(article) {
|
||||||
const links = article.querySelectorAll('a[href*="/status/"]');
|
const links = article.querySelectorAll('a[href*="/status/"]');
|
||||||
for (const link of links) {
|
for (const link of links) {
|
||||||
@@ -176,7 +191,15 @@ function inspectAboutFrame(frame, username) {
|
|||||||
const timer = setInterval(() => {
|
const timer = setInterval(() => {
|
||||||
if (!frame.isConnected) return clearInterval(timer);
|
if (!frame.isConnected) return clearInterval(timer);
|
||||||
try {
|
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) {
|
if (result.country) {
|
||||||
clearInterval(timer);
|
clearInterval(timer);
|
||||||
debug("Hidden About frame parsed", { username, country: result.country, following: result.following });
|
debug("Hidden About frame parsed", { username, country: result.country, following: result.following });
|
||||||
|
|||||||
@@ -37,6 +37,7 @@
|
|||||||
"matches": [
|
"matches": [
|
||||||
"https://x.com/*"
|
"https://x.com/*"
|
||||||
],
|
],
|
||||||
|
"all_frames": true,
|
||||||
"js": [
|
"js": [
|
||||||
"lib/parser.js",
|
"lib/parser.js",
|
||||||
"content.js"
|
"content.js"
|
||||||
|
|||||||
Reference in new issue
Block a user