"content": "// allcolors.js\n// https://github.com/bgrins/devtools-snippets\n// Print out CSS colors used in elements on the page.\n\n(function () {\n var allColors = {};\n var props = [\"background-color\", \"color\", \"border-top-color\", \"border-right-color\", \"border-bottom-color\", \"border-left-color\"];\n var skipColors = { \"rgb(0, 0, 0)\": 1, \"rgba(0, 0, 0, 0)\": 1, \"rgb(255, 255, 255)\": 1 };\n\n [].forEach.call(document.querySelectorAll(\"*\"), function (node) {\n var nodeColors = {};\n props.forEach(function (prop) {\n var color = window.getComputedStyle(node, null).getPropertyValue(prop);\n if (color && !skipColors[color]) {\n if (!allColors[color]) {\n allColors[color] = {\n count: 0,\n nodes: []\n };\n }\n if (!nodeColors[color]) {\n allColors[color].count++;\n allColors[color].nodes.push(node);\n }\n nodeColors[color] = true;\n }\n });\n });\n\n var allColorsSorted = [];\n for (var i in allColors) {\n allColorsSorted.push({\n key: i,\n value: allColors[i]\n });\n }\n allColorsSorted = allColorsSorted.sort(function (a, b) {\n return b.value.count - a.value.count;\n });\n\n var nameStyle = \"font-weight:normal;\";\n var countStyle = \"font-weight:bold;\";\n var colorStyle = function (color) {\n return \"background:\" + color + \";color:\" + color + \";border:1px solid #333;\";\n };\n\n console.group(\"All colors used in elements on the page\");\n allColorsSorted.forEach(function (c) {\n console.groupCollapsed(\"%c %c \" + c.key + \" %c(\" + c.value.count + \" times)\",\n colorStyle(c.key), nameStyle, countStyle);\n c.value.nodes.forEach(function (node) {\n console.log(node);\n });\n console.groupEnd();\n });\n console.groupEnd(\"All colors used in elements on the page\");\n})();\n"
0 commit comments