wayBetter for X

This commit is contained in:
CRTkafa 2026-10-01 06:01:13 +07:00
commit df5dff619e
720 changed files with 147465 additions and 0 deletions

View file

@ -0,0 +1,74 @@
// "Links to X pages" setting: when it is not "same", a plain click on a post, a profile, a hashtag or a
// list inside the timeline (or a hover card) opens that page beside the pane, in a new tab or in the
// system browser instead of replacing what you are reading. The click becomes an ordinary new-window
// request; native code (`new_window_mode`) decides where it opens. Photos and videos keep X's own
// in-place viewer; navigation, tabs, menus and every button are never touched. The click router is
// the wayBetter runtime's (linkRouting.ts); which X clicks count is decided here (`clickedXUrl`).
import {
installLinkRouting as installCore,
type LinkMode,
type LinkRoutingHandle,
} from '../../../../waybetter/packages/runtime/src/linkRouting.ts';
export type XLinkMode = LinkMode;
export type { LinkRoutingHandle };
/** Only content: timeline/conversation cells and hover cards. */
const SCOPE = '[data-testid="cellInnerDiv"], [data-testid="HoverCard"]';
/** A press on one of these is X's (like, reply, menu, media, play…), never a navigation to divert. */
const INTERACTIVE = 'button, [role="button"], [role="menuitem"], [role="tab"], input, textarea, select, video, [data-testid="videoPlayer"], [data-testid="tweetPhoto"]';
/** First path segments that are X pages, not @handles. */
const RESERVED = new Set([
'home', 'explore', 'notifications', 'messages', 'i', 'settings', 'search', 'hashtag', 'compose', 'login', 'logout',
'signup', 'tos', 'privacy', 'jobs', 'account', 'intent', 'share', 'download', 'about', 'display', 'keyboard_shortcuts',
'communities', 'premium', 'jf', 'bookmarks',
]);
const HANDLE = '[A-Za-z0-9_]{1,15}';
const STATUS = new RegExp(`^/${HANDLE}/status/\\d+(?:/(?:quotes|retweets|reposts|likes|history))?$`);
const PROFILE = new RegExp(`^/(${HANDLE})(?:/(?:with_replies|media|likes|highlights|articles|followers|following|verified_followers|lists))?$`);
/** Pure: an X page worth opening elsewhere (post, profile, hashtag, search, list, community). */
export function routableXPath(pathname: string, search = ''): boolean {
const path = pathname.length > 1 ? pathname.replace(/\/+$/, '') : pathname;
if (STATUS.test(path) || /^\/hashtag\/[^/]+$/.test(path) || /^\/i\/(?:lists|communities)\/\d+$/.test(path)) return true;
if (path === '/search') return /(?:^|[?&])q=./.test(search);
const profile = PROFILE.exec(path);
return !!profile && !RESERVED.has(profile[1]!.toLowerCase());
}
const sameOriginRoutable = (anchor: HTMLAnchorElement, location: Location): string | null => {
// target=_blank links already reach native code as new-window requests (the "other sites" setting).
if (anchor.target && anchor.target !== '_self') return null;
let url: URL;
try { url = new URL(anchor.getAttribute('href') ?? '', location.href); } catch { return null; }
return url.origin === location.origin && routableXPath(url.pathname, url.search) ? url.href : null;
};
/** The X page a click on `target` would open in place, when it is one to divert; else null. */
export function clickedXUrl(target: Element | null, location: Location): string | null {
const scope = target?.closest?.(SCOPE);
if (!target || !scope) return null;
const anchor = target.closest<HTMLAnchorElement>('a[href]');
if (anchor && scope.contains(anchor)) return anchor.closest(INTERACTIVE) ? null : sameOriginRoutable(anchor, location);
if (target.closest(INTERACTIVE)) return null;
// X opens a post when its body (not a link or button) is clicked: find that post's permalink.
const article = target.closest('article[data-testid="tweet"]');
if (!article || !scope.contains(article)) return null;
const permalink = (root: Element, skipQuotes: boolean) => [...root.querySelectorAll<HTMLAnchorElement>('a[href*="/status/"]')]
.find(link => link.querySelector('time') && !(skipQuotes && link.closest('div[role="link"]')));
const quote = target.closest('div[role="link"]');
if (quote && article.contains(quote)) {
const link = permalink(quote, false);
return link ? sameOriginRoutable(link, location) : null;
}
// tabindex=-1 is the post a status page is about: X does nothing on its body.
if (article.getAttribute('tabindex') !== '0') return null;
const link = permalink(article, true);
return link ? sameOriginRoutable(link, location) : null;
}
/** Native code configures it with `{ site: mode }` (waybetter-host `settings::links_script`). */
export function installLinkRouting(win: Window): LinkRoutingHandle {
return installCore(win, { clickedUrl: clickedXUrl, configKey: 'site' });
}