From ea6e8f5a4d5a4fa817c32e236eee268839fd66d7 Mon Sep 17 00:00:00 2001 From: tshmieldev Date: Wed, 11 Nov 2026 16:58:05 +0100 Subject: [PATCH] fix: read X's rebuilt client, which has no data-testid at all X began serving a rebuilt web client in November 2026: no #react-root, no data-testid anywhere. A timeline is `[id^="urt:"] > ul` of absolutely positioned, virtualised `li` rows; each post is an `article` inside a `[role="link"]` wrapper carrying the permalink in `data-href`, the author in `[data-post-avatar]`, the text in `div[dir="auto"]`, media behind `[data-engagement-action="media"]` and a quoted post inside an `[data-slot="embedded-tweet-frame"]` frame. Sharp found no posts and did nothing, on desktop and phone alike. Every reader in extract.ts now tries the old labels first and the new shapes second, so one build serves whichever client a reader is given: - posts, rows, author handle and name, post id, text, photos and video posters, the repost header, the row above (by data-index, since X recycles row nodes and document order no longer means anything), and thread order on a post page; - the slot is laid out for the rebuilt client's column articles, where a full-width flex basis would have been read as height; - a fully hidden post leaves its row at a pixel so X's virtualiser keeps measuring it and no blank gap opens; - the More button is recognised, so the menu adapter can find a trigger. Captured rows from the live page are kept as a fixture and tested. Not yet on the rebuilt client, pending its markup: Sharp's items in the post menu, the thread control, and choosing "Not interested" on the reader's behalf. --- .prettierignore | 1 + src/x/controller.ts | 6 +- src/x/extract.ts | 132 +++++++++++++++++++++++++++--- src/x/post-menu-dom.ts | 3 +- src/x/style.css | 10 +++ src/x/thread-control.tsx | 14 ++-- src/x/view.tsx | 10 ++- tests/fixtures/x-modern-home.html | 5 ++ tests/x-modern.test.ts | 93 +++++++++++++++++++++ 9 files changed, 251 insertions(+), 23 deletions(-) create mode 100644 tests/fixtures/x-modern-home.html create mode 100644 tests/x-modern.test.ts diff --git a/.prettierignore b/.prettierignore index 691721b..3e2dcd2 100644 --- a/.prettierignore +++ b/.prettierignore @@ -11,3 +11,4 @@ bun.lock /popup.html .worktree-backup /firefox +tests/fixtures/ diff --git a/src/x/controller.ts b/src/x/controller.ts index 5880940..abda3a8 100644 --- a/src/x/controller.ts +++ b/src/x/controller.ts @@ -51,7 +51,7 @@ const NAV_SETTLE = 1500; const RESTORE_WINDOW = 3000; const isHome = (path: string) => /^\/home\/?$/.test(path); type Anchor = { id: string; top: number }; -const cellSelector = '[data-testid="cellInnerDiv"]'; +const cellSelector = extract.cellSelector; /** X's acknowledgement in a timeline cell: no post, and an Undo to take it back. */ function isFeedbackCard(cell: HTMLElement): boolean { if (cell.querySelector(extract.articleSelector)) return false; @@ -366,6 +366,10 @@ export class TimelineController { scan() { if (this.stopped || !this.settings || !this.rules) return; this.scannedAt = Date.now(); + // Which client X served, for the stylesheet's few client-specific rules. + const client = extract.modernClient() ? 'modern' : 'legacy'; + if (document.documentElement.dataset.aitfClient !== client) + document.documentElement.dataset.aitfClient = client; this.threadControl.update(this.settings); if (this.path !== location.pathname) { const returning = isHome(location.pathname) && this.path !== '' && !isHome(this.path); diff --git a/src/x/extract.ts b/src/x/extract.ts index c266538..d5c224f 100644 --- a/src/x/extract.ts +++ b/src/x/extract.ts @@ -1,9 +1,36 @@ import { threadId } from '../common/post'; import type { Post } from '../common/post'; -export const articleSelector = 'article[data-testid="tweet"]'; +// X serves two clients. The old one (React Native Web, `#react-root`) labels +// everything with data-testid. The one rolled out in November 2026 has no test +// ids at all: a timeline is `[id^="urt:"] > ul` of absolutely positioned, +// virtualised `li` rows, each post an `article` inside a `[role="link"]` +// wrapper that carries the permalink in `data-href`, the author in +// `[data-post-avatar]`, the text in `div[dir="auto"]`, media behind +// `[data-engagement-action="media"]`, and a quoted post in an +// `[data-slot="embedded-tweet-frame"]` frame. Every reader below tries the old +// labels first and the new shapes second, so one build serves whichever client +// a reader is given. + +/** A quoted post's frame. Nothing inside it belongs to the post around it. */ +const frameSelector = '[data-slot="embedded-tweet-frame"]'; +const modernArticle = `[id^="urt:"] > ul > li article:not(${frameSelector} article)`; +export const articleSelector = `article[data-testid="tweet"], ${modernArticle}`; +/** The timeline row a post sits in. X recycles and swaps these. */ +export const cellSelector = '[data-testid="cellInnerDiv"], [id^="urt:"] > ul > li'; const authorSelector = '[data-testid="User-Name"]'; const handlePattern = /^\/([A-Za-z0-9_]{1,15})$/; +const statusPattern = /\/status\/(\d+)(?:\/|$)/; + +/** Whether the page is the rebuilt client. False until the page has a body. */ +export const modernClient = () => Boolean(document.body) && !document.getElementById('react-root'); + +export const cellOf = (article: Element) => article.closest(cellSelector); + +const inFrame = (node: Element, scope: Element) => { + const frame = node.closest(frameSelector); + return frame !== null && scope.contains(frame) && frame !== scope; +}; export function author(scope: Element) { const container = scope.matches(authorSelector) ? scope : scope.querySelector(authorSelector); @@ -12,22 +39,65 @@ export function author(scope: Element) { if (handle) return { handle, name: container?.querySelector('a span')?.textContent?.trim() || handle }; } + // The rebuilt client: the avatar links to the author, and the name block + // repeats the link with the display name in it. + const avatar = [...scope.querySelectorAll('[data-post-avatar] a[href^="/"]')].find( + (link) => !inFrame(link, scope), + ); + const handle = handlePattern.exec(avatar?.getAttribute('href') ?? '')?.[1]; + if (handle) { + const named = [...scope.querySelectorAll(`.x-middot-group a[href="/${handle}"]`)] + .filter((link) => !inFrame(link, scope)) + .map((link) => link.textContent?.trim() ?? '') + .find((label) => label && !label.startsWith('@')); + return { handle, name: named || handle }; + } return { handle: '', name: '' }; } +/** The post's own words, then any quoted post's, the way the reader sees them. */ export function text(article: Element): string { - // textContent is stable even when our stylesheet hides the post. - return [...article.querySelectorAll('[data-testid="tweetText"]')] + const labelled = [...article.querySelectorAll('[data-testid="tweetText"]')]; + const nodes = labelled.length ? labelled : modernText(article); + return nodes .map((node) => node.textContent?.trim() ?? '') .filter(Boolean) .join('\n—\n'); } +function modernText(article: Element): Element[] { + const candidates = [...article.querySelectorAll('div[dir="auto"]')].filter( + (node) => !node.closest('[role="group"], .x-middot-group, a'), + ); + // Counts, names and card captions also read right-to-left when they must; + // the body is the block that keeps the author's line breaks. Should that + // marker go, every free-standing block is better than nothing. + const bodies = candidates.filter((node) => node.classList.contains('whitespace-pre-wrap')); + const chosen = bodies.length ? bodies : candidates; + return [ + ...chosen.filter((node) => !inFrame(node, article)), + ...chosen.filter((node) => inFrame(node, article)), + ]; +} + export function postId(article: Element): string { for (const time of article.querySelectorAll('time')) { const link = time.closest('a'); if (!link || link.closest('div[role="link"]')) continue; - const id = /\/status\/(\d+)(?:\/|$)/.exec(link.getAttribute('href') ?? '')?.[1]; + const id = statusPattern.exec(link.getAttribute('href') ?? '')?.[1]; + if (id) return id; + } + // The rebuilt client: the row wrapper names the post; a focused post has no + // wrapper and names itself through its timestamp link. + const wrapper = article.closest('[role="link"][data-href]'); + const wrapped = wrapper && !inFrame(wrapper, article) ? wrapper.getAttribute('data-href') : null; + const fromWrapper = statusPattern.exec(wrapped ?? '')?.[1]; + if (fromWrapper) return fromWrapper; + for (const link of article.querySelectorAll('a[href*="/status/"]')) { + if (inFrame(link, article) || link.getAttribute('aria-hidden') === 'true') continue; + const href = link.getAttribute('href') ?? ''; + if (/\/(?:photo|video|analytics|quotes|retweets|likes)\b/.test(href)) continue; + const id = statusPattern.exec(href)?.[1]; if (id) return id; } return ''; @@ -47,7 +117,7 @@ export function smallImage(url: string): string { export function images(article: Element, max: number): string[] { if (max <= 0) return []; const photos = article.querySelectorAll( - '[data-testid="tweetPhoto"] img, [data-testid="previewInterstitial"] img, [data-testid^="card."] img', + '[data-testid="tweetPhoto"] img, [data-testid="previewInterstitial"] img, [data-testid^="card."] img, [data-engagement-action="media"] img', ); const videos = article.querySelectorAll('video[poster]'); return [ @@ -69,9 +139,19 @@ export function focalArticle(id: string): HTMLElement | null { ); } +/** Where a row sits in its timeline. The rebuilt client positions rows + * absolutely and reuses nodes, so document order says nothing; the index does. */ +function rowIndex(article: Element): number | null { + const index = cellOf(article)?.getAttribute('data-index'); + return index === null || index === undefined ? null : Number(index); +} + export function isThreadContext(article: Element, focal: Element | null): boolean { if (!focal || !article.isConnected || !focal.isConnected) return false; if (article === focal) return true; + const own = rowIndex(article); + const anchor = rowIndex(focal); + if (own !== null && anchor !== null) return own < anchor; const position = focal.compareDocumentPosition(article); return ( !(position & Node.DOCUMENT_POSITION_DISCONNECTED) && @@ -79,24 +159,56 @@ export function isThreadContext(article: Element, focal: Element | null): boolea ); } +/** The header above a reposted post, naming who reposted it. */ +function socialContext(article: Element): Element | null { + const labelled = article.querySelector('[data-testid="socialContext"]'); + if (labelled) return labelled; + // The rebuilt client draws it with a repost icon, either as the article's + // first block or just before the article inside its row. + let first = article.firstElementChild; + if (first?.classList.contains('aitf-slot')) first = first.nextElementSibling; + const own = socialOf(first); + if (own) return own; + let before = article.previousElementSibling; + if (before?.matches('a[aria-hidden="true"]')) before = before.previousElementSibling; + return socialOf(before); +} +function socialOf(block: Element | null): Element | null { + if (!block || block.querySelector('[data-post-avatar]')) return null; + const icon = block.querySelector('svg[data-icon]')?.getAttribute('data-icon') ?? ''; + const words = block.textContent?.trim() ?? ''; + return /repost|retweet/i.test(icon) || /\b(?:reposted|retweeted)\b/i.test(words) ? block : null; +} + export function reposter(article: Element): string { - const context = article.querySelector('[data-testid="socialContext"]'); + const context = socialContext(article); const link = context?.querySelector('a[href^="/"]') ?? context?.closest('a[href^="/"]'); return handlePattern.exec(link?.getAttribute('href') ?? '')?.[1] ?? ''; } +/** The row drawn just above this one, whichever client drew it. */ +function previousArticle(article: Element): Element | null { + const cell = cellOf(article); + if (!cell) return null; + const index = rowIndex(article); + if (index !== null && cell.parentElement) { + const previous = cell.parentElement.querySelector(`:scope > li[data-index="${index - 1}"]`); + return previous?.querySelector(articleSelector) ?? null; + } + return cell.previousElementSibling?.querySelector(articleSelector) ?? null; +} + function parentContext(article: Element, focal: Element | null): string { const targets = [...article.querySelectorAll('a[href^="/"]')] .filter( (link) => + !inFrame(link, article) && !link.closest( - `${authorSelector}, [data-testid="tweetText"], div[role="link"], [data-testid="socialContext"], .aitf-slot`, + `${authorSelector}, [data-testid="tweetText"], div[role="link"], [data-testid="socialContext"], [data-post-avatar], .x-middot-group, .aitf-slot`, ), ) .map((link) => handlePattern.exec(link.getAttribute('href') ?? '')?.[1]?.toLowerCase()); - const previous = article - .closest('[data-testid="cellInnerDiv"]') - ?.previousElementSibling?.querySelector(articleSelector); + const previous = previousArticle(article); const parent = previous && targets.includes(author(previous).handle.toLowerCase()) ? previous diff --git a/src/x/post-menu-dom.ts b/src/x/post-menu-dom.ts index b3399bb..9070ae4 100644 --- a/src/x/post-menu-dom.ts +++ b/src/x/post-menu-dom.ts @@ -1,7 +1,8 @@ // X portals its post menu into #layers: a dropdown at desktop widths, a bottom // sheet at phone widths. No generated class names or translated labels are used // for selection. Unknown layouts are left untouched. -export const moreSelector = '[data-testid="caret"]'; +export const moreSelector = + '[data-testid="caret"], button[aria-label="More"][data-slot="xds-menu-trigger"]'; export const sheetSelector = '[data-testid="sheetDialog"]'; export const dropdownSelector = `[data-testid="Dropdown"], ${sheetSelector}`; const itemSelector = '[role="menuitem"]'; diff --git a/src/x/style.css b/src/x/style.css index 5f57d1d..184faaa 100644 --- a/src/x/style.css +++ b/src/x/style.css @@ -1020,3 +1020,13 @@ html[data-aitf-motion='reduced'] .aitf-inspector { width: 14px; height: 14px; } + +/* The rebuilt client (November 2026). Its rows are absolutely positioned and + measured by X's virtualiser: a row that collapses to nothing leaves X's + cached offsets standing and opens a blank gap, so a fully hidden post keeps + its row at a pixel, and the row's own padding and rule go with the post. */ +[id^='urt:'] > ul > li:has(article[data-aitf-hidden='remove']) > [role='link'] { + min-height: 1px; + padding-block: 0 !important; + border-bottom: 0 !important; +} diff --git a/src/x/thread-control.tsx b/src/x/thread-control.tsx index 87e8743..ca3e7b3 100644 --- a/src/x/thread-control.tsx +++ b/src/x/thread-control.tsx @@ -2,7 +2,7 @@ import { render } from 'preact'; import { useEffect, useRef, useState } from 'preact/hooks'; import { request } from '../common/messages'; import type { PublicSettings } from '../common/settings'; -import { articleSelector, currentThread, focalArticle } from './extract'; +import { articleSelector, cellSelector, currentThread, focalArticle } from './extract'; import { visible } from './post-menu-dom'; import { Check, Mark } from './icons'; import { typeface } from './view'; @@ -10,7 +10,7 @@ import { typeface } from './view'; /** Find the inline reply section, never a modal composer or a timeline's post box. */ export function replySection(id: string): HTMLElement | null { const focal = focalArticle(id); - const column = focal?.closest('[data-testid="primaryColumn"]'); + const column = focal?.closest('[data-testid="primaryColumn"], main'); if (!focal || !column) return null; const candidates: HTMLElement[] = []; for (const editor of column.querySelectorAll('[data-testid="tweetTextarea_0"]')) { @@ -31,13 +31,13 @@ export function replySection(id: string): HTMLElement | null { while ( section.parentElement && section.parentElement !== column && - !section.parentElement.matches('[data-testid="cellInnerDiv"]') && + !section.parentElement.matches(cellSelector) && !section.parentElement.querySelector(articleSelector) ) { section = section.parentElement; } // Keep the control inside a virtualized cell so X can measure its height. - if (section.matches('[data-testid="cellInnerDiv"]')) { + if (section.matches(cellSelector)) { section = [...section.children].find( (child): child is HTMLElement => @@ -60,15 +60,15 @@ export function replySection(id: string): HTMLElement | null { * reason the desktop path descends into the cell it finds. */ function firstReplyAnchor(id: string): HTMLElement | null { const focal = focalArticle(id); - const cell = focal?.closest('[data-testid="cellInnerDiv"]'); - if (!cell || !focal?.closest('[data-testid="primaryColumn"]')) return null; + const cell = focal?.closest(cellSelector); + if (!cell || !focal?.closest('[data-testid="primaryColumn"], main')) return null; for (let next = cell.nextElementSibling; next; next = next.nextElementSibling) { if (!(next instanceof HTMLElement) || next.matches('.aitf-thread-control')) continue; // X separates entries with empty cells, one straight after the focal post. if (!next.textContent?.trim() && !next.querySelector(articleSelector)) continue; // Anchor on the first reply, and only on a reply: anything else after the // focal post is X's own furniture and not somewhere to mount a control. - if (!next.matches('[data-testid="cellInnerDiv"]') || !next.querySelector(articleSelector)) { + if (!next.matches(cellSelector) || !next.querySelector(articleSelector)) { return null; } return ( diff --git a/src/x/view.tsx b/src/x/view.tsx index ca52dd6..2b40adf 100644 --- a/src/x/view.tsx +++ b/src/x/view.tsx @@ -2,7 +2,7 @@ import { render } from 'preact'; import { useEffect, useState } from 'preact/hooks'; import type { PublicSettings } from '../common/settings'; import { Check, Inspect, Mark, Query } from './icons'; -import { articleSelector } from './extract'; +import { articleSelector, cellOf } from './extract'; export type HiddenStyle = PublicSettings['hideStyle'] | 'remove'; /** What the reader can do about a verdict they have already overruled by revealing. */ @@ -54,7 +54,6 @@ export type FeedbackState = { onUndo: () => void; }; -const cellSelector = '[data-testid="cellInnerDiv"]'; type Mounted = { root: HTMLDivElement; signature: string }; const mounted = new WeakMap(); const signatureOf = (state: PostState) => @@ -149,7 +148,7 @@ function unmount(article: HTMLElement) { delete article.dataset.aitfHidden; delete article.dataset.aitfRevealed; delete article.dataset.aitfKept; - article.closest(cellSelector)?.removeAttribute('data-aitf-unsure'); + cellOf(article)?.removeAttribute('data-aitf-unsure'); } export function reveal(article: HTMLElement) { @@ -277,12 +276,15 @@ export function apply(article: HTMLElement, state: PostState, onReveal: () => vo const family = typeface(article); if (family) root.style.fontFamily = family; article.prepend(root); + // The rebuilt client lays the article out as a column, where a full-width + // basis would be read as height. The stylesheet's basis is for the old row. + if (getComputedStyle(article).flexDirection.startsWith('column')) root.style.flex = '0 0 auto'; if (state.kind === 'revealed') article.dataset.aitfRevealed = ''; else if (state.kind === 'kept') article.dataset.aitfKept = ''; else article.dataset.aitfHidden = state.kind === 'pending' ? 'pending' : state.style; // The tint belongs to the cell: the article sits inside X's padding. if (state.kind === 'hidden' && state.unsure) - article.closest(cellSelector)?.setAttribute('data-aitf-unsure', ''); + cellOf(article)?.setAttribute('data-aitf-unsure', ''); mounted.set(article, { root, signature }); if (state.kind === 'pending') { diff --git a/tests/fixtures/x-modern-home.html b/tests/fixtures/x-modern-home.html new file mode 100644 index 0000000..56e5152 --- /dev/null +++ b/tests/fixtures/x-modern-home.html @@ -0,0 +1,5 @@ + +
diff --git a/tests/x-modern.test.ts b/tests/x-modern.test.ts new file mode 100644 index 0000000..26719fa --- /dev/null +++ b/tests/x-modern.test.ts @@ -0,0 +1,93 @@ +// @vitest-environment jsdom +import { readFileSync } from 'node:fs'; +import { dirname, join } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { beforeEach, expect, it } from 'vitest'; +import * as extract from '../src/x/extract'; + +// Two rows captured from x.com/home on the rebuilt client: one photo post, one +// promoted video post with text. No data-testid anywhere. +const home = readFileSync( + join(dirname(fileURLToPath(import.meta.url)), 'fixtures', 'x-modern-home.html'), + 'utf8', +); + +const legacy = `
`; + +beforeEach(() => { + document.body.innerHTML = home; +}); + +it('finds the rebuilt client and its posts', () => { + expect(extract.modernClient()).toBe(true); + const articles = document.querySelectorAll(extract.articleSelector); + expect(articles).toHaveLength(2); + expect(extract.cellOf(articles[0]!)?.getAttribute('data-index')).toBe('0'); +}); + +it('reads the author, id, text and media of a rebuilt post', () => { + const [photo, video] = [...document.querySelectorAll(extract.articleSelector)]; + expect(extract.author(photo!)).toEqual({ + handle: 'burny_tech', + name: 'Burny - Effective Curiosity', + }); + expect(extract.postId(photo!)).toBe('2120255702132551840'); + expect(extract.text(photo!)).toBe(''); + expect(extract.images(photo!, 4)).toEqual([ + 'https://pbs.twimg.com/media/HWypUhaWEAAVJwh?format=webp&name=small', + ]); + expect(extract.reposter(photo!)).toBe(''); + + expect(extract.describe(video!, 4, null)).toEqual({ + key: '2117950049422660012', + handle: 'CabelaSchmitt', + text: 'Fun Lively Rock Christmas Song', + images: [ + 'https://pbs.twimg.com/amplify_video_thumb/2117949018722693120/img/SILJkI2JXDhFGqxh.jpg', + ], + context: '', + }); +}); + +it('orders rows by their index, not by where X happened to put the nodes', () => { + const [first, second] = [...document.querySelectorAll(extract.articleSelector)]; + // X recycles nodes: move the first row after the second in the document. + second!.closest('li')!.after(first!.closest('li')!); + expect(extract.isThreadContext(first!, second!)).toBe(true); + expect(extract.isThreadContext(second!, first!)).toBe(false); +}); + +it('treats a quoted post as part of the post around it', () => { + const [photo] = [...document.querySelectorAll(extract.articleSelector)]; + const body = photo!.querySelector('.flex.min-w-0.flex-1')!; + body.insertAdjacentHTML( + 'beforeend', + `
+
+ +
quoted words
+
`, + ); + expect(document.querySelectorAll(extract.articleSelector)).toHaveLength(2); + expect(extract.postId(photo!)).toBe('2120255702132551840'); + expect(extract.author(photo!).handle).toBe('burny_tech'); + expect(extract.text(photo!)).toBe('quoted words'); +}); + +it('still reads the old client', () => { + document.body.innerHTML = legacy; + expect(extract.modernClient()).toBe(true); + const article = document.querySelector(extract.articleSelector)!; + expect(extract.describe(article, 0, null)).toEqual({ + key: '42', + handle: 'alice', + text: 'old client text', + images: [], + context: '', + }); + expect(extract.cellOf(article)?.getAttribute('data-testid')).toBe('cellInnerDiv'); +});