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 mailto, tel and other non-web links in clippings
Elian Doran855d2cb50c96
diff --git a/apps/web-clipper/entrypoints/content/index.spec.ts b/apps/web-clipper/entrypoints/content/index.spec.tsindex 6ddc6a6c17..25af1f108d 100644--- a/apps/web-clipper/entrypoints/content/index.spec.ts+++ b/apps/web-clipper/entrypoints/content/index.spec.ts@@ -67,6 +67,25 @@ describe("content script", () => {]);});+ it("keeps links to schemes other than the web as they are", async () => {+ const links = [+ "mailto:[email protected]",+ "tel:+40123",+ "ftp://ftp.example.com/file",+ "magnet:?xt=urn:btih:abc"+ ];+ document.body.innerHTML = `<p id="p">${+ links.map((href) => `<a href="${href}">link</a>`).join(" ")+ }</p>`;+ selectRange("p", "p");++ const response = await send<Clipping>({ name: "trilium-save-selection" });++ const container = document.createElement("div");+ container.innerHTML = response.content;+ expect(attributes(container, "a", "href")).toEqual(links);+ });+it("falls back to the document title and fails without a selection", async () => {document.head.innerHTML = "";document.title = " Fallback ";@@ -91,7 +110,8 @@ describe("content script", () => {<nav><a href="/">Home</a></nav><article><h1>Article title</h1>- <p>${PARAGRAPH.repeat(3)}<a href="/docs">Docs</a></p>+ <p>${PARAGRAPH.repeat(3)}<a href="/docs">Docs</a>+ <a href="mailto:[email protected]">Jane</a></p><p>${PARAGRAPH.repeat(3)}</p><img src="figure.png"><p>${PARAGRAPH.repeat(3)}</p>@@ -110,6 +130,7 @@ describe("content script", () => {} ]});expect(response.content).toContain("href=\"https://example.com/docs\"");+ expect(response.content).toContain("href=\"mailto:[email protected]\"");expect(response.content).not.toContain("Home");expect(document.querySelector("nav")).not.toBeNull();});diff --git a/apps/web-clipper/entrypoints/content/index.ts b/apps/web-clipper/entrypoints/content/index.tsindex e286b3c01c..f33b09b618 100644--- a/apps/web-clipper/entrypoints/content/index.ts+++ b/apps/web-clipper/entrypoints/content/index.ts@@ -1,30 +1,11 @@import Readability from "@/lib/Readability.js";-import { createLink, getBaseUrl, getPageLocationOrigin, randomString, type Rect } from "@/utils.js";+import { createLink, getPageLocationOrigin, randomString, type Rect } from "@/utils.js";export default defineContentScript({matches: ["<all_urls>"],main: () => {- function absoluteUrl(url: string | undefined) {- if (!url) {- return url;- }-- const protocol = url.toLowerCase().split(':')[0] ?? "";- if (['http', 'https', 'file'].indexOf(protocol) >= 0) {- return url;- }-- if (url.indexOf('//') === 0) {- return location.protocol + url;- } else if (url[0] === '/') {- return `${location.protocol}//${location.host}${url}`;- }- return `${getBaseUrl()}/${url}`;-- }-function pageTitle() {const titleElements = document.getElementsByTagName("title");@@ -196,11 +177,11 @@ export default defineContentScript({}function makeLinksAbsolute(container: HTMLElement) {+ // `link.href` reads the URL the browser resolved against the page, and leaves+ // `mailto:`, `tel:` and other non-web schemes unchanged.for (const link of container.getElementsByTagName('a')) {if (link.href) {- const newUrl = absoluteUrl(link.href);- if (!newUrl) continue;- link.href = newUrl;+ link.setAttribute("href", link.href);}}}diff --git a/apps/web-clipper/utils.spec.ts b/apps/web-clipper/utils.spec.tsindex 47ed5aa1df..75aa7fd232 100644--- a/apps/web-clipper/utils.spec.ts+++ b/apps/web-clipper/utils.spec.ts@@ -2,7 +2,7 @@ import type { Window as HappyDOMWindow } from "happy-dom";import { afterEach, describe, expect, it, vi } from "vitest";import { fakeBrowser } from "wxt/testing/fake-browser";-import { createLink, getBaseUrl, getPageLocationOrigin, randomString } from "./utils";+import { createLink, getPageLocationOrigin, randomString } from "./utils";describe("randomString", () => {it("returns alphanumeric strings of the requested length", () => {@@ -12,22 +12,15 @@ describe("randomString", () => {});});-describe("page location", () => {+describe("getPageLocationOrigin", () => {afterEach(() => setUrl("about:blank"));- it("strips the file name from the base URL", () => {+ it("returns the page's origin, and file:// for local files", () => {setUrl("https://example.com:8080/dir/page.html?q=1#hash");expect(getPageLocationOrigin()).toBe("https://example.com:8080");- expect(getBaseUrl()).toBe("https://example.com:8080/dir");- setUrl("https://example.com/dir/");- expect(getBaseUrl()).toBe("https://example.com/dir/");- });-- it("uses file:// as the origin of local files", () => {setUrl("file:///home/user/page.html");expect(getPageLocationOrigin()).toBe("file://");- expect(getBaseUrl()).toBe("file:///home/user");});});diff --git a/apps/web-clipper/utils.ts b/apps/web-clipper/utils.tsindex 7dbbafd038..74fab6c705 100644--- a/apps/web-clipper/utils.ts+++ b/apps/web-clipper/utils.ts@@ -11,18 +11,6 @@ export function randomString(len: number) {return text;}-export function getBaseUrl() {- let output = getPageLocationOrigin() + location.pathname;-- if (output[output.length - 1] !== '/') {- const outputArr = output.split('/');- outputArr.pop();- output = outputArr.join('/');- }-- return output;-}-export function getPageLocationOrigin() {// location.origin normally returns the protocol + domain + port (eg. https://example.com:8080)// but for file:// protocol this is browser dependant and in particular Firefox returns "null" in this case.