Chat+ link previews: a link in a chat shows its page's title, text and picture, YouTube playable in place
X's web chat shows a link as a bare card. An X tab on a chat page may now ask native code (link_preview, x-capture only, chat pages only) for one link's preview. Native code fetches it: https and public hosts only (addresses checked and pinned), never X's hosts, no cookies or referrer, redirects re-checked, page head <= 512 KB, images <= 1.5 MB by magic bytes as data:, 30 fetches a minute, memory-only cache. Only the link leaves the page. On by default (owner, 2026-10-10).
This commit is contained in:
parent
89c9bfc75f
commit
c03f1a989b
21 changed files with 1455 additions and 519 deletions
|
|
@ -18,6 +18,8 @@ export const FEATURES: readonly FeatureSpec[] = [
|
|||
{ id: 'chat.searchLoaded', surfaces: CHAT, heavy: true, ...words('searchLoaded'), keywords: 'find search in conversation ctrl shift f messages', permissions: ['ui', 'commands'] },
|
||||
{ id: 'chat.pins', surfaces: CHAT, heavy: true, ...words('pins'), keywords: 'pin pinned label color chats conversations strip', permissions: ['storage', 'ui', 'commands'] },
|
||||
{ id: 'chat.personFilter', surfaces: CHAT, heavy: true, ...words('personFilter'), keywords: 'group chat person participant filter only from', permissions: ['storage', 'ui', 'commands'] },
|
||||
// On by default (owner, 2026-10-10): asks native code for the preview of a link on screen (link_preview.rs).
|
||||
{ id: 'chat.linkPreviews', surfaces: CHAT, heavy: true, default: true, ...words('linkPreviews'), keywords: 'link preview embed youtube card unfurl', permissions: ['ui'] },
|
||||
{ id: 'chat.keywords', surfaces: CHAT, heavy: true, settings: true, ...words('keywords'), keywords: 'highlight mute words keywords hide messages', permissions: ['storage', 'ui'] },
|
||||
]),
|
||||
];
|
||||
|
|
|
|||
|
|
@ -5,12 +5,14 @@
|
|||
// X's chat is the one place the rest of the app stays out of (a heavy page). These modules run
|
||||
// there on purpose and keep it cheap: one shared watch each (child list only, one frame), looking
|
||||
// only at X's message list or its conversation list. ./x.ts is what they know about X's chat.
|
||||
// None of them makes a request, writes or sends a message, reacts, deletes or marks anything read.
|
||||
// None of them writes or sends a message, reacts, deletes or marks anything read. The one request is
|
||||
// Link previews' (owner-approved, 2026-10-10): the link alone, to native code, which fetches its preview.
|
||||
|
||||
import type { FeatureModule } from '../../../../../waybetter/packages/runtime/src/featureModules.ts';
|
||||
import type { PluginApi } from '../../inject/pluginApi.ts';
|
||||
import { bookmarks } from './bookmarks.ts';
|
||||
import { keywords } from './keywords.ts';
|
||||
import { linkPreviews } from './linkPreviews.ts';
|
||||
import { personFilter } from './personFilter.ts';
|
||||
import { pins } from './pins.ts';
|
||||
import { searchLoaded } from './searchLoaded.ts';
|
||||
|
|
@ -21,4 +23,5 @@ export const MODULES: ReadonlyArray<FeatureModule<PluginApi>> = [
|
|||
pins,
|
||||
personFilter,
|
||||
keywords,
|
||||
linkPreviews,
|
||||
];
|
||||
|
|
|
|||
229
apps/desktop/src/areas/chat/linkPreviews.ts
Normal file
229
apps/desktop/src/areas/chat/linkPreviews.ts
Normal file
|
|
@ -0,0 +1,229 @@
|
|||
// Chat+ · Link previews. X's web chat shows a link as a bare card (an icon and the domain). For a
|
||||
// link in a message that is on screen, this asks native code (`link_preview`, one link at a time;
|
||||
// src-tauri/src/link_preview.rs) for its preview and shows it under X's card: the page's title,
|
||||
// description and image, or for YouTube the video's thumbnail with a play button that opens
|
||||
// YouTube's own player in place (www.youtube.com is in X's own frame-src).
|
||||
//
|
||||
// Owner-approved network use (2026-10-10). Only the link leaves the page: never the message text,
|
||||
// the sender or the conversation. Nothing is stored: answers are kept in memory for this page only.
|
||||
// X's card is hidden while a preview stands in for it and comes back when the feature stops.
|
||||
|
||||
import type { FeatureContext, FeatureModule } from '../../../../../waybetter/packages/runtime/src/featureModules.ts';
|
||||
import type { PluginApi } from '../../inject/pluginApi.ts';
|
||||
import { clearAttribute } from './ui.ts';
|
||||
import { CONVERSATION_PROBES, CHAT_PAGE_PROBE, chatRoot, chatSelectors, renderedRows } from './x.ts';
|
||||
|
||||
export type LinkPreview = { url: string; site: string; title: string; description: string; image?: string; youtube?: string };
|
||||
type Invoke = (command: 'link_preview', args: { url: string }) => Promise<unknown>;
|
||||
|
||||
let bridge: Invoke | null = null;
|
||||
/** The runtime hands over its native bridge (src/inject/entry.tsx); without one the feature shows nothing. */
|
||||
export function setLinkPreviewBridge(invoke: Invoke | null | undefined): void { bridge = invoke ?? null; }
|
||||
|
||||
const X_HOSTS = /(^|\.)(x\.com|twitter\.com|t\.co|twimg\.com|x\.co)$/i;
|
||||
|
||||
/** A link worth a preview: plain https, a public-looking host name, not X's own. */
|
||||
export function previewable(href: string | null | undefined): string | null {
|
||||
if (!href) return null;
|
||||
let url: URL;
|
||||
try { url = new URL(href); } catch { return null; }
|
||||
if (url.protocol !== 'https:' || url.username || url.password || (url.port && url.port !== '443')) return null;
|
||||
const host = url.hostname.toLowerCase();
|
||||
if (!host.includes('.') || /^[\d.]+$/.test(host) || host.startsWith('[') || X_HOSTS.test(host)) return null;
|
||||
return url.href;
|
||||
}
|
||||
|
||||
/** A native answer as a preview, or null: every field is checked again (it becomes page text). */
|
||||
export function asPreview(value: unknown): LinkPreview | null {
|
||||
if (!value || typeof value !== 'object') return null;
|
||||
const v = value as Record<string, unknown>;
|
||||
const str = (key: string, max: number) => (typeof v[key] === 'string' ? (v[key] as string).slice(0, max) : '');
|
||||
const url = previewable(str('url', 2048));
|
||||
if (!url) return null;
|
||||
const image = str('image', 3_000_000);
|
||||
const youtube = str('youtube', 11);
|
||||
const preview: LinkPreview = { url, site: str('site', 80), title: str('title', 200), description: str('description', 300) };
|
||||
if (/^data:image\/(png|jpeg|gif|webp);base64,[A-Za-z0-9+/=]+$/.test(image)) preview.image = image;
|
||||
if (/^[A-Za-z0-9_-]{11}$/.test(youtube)) preview.youtube = youtube;
|
||||
return preview.title || preview.image || preview.youtube ? preview : null;
|
||||
}
|
||||
|
||||
const CARD = 'bt-chat-lp';
|
||||
const HIDE = 'data-bt-chat-lp-hidden';
|
||||
const FOR = 'data-bt-chat-lp-for';
|
||||
const ACCENT = 'var(--wb-accent, rgb(29, 155, 240))';
|
||||
|
||||
const CSS = `
|
||||
[${HIDE}] { display: none !important; }
|
||||
.${CARD} { display: block; box-sizing: border-box; width: 340px; max-width: 100%; margin: 6px 0 2px; overflow: hidden; border-radius: 16px; border: 1px solid rgba(127,127,127,.28); background: rgba(127,127,127,.10); color: inherit; text-decoration: none; font: 400 14px/1.35 var(--wb-font, system-ui, sans-serif); cursor: pointer; }
|
||||
.${CARD}:hover { background: rgba(127,127,127,.16); }
|
||||
.${CARD}:focus-visible { outline: 2px solid ${ACCENT}; outline-offset: 2px; }
|
||||
.${CARD}-media { position: relative; display: block; width: 100%; aspect-ratio: 1.91 / 1; background: rgba(0,0,0,.35); }
|
||||
.${CARD}-media.is-video { aspect-ratio: 16 / 9; }
|
||||
.${CARD}-media img, .${CARD}-media iframe { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; border: 0; display: block; }
|
||||
.${CARD}-play { position: absolute; left: 50%; top: 50%; width: 68px; height: 48px; transform: translate(-50%, -50%); border: 0; border-radius: 14px; background: rgba(0,0,0,.72); color: #fff; display: flex; align-items: center; justify-content: center; cursor: pointer; }
|
||||
.${CARD}-play:hover { background: #f00; }
|
||||
.${CARD}-play svg { width: 26px; height: 26px; fill: currentColor; }
|
||||
.${CARD}-body { display: block; padding: 10px 14px 12px; }
|
||||
.${CARD}-site { display: block; font-size: 12.5px; opacity: .65; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
.${CARD}-title { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; margin-top: 2px; font-weight: 700; }
|
||||
.${CARD}-desc { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; margin-top: 2px; font-size: 13.5px; opacity: .78; }
|
||||
`;
|
||||
|
||||
/** The links of one message: its own anchors to the outside, first one only per address. */
|
||||
export function rowLinks(row: Element): HTMLAnchorElement[] {
|
||||
const seen = new Set<string>();
|
||||
const out: HTMLAnchorElement[] = [];
|
||||
for (const anchor of Array.from(row.querySelectorAll<HTMLAnchorElement>('a[href]'))) {
|
||||
if (anchor.closest(`.${CARD}`)) continue;
|
||||
const url = previewable(anchor.href || anchor.getAttribute('href'));
|
||||
if (!url || seen.has(url)) continue;
|
||||
seen.add(url);
|
||||
out.push(anchor);
|
||||
}
|
||||
return out.slice(0, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* X's bare link card (a message that is only a link: an icon and the host name, no picture). A link
|
||||
* inside a message's text shows its path too and is never hidden.
|
||||
*/
|
||||
export function isBareCard(anchor: Element): boolean {
|
||||
if (anchor.querySelector('img, video')) return false;
|
||||
const text = (anchor.textContent ?? '').trim().replace(/^www\./i, '');
|
||||
return /^[a-z0-9-]+(\.[a-z0-9-]+)*\.[a-z]{2,}$/i.test(text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Where a link's preview goes: after the message's bubble (X's `[role="article"]`), or after X's own
|
||||
* link card when the link is one; null when the anchor is in neither (nothing is placed).
|
||||
*/
|
||||
export function cardPlace(anchor: Element): Element | null {
|
||||
const bubble = anchor.closest('[role="article"]') ?? anchor.closest('[data-testid^="message-text-"]');
|
||||
return bubble?.parentElement ? bubble : null;
|
||||
}
|
||||
|
||||
const PLAY = 'M8 5.5v13l11-6.5z';
|
||||
const SVG = 'http://www.w3.org/2000/svg';
|
||||
|
||||
function el<K extends keyof HTMLElementTagNameMap>(doc: Document, tag: K, className: string, text?: string): HTMLElementTagNameMap[K] {
|
||||
const node = doc.createElement(tag);
|
||||
node.className = className;
|
||||
if (text) node.textContent = text;
|
||||
return node;
|
||||
}
|
||||
|
||||
/** The preview card for `preview` (text only through textContent; URLs re-checked). */
|
||||
export function renderCard(doc: Document, preview: LinkPreview, words: { play: string; open: string }): HTMLAnchorElement {
|
||||
const card = el(doc, 'a', CARD);
|
||||
card.href = preview.url;
|
||||
card.target = '_blank';
|
||||
card.rel = 'noopener noreferrer';
|
||||
card.setAttribute(FOR, preview.url);
|
||||
card.title = words.open;
|
||||
if (preview.youtube) {
|
||||
const media = el(doc, 'span', `${CARD}-media is-video`);
|
||||
const thumb = el(doc, 'img', '');
|
||||
thumb.src = preview.image ?? `https://i.ytimg.com/vi/${preview.youtube}/hqdefault.jpg`;
|
||||
thumb.alt = '';
|
||||
thumb.referrerPolicy = 'no-referrer';
|
||||
const play = el(doc, 'button', `${CARD}-play`);
|
||||
play.type = 'button';
|
||||
play.setAttribute('aria-label', words.play);
|
||||
const svg = doc.createElementNS(SVG, 'svg');
|
||||
svg.setAttribute('viewBox', '0 0 24 24');
|
||||
const path = doc.createElementNS(SVG, 'path');
|
||||
path.setAttribute('d', PLAY);
|
||||
svg.append(path);
|
||||
play.append(svg);
|
||||
play.addEventListener('click', event => {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
const frame = el(doc, 'iframe', '');
|
||||
frame.src = `https://www.youtube.com/embed/${preview.youtube}?autoplay=1&rel=0`;
|
||||
frame.allow = 'autoplay; encrypted-media; picture-in-picture; fullscreen';
|
||||
frame.allowFullscreen = true;
|
||||
frame.referrerPolicy = 'strict-origin-when-cross-origin';
|
||||
frame.title = preview.title || 'YouTube';
|
||||
media.replaceChildren(frame);
|
||||
card.removeAttribute('href');
|
||||
card.style.cursor = 'default';
|
||||
});
|
||||
media.append(thumb, play);
|
||||
card.append(media);
|
||||
} else if (preview.image) {
|
||||
const media = el(doc, 'span', `${CARD}-media`);
|
||||
const image = el(doc, 'img', '');
|
||||
image.src = preview.image;
|
||||
image.alt = '';
|
||||
image.decoding = 'async';
|
||||
media.append(image);
|
||||
card.append(media);
|
||||
}
|
||||
const body = el(doc, 'span', `${CARD}-body`);
|
||||
body.append(el(doc, 'span', `${CARD}-site`, preview.site));
|
||||
if (preview.title) body.append(el(doc, 'span', `${CARD}-title`, preview.title));
|
||||
if (preview.description) body.append(el(doc, 'span', `${CARD}-desc`, preview.description));
|
||||
card.append(body);
|
||||
return card;
|
||||
}
|
||||
|
||||
type Ctx = FeatureContext<PluginApi>;
|
||||
|
||||
export const linkPreviews: FeatureModule<PluginApi> = {
|
||||
id: 'chat.linkPreviews',
|
||||
probes: { ...CONVERSATION_PROBES, ...CHAT_PAGE_PROBE },
|
||||
start(ctx: Ctx) {
|
||||
const { win } = ctx;
|
||||
const doc = win.document;
|
||||
const sel = chatSelectors(ctx, CONVERSATION_PROBES);
|
||||
/** Answers for this page only (null: no preview for that link). */
|
||||
const answers = new Map<string, LinkPreview | null>();
|
||||
const pending = new Set<string>();
|
||||
let stopped = false;
|
||||
const words = () => ({ play: ctx.text('play'), open: ctx.text('open') });
|
||||
|
||||
const ask = (url: string) => {
|
||||
if (!bridge || pending.has(url) || answers.has(url)) return;
|
||||
pending.add(url);
|
||||
bridge('link_preview', { url })
|
||||
.then(value => answers.set(url, asPreview(value)), () => answers.set(url, null))
|
||||
.finally(() => { pending.delete(url); if (!stopped) watch.schedule(); });
|
||||
};
|
||||
|
||||
const scan = () => {
|
||||
for (const row of renderedRows(chatRoot(doc), sel)) {
|
||||
let after: Element | null = null;
|
||||
for (const anchor of rowLinks(row)) {
|
||||
const url = previewable(anchor.href || anchor.getAttribute('href'))!;
|
||||
const answer = answers.get(url);
|
||||
if (answer === undefined) { ask(url); continue; }
|
||||
if (!answer) { if (anchor.hasAttribute(HIDE)) anchor.removeAttribute(HIDE); continue; }
|
||||
// Under the message's bubble (live 2026-10-10: X keeps a link in the text as a plain anchor),
|
||||
// in the bubble's own column, so it lines up with the message on either side.
|
||||
const bubble = cardPlace(anchor);
|
||||
if (!bubble) continue;
|
||||
const column = bubble.parentElement!;
|
||||
const existing = Array.from(column.children).find(node => node.getAttribute(FOR) === url);
|
||||
const card = existing ?? renderCard(doc, answer, words());
|
||||
const anchorAfter: Element = after ?? bubble;
|
||||
if (anchorAfter.nextElementSibling !== card) anchorAfter.after(card);
|
||||
after = card;
|
||||
if (isBareCard(anchor) && !anchor.hasAttribute(HIDE)) anchor.setAttribute(HIDE, '');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ctx.css(CSS);
|
||||
const watch = ctx.watch(scan);
|
||||
ctx.onRouteChange(() => watch.schedule());
|
||||
ctx.defer(() => {
|
||||
stopped = true;
|
||||
for (const place of new Set<ParentNode>([doc, chatRoot(doc)])) {
|
||||
for (const card of Array.from(place.querySelectorAll(`.${CARD}`))) card.remove();
|
||||
clearAttribute(place, HIDE);
|
||||
}
|
||||
});
|
||||
watch.schedule();
|
||||
},
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue