Skip to content

Instantly share code, notes, and snippets.

@wosephjeber
Created October 15, 2020 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wosephjeber/0c87082123526ff4f6c9b8c2e760c25b to your computer and use it in GitHub Desktop.
Save wosephjeber/0c87082123526ff4f6c9b8c2e760c25b to your computer and use it in GitHub Desktop.
Quick and dirty function for scraping colors from web page
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