Something went wrong. Try again.
smol embed service for rednote (xiaohongshu)
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124import { env } from "cloudflare:workers";
const HEADERS = { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", "Accept-Encoding": "gzip, deflate, br, zstd", "Accept-Language": "de-DE,de;q=0.9", "Cache-Control": "max-age=0", "Connection": "keep-alive", "Sec-Fetch-Dest": "document", "Sec-Fetch-Mode": "navigate", "Sec-Fetch-Site": "none", "Sec-Fetch-User": "?1", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/153.0.0.0 Safari/537.36", "sec-ch-ua": `"Google Chrome";v="153", "Not_A Brand";v="8", "Chromium";v="153"`, "sec-ch-ua-mobile": "?0", "sec-ch-ua-platform": "\"Windows\"",};
export const fetchData = async (url: string) => { let req; if (env.XHS_PROXY) { const parsedUrl = new URL(url); const proxyUrl = new URL(env.XHS_PROXY); proxyUrl.pathname = parsedUrl.pathname; proxyUrl.search = parsedUrl.search;
req = await fetch(proxyUrl, { headers: { ...HEADERS, "X-Host": parsedUrl.hostname, "Authorization": env.XHS_PROXY_AUTH, } }); } else { req = await fetch(url, { headers: HEADERS }); }
const html = await req.text(); return extractData(html);}
const INITIAL_DATA_RE = /<script>window.__INITIAL_STATE__=(.+?)<\/script>/;export const extractData = (html: string) => { const match = html.match(INITIAL_DATA_RE); if (!match) throw new Error("No data in HTML");
const [, initialDataJSON] = match; const initialData = JSON.parse(initialDataJSON.replaceAll(":undefined", ":null"));
return buildDataFromInitialData(initialData);}
export const buildDataFromInitialData = (data: any) => { if (!data.note || !data.note.firstNoteId) throw new Error("No note found; is &xsec_token missing?");
const { note } = data.note.noteDetailMap[data.note.firstNoteId]; const videoInfo = note.type == "video" ? JSON.parse(note.video.mediaV2) : null;
return { title: note.title, description: note.desc, author: { username: note.user.nickname, avatar: note.user.avatar, }, images: note.imageList?.map((i: any) => ({ url: i.urlDefault, })), video: videoInfo ? (Object.values(videoInfo?.stream) as any[]).flat().find((stream: any) => stream.master_url)?.master_url : null, time: note.time,
url: `https://xiaohongshu.com/explore/${note.noteId}?xsec_token=${note.xsecToken}`, };}type ExtractedData = ReturnType<typeof buildDataFromInitialData>;
const sanitize = (text: string) => { return text .replaceAll("&", "&") .replaceAll(">", ">") .replaceAll("<", "<") .replaceAll("\"", """);}
export const generateEmbedResponse = (data: ExtractedData) => { const html = /*html*/`<html> <head> <title>${data.title}</title> <meta charset="utf-8">
<link rel="canonical" href="${data.url}"> <meta property="og:url" content="${data.url}">
<meta property="og:title" content="${sanitize(data.title)}"> <meta property="og:description" content="${sanitize(data.description)}"> <meta property="og:site_name" content="rednote"> <meta name="theme-color" content="#ff2442"> ${data.video ? /*html*/` <meta property="twitter:card" content="player"> <meta property="og:video" content="${sanitize(data.video)}"> <meta property="og:video:type" content="video/mp4"> <meta property="twitter:title" content="${sanitize(data.title)}"> ` : ""}
${!data.video && data.images ? /*html*/` <meta property="twitter:card" content="summary_large_image"> <meta property="og:image" content="${data.images[0].url}"> ` : ""} </head> <body> <a href="${data.url}">Click here to go to Rednote</a> <meta http-equiv="Refresh" content="1;url=${data.url}"> </body> </html>`;
return new Response(html, { headers: { "Content-Type": "text/html;charset=utf-8", "Cache-Control": "public, max-age=604800", } });};