Created
October 15, 2020 22:19
-
-
Save wosephjeber/0c87082123526ff4f6c9b8c2e760c25b to your computer and use it in GitHub Desktop.
Quick and dirty function for scraping colors from web 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
function getAllColors() { | |
const COLOR_CONTAINING_PROPERTIES = ["backgroundColor", "borderTopColor", "borderRightColor", "borderLeftColor", "borderBottomColor", "color"] | |
let colors = [] | |
let elements = document.querySelectorAll('*') | |
elements.forEach(element => { | |
const styles = window.getComputedStyle(element) | |
COLOR_CONTAINING_PROPERTIES.forEach(property => { | |
const color = styles[property] | |
if (color && !colors.includes(color)) { | |
colors.push(color) | |
} | |
}) | |
}) | |
const div = document.createElement('div') | |
div.style.position = 'fixed' | |
div.style.left = 0 | |
div.style.top = 0 | |
div.style.backgroundColor = '#fff' | |
div.style.width = '392px' | |
div.style.display = 'flex' | |
div.style.flexWrap = 'wrap' | |
div.style.border = 'solid 1px #000' | |
div.style.zIndex = 1000000 | |
document.body.appendChild(div) | |
colors.forEach(color => { | |
const colorDiv = document.createElement('div') | |
colorDiv.style.width = '30px' | |
colorDiv.style.height = '30px' | |
colorDiv.style.margin = '0' | |
colorDiv.style.backgroundColor = color | |
div.appendChild(colorDiv) | |
}) | |
return colors | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment