From e700fcdfa077824c4da1c0239489d12a9a2c02b6 Mon Sep 17 00:00:00 2001 From: cho Date: Sun, 10 Mar 2024 05:20:02 -0700 Subject: [PATCH] deduplicate image by content sans metadata --- web/js/imageFeed.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/web/js/imageFeed.js b/web/js/imageFeed.js index c830d76..4f71632 100644 --- a/web/js/imageFeed.js +++ b/web/js/imageFeed.js @@ -456,6 +456,30 @@ app.registerExtension({ // NOOP: image is a duplicate } else { seenImages.set(fingerprint, true); + + // deduplicate by ignoring images with the same content + let img = $el("img", { src: href }) + img.onload = () => { + // redraw the image onto a canvas to strip metadata + var imgCanvas = document.createElement("canvas"); + imgCanvas.width = img.width; + imgCanvas.height = img.height; + + var imgContext = imgCanvas.getContext("2d"); + imgContext.drawImage(img, 0, 0, img.width, img.height); + + var strippedImgB64 = imgCanvas.toDataURL("image/png"); + if (seenImages[strippedImgB64]) { + // NOOP: image is a duplicate + } else { + addImageToFeed(href); + seenImages[strippedImgB64] = true; + } + } + img.onerror = () => { + // fall back to default behavior + addImageToFeed(href); + } } } else { addImageToFeed(href);