v0.4: background reads, JS-state login signals, blind sleeps dropped

get_or_open_tab(activate=True) — added, not replaced. Read paths pass
activate=False; post flows keep the foreground for dialogs and file inputs.

check_login moved off DOM signals onto session state: fb c_user, ig/threads
ds_user_id, twitter twid, yt ytcfg.LOGGED_IN. Three platforms were reporting
OUT on a signed-in account because their composer never mounts in a hidden
tab. The fixed sleep(4)/sleep(5) before each check goes with them — every
signal reads present the moment navigate() returns.

Unreadable cookies raise instead of returning False: "cannot tell" is not
"signed out". tiktok untouched and still activated — it reported
visibilityState "visible" from a sibling-activated tab, so it has never been
read from a genuine background.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-09-13 01:58:25 +08:00
co-authored by Claude Opus 5
parent 1ac06c1bbe
commit 5419b08264
7 changed files with 85 additions and 30 deletions
+12 -5
View File
@@ -80,15 +80,22 @@ async def _flow(tab, text, media_path, visibility):
async def check_login(cdp_client):
tab, reused = await cdp_client.get_or_open_tab(S["url"], "facebook.com")
tab, reused = await cdp_client.get_or_open_tab(S["url"], "facebook.com", activate=False)
if reused:
# Dismiss any leftover dialog (e.g. unsaved Create Post from a failed run)
await tab.eval("window.onbeforeunload = null")
await tab.navigate(S["url"])
await asyncio.sleep(4)
if await tab.exists(S["login_marker"]):
return False
return await tab.eval(
"(() => !![...document.querySelectorAll('span')].find(s => s.innerText"
" && s.innerText.toLowerCase().includes('on your mind')))()"
# JS state, not the composer's "on your mind" span: that string is
# localised, A/B'd, and absent until the feed renders, so a signed-in
# account reads as signed out. The c_user cookie is the session itself.
state = await tab.eval(
"(() => { const m = document.cookie.match(/(?:^|;\\s*)c_user=(\\d+)/);"
" return m ? 'in:' + m[1] : (document.cookie ? 'out' : 'unavailable'); })()"
)
if state.startswith("in:"):
return True
if state == "out":
return False
raise CDPError("fb: no cookies readable on facebook.com — cannot tell signed-in from signed-out")
+13 -3
View File
@@ -99,11 +99,21 @@ async def _wait_for_share(tab):
async def check_login(cdp_client):
tab, reused = await cdp_client.get_or_open_tab(S["url"], "instagram.com")
tab, reused = await cdp_client.get_or_open_tab(S["url"], "instagram.com", activate=False)
if reused:
await tab.eval("window.onbeforeunload = null")
await tab.navigate(S["url"])
await asyncio.sleep(5)
if await tab.exists(S["login_marker"]):
return False
return await tab.exists(S["new_post_svg"])
# JS state rather than the new-post SVG. The icon does survive a background
# tab today (measured 2026-09-12), but it is an icon — it moves when the nav
# is redesigned. The session cookie is the session.
state = await tab.eval(
"(() => { const m = document.cookie.match(/(?:^|;\\s*)ds_user_id=(\\d+)/);"
" return m ? 'in:' + m[1] : (document.cookie ? 'out' : 'unavailable'); })()"
)
if state.startswith("in:"):
return True
if state == "out":
return False
raise CDPError("ig: no cookies readable on instagram.com — cannot tell signed-in from signed-out")
+13 -6
View File
@@ -103,18 +103,25 @@ async def _wait_for_post_complete(tab, timeout=30.0):
async def check_login(cdp_client):
tab, reused = await cdp_client.get_or_open_tab(S["url"], "threads.com")
tab, reused = await cdp_client.get_or_open_tab(S["url"], "threads.com", activate=False)
if reused:
await tab.eval("window.onbeforeunload = null")
await tab.navigate(S["url"])
await asyncio.sleep(4)
if await tab.exists(S["login_marker"]):
return False
return await tab.eval(
"!!(document.querySelector('[placeholder=\"What\\'s new?\"]')"
" || [...document.querySelectorAll('span,div')]"
".find(e => e.innerText && e.innerText.trim() === 'What\\'s new?'))"
# JS state, not the "What's new?" composer: that composer never mounts in a
# backgrounded tab, so a signed-in account read as signed out (measured
# 2026-09-12 — DOM signal False while the session cookie was right there).
# Threads runs on the Instagram session, hence ds_user_id.
state = await tab.eval(
"(() => { const m = document.cookie.match(/(?:^|;\\s*)ds_user_id=(\\d+)/);"
" return m ? 'in:' + m[1] : (document.cookie ? 'out' : 'unavailable'); })()"
)
if state.startswith("in:"):
return True
if state == "out":
return False
raise CDPError("threads: no cookies readable on threads.com — cannot tell signed-in from signed-out")
async def get_notifications(cdp_client, count=10):
+7
View File
@@ -120,6 +120,13 @@ async def _wait_for_success(tab, timeout=120.0):
async def check_login(cdp_client):
# The only platform still activated for a login check. TikTok could not be
# backgrounded on 2026-09-12: a sibling tab in the *same* window was
# activated (Browser.getWindowForTarget agreed on windowId) and TikTok still
# reported visibilityState "visible" — own window, or it overrides the
# property to keep video playing. Its DOM signal has never lied, but it has
# never been read from a genuine background either. Three states, not two:
# this stays foreground until there is evidence, not until it looks fine.
tab, reused = await cdp_client.get_or_open_tab(S["url"], "tiktok.com")
if reused:
await tab.eval("window.onbeforeunload = null")
+14 -6
View File
@@ -103,19 +103,27 @@ async def _wait_for_post_complete(tab, timeout=30.0):
async def check_login(cdp_client):
tab, reused = await cdp_client.get_or_open_tab(S["url"], "x.com")
tab, reused = await cdp_client.get_or_open_tab(S["url"], "x.com", activate=False)
if reused:
await tab.eval("window.onbeforeunload = null")
await tab.navigate(S["url"])
await asyncio.sleep(4)
# If we see login markers, not logged in
if await tab.exists(S["login_marker"]):
return False
# If we see the home timeline composer, we're in
return await tab.eval(
"!!(document.querySelector('[data-testid=\"tweetTextarea_0\"]')"
" || document.querySelector('[data-testid=\"SideNav_NewTweet_Button\"]'))"
# JS state, not the composer. The DOM signal does mount in a background tab,
# but only once React has painted — dropping the old sleep(4) made it read
# OUT on a signed-in account the moment five tabs navigated at once
# (measured 2026-09-12). `twid` carries the user id and is set at login,
# same shape as fb's c_user and ig's ds_user_id.
state = await tab.eval(
"(() => { const m = document.cookie.match(/(?:^|;\\s*)twid=([^;]+)/);"
" return m ? 'in:' + m[1] : (document.cookie ? 'out' : 'unavailable'); })()"
)
if state.startswith("in:"):
return True
if state == "out":
return False
raise CDPError("twitter: no cookies readable on x.com — cannot tell signed-in from signed-out")
# ── Scrape tweets from a page ────────────────────────────────────────────────
+12 -5
View File
@@ -93,14 +93,21 @@ async def _wait_for_editor_empty(tab, timeout=30.0):
async def check_login(cdp_client):
tab, reused = await cdp_client.get_or_open_tab(S["url"], "youtube.com")
tab, reused = await cdp_client.get_or_open_tab(S["url"], "youtube.com", activate=False)
if reused:
await tab.eval("window.onbeforeunload = null")
await tab.navigate(S["url"])
await asyncio.sleep(5)
if await tab.exists(S["login_marker"]):
return False
return await tab.eval(
"!!document.querySelector('#avatar-btn, button#avatar-btn,"
" ytd-topbar-menu-button-renderer #avatar-btn')"
# JS state, not the masthead DOM: #avatar-btn needs a fully rendered
# foreground tab and reads absent for a signed-in account otherwise.
# ytcfg is set by the page's own bootstrap and does not depend on render.
state = await tab.eval(
"(() => { try { return String(ytcfg.get('LOGGED_IN')); }"
" catch (e) { return 'unavailable'; } })()"
)
if state == "true":
return True
if state == "false":
return False
raise CDPError("yt: ytcfg.LOGGED_IN unavailable — cannot tell signed-in from signed-out")