Chat+ link previews: players for Vimeo and SoundCloud too, image links show the image

The YouTube-only field becomes a general embed address, made natively only for the frame hosts
X's chat CSP allows (www.youtube.com, player.vimeo.com, w.soundcloud.com) and re-checked in the
page. A link to an image previews the image itself. YouTube's thumbnail comes as data.
This commit is contained in:
CRTkafa 2026-10-10 21:56:46 +08:00
parent c03f1a989b
commit e90b468359
7 changed files with 115 additions and 32 deletions

View file

@ -1,8 +1,8 @@
// 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).
// src-tauri/src/link_preview.rs) for its preview and shows it under the message: any page's title,
// description and picture, an image link's image, and for YouTube, Vimeo and SoundCloud a play
// button that opens the site's own player in place (hosts X's own CSP allows as frames).
//
// 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.
@ -13,13 +13,20 @@ 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 };
export type LinkPreview = { url: string; site: string; title: string; description: string; image?: string; embed?: 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; }
/** The players a preview may open: the sites' own, on hosts X's chat page allows as frames. */
const PLAYERS = [
/^https:\/\/www\.youtube\.com\/embed\/[A-Za-z0-9_-]{11}\?autoplay=1&rel=0$/,
/^https:\/\/player\.vimeo\.com\/video\/\d{6,12}\?autoplay=1$/,
/^https:\/\/w\.soundcloud\.com\/player\/\?url=https%3A%2F%2Fsoundcloud\.com(?:%2F[A-Za-z0-9_-]+){1,3}&auto_play=true&visual=true$/,
];
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. */
@ -41,11 +48,11 @@ export function asPreview(value: unknown): LinkPreview | null {
const url = previewable(str('url', 2048));
if (!url) return null;
const image = str('image', 3_000_000);
const youtube = str('youtube', 11);
const embed = str('embed', 400);
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;
if (PLAYERS.some(player => player.test(embed))) preview.embed = embed;
return preview.title || preview.image || preview.embed ? preview : null;
}
const CARD = 'bt-chat-lp';
@ -121,12 +128,15 @@ export function renderCard(doc: Document, preview: LinkPreview, words: { play: s
card.rel = 'noopener noreferrer';
card.setAttribute(FOR, preview.url);
card.title = words.open;
if (preview.youtube) {
if (preview.embed) {
const embed = preview.embed;
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';
if (preview.image) {
const thumb = el(doc, 'img', '');
thumb.src = preview.image;
thumb.alt = '';
media.append(thumb);
}
const play = el(doc, 'button', `${CARD}-play`);
play.type = 'button';
play.setAttribute('aria-label', words.play);
@ -140,16 +150,16 @@ export function renderCard(doc: Document, preview: LinkPreview, words: { play: s
event.preventDefault();
event.stopPropagation();
const frame = el(doc, 'iframe', '');
frame.src = `https://www.youtube.com/embed/${preview.youtube}?autoplay=1&rel=0`;
frame.src = embed;
frame.allow = 'autoplay; encrypted-media; picture-in-picture; fullscreen';
frame.allowFullscreen = true;
frame.referrerPolicy = 'strict-origin-when-cross-origin';
frame.title = preview.title || 'YouTube';
frame.title = preview.title || preview.site;
media.replaceChildren(frame);
card.removeAttribute('href');
card.style.cursor = 'default';
});
media.append(thumb, play);
media.append(play);
card.append(media);
} else if (preview.image) {
const media = el(doc, 'span', `${CARD}-media`);