- "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 // Should include colors from elements that have a border color but have a zero width?\n var includeBorderColorsWithZeroWidth = false;\n\n var allColors = {};\n var props = [\"background-color\", \"color\", \"border-top-color\", \"border-right-color\", \"border-bottom-color\", \"border-left-color\"];\n var skipColors = {\n \"rgb(0, 0, 0)\": 1,\n \"rgba(0, 0, 0, 0)\": 1,\n \"rgb(255, 255, 255)\": 1\n };\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 thisIsABorderProperty = (prop.indexOf(\"border\") != -1),\n notBorderZero = thisIsABorderProperty ? window.getComputedStyle(node, null).getPropertyValue(prop.replace(\"color\", \"width\")) !== \"0px\" : true,\n colorConditionsMet;\n\n if (includeBorderColorsWithZeroWidth) {\n colorConditionsMet = color && !skipColors[color];\n } else {\n colorConditionsMet = color && !skipColors[color] && notBorderZero;\n }\n\n if (colorConditionsMet) {\n if (!allColors[color]) {\n allColors[color] = {\n count: 0,\n nodes: []\n };\n }\n\n if (!nodeColors[color]) {\n allColors[color].count++;\n allColors[color].nodes.push(node);\n }\n\n nodeColors[color] = true;\n }\n });\n });\n\n function rgbTextToRgbArray(rgbText) {\n return rgbText.replace(/\\s/g, \"\").match(/\\d+,\\d+,\\d+/)[0].split(\",\").map(function(num) {\n return parseInt(num, 10);\n });\n }\n\n function componentToHex(c) {\n var hex = c.toString(16);\n return hex.length == 1 ? \"0\" + hex : hex;\n }\n\n function rgbToHex(rgbArray) {\n var r = rgbArray[0],\n g = rgbArray[1],\n b = rgbArray[2];\n return \"#\" + componentToHex(r) + componentToHex(g) + componentToHex(b);\n }\n\n var allColorsSorted = [];\n for (var i in allColors) {\n var rgbArray = rgbTextToRgbArray(i);\n var hexValue = rgbToHex(rgbArray);\n\n allColorsSorted.push({\n key: i,\n value: allColors[i],\n hexValue: hexValue\n });\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 function colorStyle(color) {\n return \"background:\" + color + \";color:\" + color + \";border:1px solid #333;\";\n };\n\n console.group(\"All colors used in elements on the page: \" + window.location.href);\n allColorsSorted.forEach(function (c) {\n console.groupCollapsed(\"%c %c \" + c.key + \" \" + c.hexValue + \" %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})();\n"
0 commit comments