Skip to content

Build your personal knowledge base with Trilium Notes

fix(clipper): save a selection's lazy images from their real address

Elian Doranc86a3eb028a0
diff --git a/.claude/skills/developing-web-clipper/SKILL.md b/.claude/skills/developing-web-clipper/SKILL.md
index 668aa172b9..66fb341010 100644
--- a/.claude/skills/developing-web-clipper/SKILL.md
+++ b/.claude/skills/developing-web-clipper/SKILL.md
@@ -93,6 +93,10 @@ against older versions of the other.** Prefer changes that need nothing new from
20-character URLs on purpose, so **an image left with its placeholder is a broken image**.
`postProcessImages()` therefore restores the original URL (escaped with `escapeHtml()`) for every
image it cannot download and reports the count in the toast.
+- A selection copies the page's DOM as it is, so an image not yet scrolled into view still has a
+ placeholder (often a `data:image/svg+xml` URL) in `src`. `getImages()` lists it under the web
+ address in `data-src`, `data-original` or `data-lazy-src` (the attributes Defuddle reads), and
+ ignores one that is not `http(s)` or does not parse.
- **A `replaceAll()`/`replace()` replacement built from page data is a callback**
(`replaceAll(id, () => escapeHtml(src))`). A string replacement reads `$&`, `$$`, `` $` `` and
`$'` as patterns, so an image URL containing `$&` came back with the placeholder inside it.
diff --git a/apps/web-clipper/entrypoints/content/index.spec.ts b/apps/web-clipper/entrypoints/content/index.spec.ts
index 2c686ae5b2..c005006d5d 100644
--- a/apps/web-clipper/entrypoints/content/index.spec.ts
+++ b/apps/web-clipper/entrypoints/content/index.spec.ts
@@ -67,6 +67,36 @@ describe("content script", () => {
]);
});
+ it("takes the address a lazy image has not loaded yet from where the page keeps it", async () => {
+ const placeholder = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'%3E%3C/svg%3E";
+ document.body.innerHTML = `
+ <p id="start">Photos</p>
+ <img src="${placeholder}" data-src="/photos/one.jpg">
+ <img data-original="https://cdn.example.com/two.jpg">
+ <img src="${placeholder}" data-lazy-src="three.jpg">
+ <img src="/photos/one.jpg" data-src="/photos/one.jpg">
+ <img src="/loaded.png" data-src="javascript:alert(1)">
+ <img src="/kept.png" data-src="">
+ <img src="/malformed.png" data-src="http://[">
+ <p id="end">End</p>`;
+ selectRange("start", "end");
+
+ const response = await send<Clipping>({ name: "trilium-save-selection" });
+
+ expect(response.images.map((image) => image.src)).toEqual([
+ "https://example.com/photos/one.jpg",
+ "https://cdn.example.com/two.jpg",
+ "https://example.com/blog/three.jpg",
+ "https://example.com/loaded.png",
+ "https://example.com/kept.png",
+ "https://example.com/malformed.png"
+ ]);
+ const container = document.createElement("div");
+ container.innerHTML = response.content;
+ expect(attributes(container, "img", "src")).toEqual(
+ [ 0, 1, 2, 0, 3, 4, 5 ].map((index) => response.images[index]?.imageId));
+ });
+
it("keeps links to schemes other than the web as they are", async () => {
const links = [
diff --git a/apps/web-clipper/utils.ts b/apps/web-clipper/utils.ts
index 3d60d00510..7ffcc8daa6 100644
--- a/apps/web-clipper/utils.ts
+++ b/apps/web-clipper/utils.ts
@@ -86,16 +86,20 @@ export function makeLinksAbsolute(container: HTMLElement) {
}
}
-/** Replaces the `src` of each image in `container` with an `imageId`, the same one for the same image. */
+/**
+ * Replaces the `src` of each image in `container` with an `imageId`, the same one for the same image.
+ * An image waiting to be lazy-loaded is listed under the address the page keeps for later.
+ */
export function getImages(container: HTMLElement) {
const images: ClippedImage[] = [];
for (const img of container.getElementsByTagName('img')) {
- if (!img.src) {
+ const src = lazyImageSource(img) ?? img.src;
+ if (!src) {
continue;
}
- const existingImage = images.find(image => image.src === img.src);
+ const existingImage = images.find(image => image.src === src);
if (existingImage) {
img.src = existingImage.imageId;
@@ -105,7 +109,7 @@ export function getImages(container: HTMLElement) {
images.push({
imageId,
- src: img.src
+ src
});
img.src = imageId;
@@ -114,3 +118,25 @@ export function getImages(container: HTMLElement) {
return images;
}
+
+/** The attributes lazy-loading scripts keep an image's address in, the same ones Defuddle reads. */
+const LAZY_SOURCE_ATTRIBUTES = [ "data-src", "data-original", "data-lazy-src" ];
+
+/** The web address in the first lazy-loading attribute of `img` that holds one. */
+function lazyImageSource(img: HTMLImageElement) {
+ for (const attribute of LAZY_SOURCE_ATTRIBUTES) {
+ const value = img.getAttribute(attribute)?.trim();
+ if (!value) continue;
+
+ let url: URL;
+ try {
+ url = new URL(value, img.baseURI);
+ } catch {
+ continue;
+ }
+ if (url.protocol === "http:" || url.protocol === "https:") {
+ return url.href;
+ }
+ }
+ return undefined;
+}