Skip to content

Instantly share code, notes, and snippets.

@LinzardMac
Created July 9, 2016 01:15
Show Gist options
  • Save LinzardMac/7a7806ea4f4e31bee3921a5c8a8f9f28 to your computer and use it in GitHub Desktop.
Save LinzardMac/7a7806ea4f4e31bee3921a5c8a8f9f28 to your computer and use it in GitHub Desktop.
Chrome Dev Tools Snippets
// list all colors used on the page in CSS
(function () {
// Should include colors from elements that have a border color but have a zero width?
var includeBorderColorsWithZeroWidth = false;
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),
thisIsABorderProperty = (prop.indexOf("border") != -1),
notBorderZero = thisIsABorderProperty ? window.getComputedStyle(node, null).getPropertyValue(prop.replace("color", "width")) !== "0px" : true,
colorConditionsMet;
if (includeBorderColorsWithZeroWidth) {
colorConditionsMet = color && !skipColors[color];
} else {
colorConditionsMet = color && !skipColors[color] && notBorderZero;
}
if (colorConditionsMet) {
if (!allColors[color]) {
allColors[color] = {
count: 0,
nodes: []
};
}
if (!nodeColors[color]) {
allColors[color].count++;
allColors[color].nodes.push(node);
}
nodeColors[color] = true;
}
});
});
function rgbTextToRgbArray(rgbText) {
return rgbText.replace(/\s/g, "").match(/\d+,\d+,\d+/)[0].split(",").map(function(num) {
return parseInt(num, 10);
});
}
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(rgbArray) {
var r = rgbArray[0],
g = rgbArray[1],
b = rgbArray[2];
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
var allColorsSorted = [];
for (var i in allColors) {
var rgbArray = rgbTextToRgbArray(i);
var hexValue = rgbToHex(rgbArray);
allColorsSorted.push({
key: i,
value: allColors[i],
hexValue: hexValue
});
}
allColorsSorted = allColorsSorted.sort(function (a, b) {
return b.value.count - a.value.count;
});
var nameStyle = "font-weight:normal;";
var countStyle = "font-weight:bold;";
function colorStyle(color) {
return "background:" + color + ";color:" + color + ";border:1px solid #333;";
};
console.group("Total colors used in elements on the page: " + window.location.href + " are " + allColorsSorted.length);
allColorsSorted.forEach(function (c) {
console.groupCollapsed("%c %c " + c.key + " " + c.hexValue + " %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");
})();
// print all get values from urls
(function() {
var url = location;
var querystring = location.search.slice(1);
var tab = querystring.split("&").map(function(qs) {
return { "Key": qs.split("=")[0], "Value": qs.split("=")[1], "Pretty Value": decodeURIComponent(qs.split("=")[1]).replace(//g," ") }
});
console.group("Querystring Values");
console.log("URL: "+url+"\nQS: "+querystring);
console.table(tab);
console.groupEnd("Querystring Values");
})();
// reload your css files without reloading the whole page
(function () {
function insertAfter(newElement, targetElement) {
var parent = targetElement.parentNode;
if (parent.lastChild == targetElement) {
parent.appendChild(newElement);
} else {
parent.insertBefore(newElement, targetElement.nextSibling);
}
}
function reloadStyleSheet(stylesheet) {
var element = stylesheet.ownerNode;
var clone = element.cloneNode(false);
clone.href = addRandomToUrl(clone.href);
clone.addEventListener("load", function() {
if (element.parentNode) {
element.parentNode.removeChild(element);
}
});
insertAfter(clone, element);
}
function addRandomToUrl(input) {
// prevent CSS caching
var hasRnd = /([?&])_=[^&]*/,
hasQueryString = /\?/,
hasHash = /(.+)#(.+)/,
hash = null,
rnd = Math.random();
var hashMatches = input.match(hasHash);
if (hashMatches) {
input = hashMatches[1];
hash = hashMatches[2];
}
url = hasRnd.test(input) ?
input.replace(hasRnd, "$1_=" + rnd) :
input + (hasQueryString.test(input) ? "&" : "?") + "_=" + rnd;
if (hash) url += '#' + hash;
return url;
}
[].forEach.call(document.styleSheets, function(styleSheet) {
if (!styleSheet.href) return;
console.log('reload ' + styleSheet.href);
reloadStyleSheet(styleSheet);
});
})();
var xmlhttp = new XMLHttpRequest();
var url = "";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var myArr = JSON.parse(xmlhttp.responseText);
myFunction(myArr);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment