Skip to content

Instantly share code, notes, and snippets.

@kandy
Created September 10, 2020 03:36
Show Gist options
  • Save kandy/555f1a54418d1155916b19cd840430aa to your computer and use it in GitHub Desktop.
Save kandy/555f1a54418d1155916b19cd840430aa to your computer and use it in GitHub Desktop.
Image deduplicator
(function (blockSelector, itemSelector) {
let list = document.querySelector(blockSelector);
let observer = () => list.querySelectorAll(itemSelector +":not(.imd-hash)")
.forEach(el => {
el.classList.add('imd-hash');
let width = 32;
let height = 32;
let img = new Image();
img.onload = function() {
let canvas = document.createElement("canvas");
canvas.width = width; canvas.height = height;
let context = canvas.getContext('2d');
context.drawImage(img, 0, 0, width, height);
let data = context.getImageData(0, 0, width, height).data;
let bytesMap = new Array(data.length / 4);
for(let i = 0; i < bytesMap.length; i++){
let j = i * 4;
bytesMap[i] = Math.round((data[j] + data[j+1] + data[j+2]) / 3);
}
let average = bytesMap.reduce((i, p) => p + i) / bytesMap.length;
let bit = 0n;
let hash = 0n;
bytesMap.forEach((i) => hash |= (i >= average ? 1n : 0n) << bit ++);
let lhash = 'img-hash-' + hash.toString(32);
const hv = localStorage.getItem(lhash);
console.log(lhash, hv);
if (!hv) {
localStorage.setItem(lhash, 1);
} else {
el.style.filter = "blur(2px) grayscale(50%)";
}
}
img.crossOrigin = "Anonymous";
img.src = el.dataset.thumb
});
let mo = new MutationObserver(observer);
mo.observe(list, {
childList: true,
subtree: true
});
observer();
})("#video_layout_search", ".video_item")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment