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): save tabs without a URL, and escape tab titles
Elian Dorand35d7c9b9a6f
diff --git a/apps/web-clipper/entrypoints/background/index.spec.ts b/apps/web-clipper/entrypoints/background/index.spec.tsindex 82865164c8..40a90e0e99 100644--- a/apps/web-clipper/entrypoints/background/index.spec.ts+++ b/apps/web-clipper/entrypoints/background/index.spec.ts@@ -485,6 +485,28 @@ describe("background", () => {expect(await lastToast()).toMatchObject({ noteId: "saved", tabIds: [ 1, 2, 4, 5 ] });});+ it("skips tabs without a URL and escapes the titles", async () => {+ tabs = [+ { id: 1, title: "Loading" },+ { id: 2, title: "<b>Bold</b> & \"quoted\"", url: "https://a.com/?x=1&y=\"2\"" },+ { id: 3, url: "https://a.com/untitled" }+ ];++ await onCommand("saveTabs");++ expect(lastPayload()).toEqual({+ title: "2 browser tabs: a.com",+ content: "<ul><li><a href=\"https://a.com/?x=1&y="2"\">"+ + "<b>Bold</b> & "quoted"</a></li>"+ + "<li><a href=\"https://a.com/untitled\">https://a.com/untitled</a></li></ul>",+ clipType: "tabs"+ });+ expect(await lastToast()).toMatchObject({+ message: "2 links have been saved to Trilium.",+ tabIds: [ 2, 3 ]+ });+ });+it("adds no ellipsis for up to three tabs and shows why saving failed", async () => {tabs = [{ id: 1, title: "A", url: "https://a.com/" },diff --git a/apps/web-clipper/entrypoints/background/index.ts b/apps/web-clipper/entrypoints/background/index.tsindex 7b7e51f61d..6e9ae3942e 100644--- a/apps/web-clipper/entrypoints/background/index.ts+++ b/apps/web-clipper/entrypoints/background/index.ts@@ -246,7 +246,7 @@ export default defineBackground(() => {if (await postProcessImage(image)) {downloaded.push(image);} else {- payload.content = payload.content?.replaceAll(image.imageId, escapeAttribute(image.src));+ payload.content = payload.content?.replaceAll(image.imageId, escapeHtml(image.src));}}@@ -342,16 +342,16 @@ export default defineBackground(() => {return true;}- async function getTabsPayload(tabs: Browser.tabs.Tab[]) {+ async function getTabsPayload(tabs: (Browser.tabs.Tab & { url: string })[]) {let content = '<ul>';- tabs.forEach(tab => {- content += `<li><a href="${tab.url}">${tab.title}</a></li>`;- });+ for (const tab of tabs) {+ content += `<li><a href="${escapeHtml(tab.url)}">${escapeHtml(tab.title ?? tab.url)}</a></li>`;+ }content += '</ul>';const domainsCount = tabs.map(tab => tab.url).reduce((acc, url) => {- const hostname = new URL(url ?? "").hostname;+ const hostname = new URL(url).hostname;return acc.set(hostname, (acc.get(hostname) || 0) + 1);}, new Map());@@ -371,7 +371,8 @@ export default defineBackground(() => {}async function saveTabs() {- const tabs = await getWindowTabs();+ const tabs = (await getWindowTabs())+ .filter((tab): tab is Browser.tabs.Tab & { url: string } => !!tab.url);const payload = await getTabsPayload(tabs);@@ -485,7 +486,7 @@ function savedMessage(subject: string, failedImages: number) {return `${subject} has been saved to Trilium, but ${images} could not be downloaded.`;}-function escapeAttribute(value: string) {+function escapeHtml(value: string) {return value.replaceAll("&", "&").replaceAll("\"", """)