Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(web-clipper): keep the page preview from navigating away
Elian Doran334b3e6e808d
diff --git a/apps/web-clipper/entrypoints/popup/main.tsx b/apps/web-clipper/entrypoints/popup/main.tsxindex a74980cdde..0355a71881 100644--- a/apps/web-clipper/entrypoints/popup/main.tsx+++ b/apps/web-clipper/entrypoints/popup/main.tsx@@ -74,7 +74,7 @@ const PREVIEW_STYLE = `h1 { font-size: 1.3em; } h2 { font-size: 1.15em; } h3, h4, h5, h6 { font-size: 1em; }img, video, svg, iframe { max-width: 100%; height: auto; }pre { white-space: pre-wrap; }- a { color: light-dark(#0076af, #95c3d9); text-decoration: none; pointer-events: none; }+ a { color: light-dark(#0076af, #95c3d9); text-decoration: none; }`;const MODE_LABELS: Record<ClipMode, string> = { selection: "Selection", page: "Page", note: "Bookmark" };@@ -508,6 +508,14 @@ export function previewDocument({ content, images }: Pick<ExtractedPage, "contenimg.setAttribute("src", image.src);}}+ // An empty `sandbox` still lets the frame navigate itself: through a link, which the keyboard+ // can follow, or through a `<meta http-equiv="refresh">`.+ for (const link of doc.querySelectorAll("a[href], area[href]")) {+ link.removeAttribute("href");+ }+ for (const meta of doc.querySelectorAll("meta[http-equiv]")) {+ meta.remove();+ }return `<!DOCTYPE html><html><head><meta charset="utf-8"><style>${PREVIEW_STYLE}</style></head>`+ `<body>${doc.body.innerHTML}</body></html>`;diff --git a/apps/web-clipper/entrypoints/popup/popup.spec.tsx b/apps/web-clipper/entrypoints/popup/popup.spec.tsxindex 30e405db1a..9517a2366c 100644--- a/apps/web-clipper/entrypoints/popup/popup.spec.tsx+++ b/apps/web-clipper/entrypoints/popup/popup.spec.tsx@@ -419,6 +419,20 @@ describe("previewDocument", () => {expect(doc.querySelector("style")?.textContent).toContain("max-width: 100%");});+ it("leaves the frame nothing to navigate to, by mouse, keyboard or refresh", () => {+ const doc = new DOMParser().parseFromString(previewDocument({+ content: `<p>See <a href="https://example.com/next">the next page</a></p>`+ + `<meta http-equiv="refresh" content="0; url=https://example.com/elsewhere">`+ + `<map><area href="https://example.com/area" alt="Area"></map>`,+ images: []+ }), "text/html");+ const link = doc.querySelector("p a");+ expect(link?.textContent).toBe("the next page");+ expect(link?.hasAttribute("href")).toBe(false);+ expect(doc.querySelector("area")?.hasAttribute("href")).toBe(false);+ expect(doc.querySelector("meta[http-equiv]")).toBeNull();+ });+it("colors links with the theme's link colors, which the frame cannot read from the popup", () => {const theme = readFileSync(join(import.meta.dirname, "../../assets/theme.css"), "utf8");const [ light, dark ] = [ ...theme.matchAll(/--link-color: (#[0-9a-f]+);/g) ].map((match) => match[1]);