AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.
HTML ai-agentcloudflarecloudflare-d1cloudflare-r2cloudflare-workersdockerelectronevernoteevernote-alternativeknowledge-basemcpnote-takingobsidian-alternativeoffline-firstpersonal-knowledge-basepwaself-hostedsqlitetiptapweb-clipper
fix(extension): save the full text of a folded X post
tianma-if59b6ee2c7197
diff --git a/apps/extension/README.md b/apps/extension/README.mdindex 778b3c27..99613233 100644--- a/apps/extension/README.md+++ b/apps/extension/README.md@@ -9,7 +9,7 @@ Chrome, Edge, and Firefox Manifest V3 extension for saving the current webpage,- Click the extension action to capture the selected content, or extract the article body with Mozilla Readability when there is no selection.- Convert the extracted article HTML into Markdown with Turndown before uploading it.- Right-click an image and choose **Save image to EdgeEver**. The image file is uploaded into a new note in the default notebook, with the source page linked underneath.-- On X, right-click a post and choose **Save this post to EdgeEver**. The visible text, author, time, and photos already shown in that post are saved into one note. A single-post page can be saved immediately. On a timeline, the extension asks once for access to X so it can remember the post under the pointer.+- On X, right-click a post and choose **Save this post to EdgeEver**. The post text, author, time, and photos already shown in that post are saved into one note. Text folded behind Show more is expanded first, so a long post is saved in full. A single-post page can be saved immediately. On a timeline, the extension asks once for access to X so it can remember the post under the pointer.- When the page cannot hand over the image bytes, the extension asks once for access to that image's site. After that, later images from the same site save directly.- Create a searchable EdgeEver memo with the source URL and a `web-clip` tag.diff --git a/apps/extension/public/_locales/en/messages.json b/apps/extension/public/_locales/en/messages.jsonindex 84beeabc..22f690ed 100644--- a/apps/extension/public/_locales/en/messages.json+++ b/apps/extension/public/_locales/en/messages.json@@ -259,7 +259,7 @@"message": "Save this post to EdgeEver"},"tweetSaveHint": {- "message": "On X, right-click a post and choose “Save this post to EdgeEver”. The visible text and photos go into one new note in the same default notebook."+ "message": "On X, right-click a post and choose “Save this post to EdgeEver”. The post text, including text behind Show more, and the photos go into one new note in the same default notebook."},"savingTweet": {"message": "Saving this post…"diff --git a/apps/extension/public/_locales/ja/messages.json b/apps/extension/public/_locales/ja/messages.jsonindex 2e26801a..f19aa595 100644--- a/apps/extension/public/_locales/ja/messages.json+++ b/apps/extension/public/_locales/ja/messages.json@@ -259,7 +259,7 @@"message": "この投稿を EdgeEver に保存"},"tweetSaveHint": {- "message": "X で投稿を右クリックし、「この投稿を EdgeEver に保存」を選びます。表示中の本文と画像は、上で選んだ既定のノートブックに 1 件のノートとして保存されます。"+ "message": "X で投稿を右クリックし、「この投稿を EdgeEver に保存」を選びます。本文は「さらに表示」で折りたたまれた部分も含めて保存され、表示中の画像とともに、上で選んだ既定のノートブックへ 1 件のノートになります。"},"savingTweet": {"message": "この投稿を保存しています…"diff --git a/apps/extension/public/_locales/zh_CN/messages.json b/apps/extension/public/_locales/zh_CN/messages.jsonindex c7b183ef..1ebef711 100644--- a/apps/extension/public/_locales/zh_CN/messages.json+++ b/apps/extension/public/_locales/zh_CN/messages.json@@ -259,7 +259,7 @@"message": "保存这条推文到 EdgeEver"},"tweetSaveHint": {- "message": "在 X 上右键一条推文,选择“保存这条推文到 EdgeEver”。已经显示的文字和图片会放进同一条新笔记,笔记本与上面的默认笔记本相同。"+ "message": "在 X 上右键一条推文,选择“保存这条推文到 EdgeEver”。正文会完整保存,包括被「显示更多」折叠的部分;已经显示的图片也会放进同一条新笔记,笔记本与上面的默认笔记本相同。"},"savingTweet": {"message": "正在保存这条推文……"diff --git a/apps/extension/src/capture-tweet.ts b/apps/extension/src/capture-tweet.tsindex 4311b62c..e3732feb 100644--- a/apps/extension/src/capture-tweet.ts+++ b/apps/extension/src/capture-tweet.ts@@ -17,6 +17,15 @@const clean = (value: string) => value.replace(/\s+/g, " ").trim();+ // Keep identical to visibleTweetAuthor in tweet-clip.ts.+ const visibleTweetAuthor = (value: string) => {+ const parts = value.replace(/\r/g, "").split("\n").map((part) => part.trim()).filter(Boolean);+ const handlePart = parts.find((part) => /^@[A-Za-z0-9_]{1,15}$/.test(part));+ const handle = handlePart ? handlePart.slice(1) : "";+ const displayName = parts.find((part) => part !== handlePart && part !== "·" && part !== "•") ?? "";+ return { displayName, handle };+ };+const parseName = (rootNode: Element | null) => {if (!rootNode) return { displayName: "", handle: "" };const links = [...rootNode.querySelectorAll("a[href]")];@@ -28,20 +37,100 @@const text = clean(link.textContent || "");return link !== handleLink && text.length > 0 && !text.startsWith("@");});- return { displayName: clean(nameLink?.textContent || ""), handle };+ const displayName = clean(nameLink?.textContent || "");+ if (displayName || handle) return { displayName, handle };+ return visibleTweetAuthor((rootNode as HTMLElement).innerText || rootNode.textContent || "");};const hrefMatchesStatus = (href: string, id: string) => new RegExp(`/status/${id}(?:/|\\?|$)`).test(href);- const ownStatusHref = (article: Element) => {- const name = [...article.querySelectorAll('[data-testid="User-Name"]')].find((node) => !node.closest('[role="link"]'))- ?? article.querySelector('[data-testid="User-Name"]');- return name?.querySelector("time")?.closest("a")?.getAttribute("href") ?? "";+ const quotedTime = (time: Element) => {+ const name = time.closest('[data-testid="User-Name"]');+ return Boolean(name?.closest('[role="link"]'));+ };++ const ownTime = (article: Element) => [...article.querySelectorAll("time")].find((time) => !quotedTime(time)) ?? null;++ const ownStatusHref = (article: Element) => ownTime(article)?.closest("a")?.getAttribute("href") ?? "";++ const findArticle = (preferredId: string) => {+ const marked = document.querySelector('article[data-testid="tweet"][data-edgeever-tweet-target="1"]');+ if (marked) return marked;+ const id = preferredId || statusId;+ if (!id) return null;+ return [...document.querySelectorAll('article[data-testid="tweet"]')].find((article) => hrefMatchesStatus(ownStatusHref(article), id)) ?? null;+ };++ const isMainShowMore = (node: Element) => {+ const card = node.closest('[role="link"]');+ return !card || card === node;+ };++ const mainShowMore = (article: Element) => [...article.querySelectorAll('[data-testid="tweet-text-show-more-link"]')].find((node) => isMainShowMore(node)) ?? null;++ const mainTextNode = (article: Element) => {+ const texts = [...article.querySelectorAll('[data-testid="tweetText"]')];+ return texts.find((node) => !node.closest('[role="link"]')) ?? texts[0] ?? null;};- const articleFromStatus = () => {- if (!statusId) return null;- return [...document.querySelectorAll('article[data-testid="tweet"]')].find((article) => hrefMatchesStatus(ownStatusHref(article), statusId)) ?? null;+ const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));++ const expandTruncatedText = async (article: Element) => {+ const button = mainShowMore(article);+ if (!button) return article;+ const before = (mainTextNode(article)?.textContent || "").length;+ const statusKey = ownStatusHref(article).match(/\/status\/(\d+)/)?.[1] ?? statusId;+ (button as HTMLElement).click();+ const started = Date.now();+ const deadline = started + 4000;+ let current = article;+ while (Date.now() < deadline) {+ await sleep(80);+ const next = findArticle(statusKey) ?? (article.isConnected ? article : null);+ if (!next) continue;+ current = next;+ const length = (mainTextNode(current)?.textContent || "").length;+ const stillFolded = Boolean(mainShowMore(current));+ if (!stillFolded && (length > before || Date.now() - started > 500)) break;+ }+ const photoDeadline = Date.now() + 800;+ while (Date.now() < photoDeadline) {+ const next = findArticle(statusKey) ?? (current.isConnected ? current : null);+ if (next) current = next;+ if (current.querySelector('[data-testid="tweetPhoto"] img, [data-testid="card.layoutLarge.media"] img')) break;+ await sleep(80);+ }+ return current;+ };++ const visibleText = (node: Element | null) => {+ if (!node) return "";+ const parts: string[] = [];+ const walk = (current: Node) => {+ if (current.nodeType === Node.TEXT_NODE) {+ parts.push(current.nodeValue || "");+ return;+ }+ if (current.nodeType !== Node.ELEMENT_NODE) return;+ const element = current as HTMLElement;+ if (element.getAttribute("aria-hidden") === "true") return;+ if (element.getAttribute("data-testid") === "tweet-text-show-more-link") return;+ if (element.tagName === "BR") {+ parts.push("\n");+ return;+ }+ if (element.tagName === "IMG") {+ const alt = element.getAttribute("alt");+ if (alt) parts.push(alt);+ return;+ }+ const block = element !== node && /^(block|flex|grid|list-item)$/.test(getComputedStyle(element).display);+ if (block && parts.length > 0 && !parts[parts.length - 1]?.endsWith("\n")) parts.push("\n");+ for (const child of element.childNodes) walk(child);+ if (block && parts.length > 0 && !parts[parts.length - 1]?.endsWith("\n")) parts.push("\n");+ };+ walk(node);+ return parts.join("").replace(/\u00a0/g, " ").replace(/\n{3,}/g, "\n\n").trim();};const isPhoto = (src: string) => {@@ -71,14 +160,7 @@}};- try {- const marked = document.querySelector('article[data-testid="tweet"][data-edgeever-tweet-target="1"]');- const article = marked ?? articleFromStatus();- if (!article) {- finish({ ok: false, reason: root.__edgeeverTweetTarget ? "not-found" : "needs-listener" });- return;- }-+ const readArticle = (article: Element) => {const names = [...article.querySelectorAll('[data-testid="User-Name"]')];const mainName = names.find((node) => !node.closest('[role="link"]')) ?? names[0] ?? null;const quoteName = names.find((node) => node !== mainName && Boolean(node.closest('[role="link"]'))) ?? null;@@ -98,32 +180,44 @@images.push({ url: src, alt: clean(img.alt) });if (images.length >= 6) break;}-- const visibleText = (node: Element | null) => {- if (!node) return "";- return ((node as HTMLElement).innerText || node.textContent || "").trim();+ return {+ author,+ quoted,+ text: visibleText(mainText),+ quotedText: visibleText(quoteText),+ datetime: ownTime(article)?.getAttribute("datetime") ?? "",+ statusUrl: absoluteStatus(ownStatusHref(article)),+ images,};- const text = visibleText(mainText);- const quotedText = visibleText(quoteText);- const statusUrl = absoluteStatus(ownStatusHref(article));- if (!author.displayName && !author.handle && !text && !quotedText && images.length === 0) {+ };++ void (async () => {+ try {+ const initial = findArticle(statusId);+ if (!initial) {+ finish({ ok: false, reason: root.__edgeeverTweetTarget ? "not-found" : "needs-listener" });+ return;+ }+ const article = await expandTruncatedText(initial);+ const read = readArticle(article);+ if (!read.author.displayName && !read.author.handle && !read.text && !read.quotedText && read.images.length === 0) {+ finish({ ok: false, reason: "not-found" });+ return;+ }+ finish({+ ok: true,+ displayName: read.author.displayName,+ handle: read.author.handle,+ text: read.text,+ quotedDisplayName: read.quoted.displayName,+ quotedHandle: read.quoted.handle,+ quotedText: read.quotedText,+ datetime: read.datetime,+ statusUrl: read.statusUrl,+ images: read.images,+ });+ } catch {finish({ ok: false, reason: "not-found" });- return;}-- finish({- ok: true,- displayName: author.displayName,- handle: author.handle,- text,- quotedDisplayName: quoted.displayName,- quotedHandle: quoted.handle,- quotedText,- datetime: mainName?.querySelector("time")?.getAttribute("datetime") ?? "",- statusUrl,- images,- });- } catch {- finish({ ok: false, reason: "not-found" });- }+ })();})();diff --git a/apps/extension/src/tweet-clip.ts b/apps/extension/src/tweet-clip.tsindex 9641e020..8be093fb 100644--- a/apps/extension/src/tweet-clip.ts+++ b/apps/extension/src/tweet-clip.ts@@ -84,6 +84,16 @@ export const cleanTweetText = (value: string) => {return lines.join("\n").trim();};+// Quote cards on X put the name and @handle in plain text, not in links.+// Keep the body of this function identical to visibleTweetAuthor in capture-tweet.ts.+export const visibleTweetAuthor = (value: string) => {+ const parts = value.replace(/\r/g, "").split("\n").map((part) => part.trim()).filter(Boolean);+ const handlePart = parts.find((part) => /^@[A-Za-z0-9_]{1,15}$/.test(part));+ const handle = handlePart ? handlePart.slice(1) : "";+ const displayName = parts.find((part) => part !== handlePart && part !== "·" && part !== "•") ?? "";+ return { displayName, handle };+};+export const tweetAuthorLine = (displayName: string, handle: string) => {const name = displayName.replace(/\s+/g, " ").trim();const user = handle.replace(/^@/, "").replace(/\s+/g, "").trim();diff --git a/apps/extension/tweet-clip.test.mjs b/apps/extension/tweet-clip.test.mjsindex 3a22b184..ccb84033 100644--- a/apps/extension/tweet-clip.test.mjs+++ b/apps/extension/tweet-clip.test.mjs@@ -1,3 +1,4 @@+import { readFileSync } from "node:fs";import { describe, expect, test } from "bun:test";import {canonicalStatusUrl,@@ -8,6 +9,7 @@ import {tweetAuthorLine,tweetNoteMarkdown,tweetNoteTitle,+ visibleTweetAuthor,} from "./src/tweet-clip.ts";const labels = {@@ -58,6 +60,18 @@ describe("tweet note content", () => {expect(markdown).not.toContain("pbs.twimg.com");});+ test("reads a quote author from the visible name block when it has no links", () => {+ expect(visibleTweetAuthor("yetone\n@yetone\n·\n9月25日")).toEqual({ displayName: "yetone", handle: "yetone" });+ expect(visibleTweetAuthor("Geek\n@geekbb\n·\n2小时")).toEqual({ displayName: "Geek", handle: "geekbb" });+ expect(visibleTweetAuthor("")).toEqual({ displayName: "", handle: "" });+ const page = readFileSync(new URL("./src/capture-tweet.ts", import.meta.url), "utf8");+ const library = readFileSync(new URL("./src/tweet-clip.ts", import.meta.url), "utf8");+ const parser = library.match(/const visibleTweetAuthor = \(value: string\) => \{[\s\S]*?\n\};/);+ const compact = (source) => source.replace(/^[ \t]+/gm, "");+ expect(parser?.[0]).toBeTruthy();+ expect(compact(page)).toContain(compact(parser[0]));+ });+test("accepts post photos and skips avatars, emoji, and video posters", () => {expect(isTweetPhotoUrl("https://pbs.twimg.com/media/Abc?format=jpg&name=small")).toBe(true);expect(isTweetPhotoUrl("https://pbs.twimg.com/card_img/Abc/img")).toBe(true);