Skip to content

Commit

Permalink
Fix: compare image info before removing dups
Browse files Browse the repository at this point in the history
  • Loading branch information
eight04 committed Aug 12, 2024
1 parent 75d12d0 commit 2256368
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,40 @@ function downloadImage({url, referrerPolicy = getDefaultReferrerPolicy(), alt})
function getImages() {
const images = new Map;
for (const {src, referrerPolicy, alt} of getAllImages()) {
const url = transformURL(src);
if (images.has(url)) {
continue;
}
images.set(url, {
const url = transformURL(src)
const image = {
url,
referrer: getReferrer(location.href, url, referrerPolicy || getDefaultReferrerPolicy()),
alt
});
};
const old = images.get(image.url);
if (old && cmpInfo(old, image) >= 0) {
continue;
}
images.set(url, image);
}
return [...images.values()];
}

function cmpInfo(a, b) {
return cmp(a.url, b.url) ||
cmp(a.referrer, b.referrer) ||
cmp(a.alt, b.alt);

function cmp(x, y) {
if (x === y) {
return 0;
}
if (x == null) {
return -1;
}
if (y == null) {
return 1;
}
return x < y ? -1 : x > y ? 1 : 0;
}
}

function getEnv() {
return {
pageTitle: document.title,
Expand Down

0 comments on commit 2256368

Please sign in to comment.