Created
August 28, 2013 23:29
-
-
Save kayz1/6372652 to your computer and use it in GitHub Desktop.
Chrome snippet for colors 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
// allcolors.js | |
// https://github.com/bgrins/devtools-snippets | |
// Print out CSS colors used in elements on the page. | |
(function () { | |
var allColors = {}; | |
var props = ["background-color", "color", "border-top-color", "border-right-color", "border-bottom-color", "border-left-color"]; | |
var skipColors = { "rgb(0, 0, 0)": 1, "rgba(0, 0, 0, 0)": 1, "rgb(255, 255, 255)": 1 }; | |
[].forEach.call(document.querySelectorAll("*"), function (node) { | |
var nodeColors = {}; | |
props.forEach(function (prop) { | |
var color = window.getComputedStyle(node, null).getPropertyValue(prop); | |
if (color && !skipColors[color]) { | |
if (!allColors[color]) { | |
allColors[color] = { | |
count: 0, | |
nodes: [] | |
}; | |
} | |
if (!nodeColors[color]) { | |
allColors[color].count++; | |
allColors[color].nodes.push(node); | |
} | |
nodeColors[color] = true; | |
} | |
}); | |
}); | |
var allColorsSorted = []; | |
for (var i in allColors) { | |
allColorsSorted.push({ | |
key: i, | |
value: allColors[i] | |
}); | |
} | |
allColorsSorted = allColorsSorted.sort(function (a, b) { | |
return b.value.count - a.value.count; | |
}); | |
var nameStyle = "font-weight:normal;"; | |
var countStyle = "font-weight:bold;"; | |
var colorStyle = function (color) { | |
return "background:" + color + ";color:" + color + ";border:1px solid #333;"; | |
}; | |
console.group("All colors used in elements on the page"); | |
allColorsSorted.forEach(function (c) { | |
console.groupCollapsed("%c %c " + c.key + " %c(" + c.value.count + " times)", | |
colorStyle(c.key), nameStyle, countStyle); | |
c.value.nodes.forEach(function (node) { | |
console.log(node); | |
}); | |
console.groupEnd(); | |
}); | |
console.groupEnd("All colors used in elements on the page"); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works nice. Thanks.