Last active
May 20, 2017 13:21
-
-
Save tuxracer/5061232 to your computer and use it in GitHub Desktop.
Find the prominent images on the page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getProminent = (limit = 25, minPixelCount = 50000) -> | |
imageEls = document.getElementsByTagName 'img' | |
matches = [] | |
sortCollection = (collection, key) -> | |
collection.sort (a, b) -> | |
b[key] - a[key] | |
isInCollection = (collection, key, value) -> | |
collection.some (model) -> | |
model[key] is value | |
storeProminent = (image) -> | |
unless isInCollection(matches, 'src', image.src) or image.pixelCount < minPixelCount | |
matches.push image | |
images = for image in imageEls | |
src: image.src | |
width: image.naturalWidth | |
height: image.naturalHeight | |
pixelCount: image.naturalWidth * image.naturalHeight | |
storeProminent image for image in images | |
sortCollection(matches, 'pixelCount').slice(0,limit) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getProminent(limit = 25, minPixelCount = 50000) { | |
const imageEls = document.getElementsByTagName("img"); | |
const images = []; | |
const sortCollection = (collection, key) => { | |
return collection.sort((a, b) => b[key] - a[key]); | |
}; | |
const isInCollection = (collection, key, value) => { | |
return collection.some((model) => model[key] === value); | |
}; | |
for (let i = 0; i < imageEls.length; i++) { | |
const imageEl = imageEls[i]; | |
const duplicate = isInCollection(images, "src", imageEl.src); | |
const image = { | |
src: imageEl.src, | |
width: imageEl.naturalWidth, | |
height: imageEl.naturalHeight, | |
pixelCount: imageEl.naturalWidth * imageEl.naturalHeight | |
}; | |
if (!duplicate && image.pixelCount > minPixelCount) { | |
images.push(image); | |
} | |
} | |
return sortCollection(images, "pixelCount").slice(0, limit); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment