Skip to content

Instantly share code, notes, and snippets.

@p3t3r67x0 p3t3r67x0/get-colors.js
Last active Feb 1, 2019

Embed
What would you like to do?
var pseudoClassesAndElementsArray = ['', ':hover', ':focus', ':active', ':first-child', ':first-of-type', ':checked', ':disabled', ':empty', ':enabled', ':invalid', ':last-child', ':last-of-type', ':link', ':only-of-type', ':only-child', ':optional', ':out-of-range', ':read-only', ':read-write', ':required', ':root', ':target', ':valid', ':visited', '::after', '::before', '::first-letter', '::first-line', '::selection', '', ];
var colorListArray = [];
function isArray(data) {
var isArrayBoolean = Object.prototype.toString.call(data) == '[object Array]';
return isArrayBoolean;
}
function inArray(needle, haystack) {
var length = haystack.length;
for (var i = 0; i < length; i++) {
if (haystack[i] == needle) {
return true;
}
}
return false;
}
function inStringRegex(stringData) {
var regex = /rgba?\(.*?\)/g;
var outputMatchedArray;
if (!!stringData) {
outputMatchedArray = stringData.match(regex);
if (isArray(outputMatchedArray)) {
outputMatchedArray.forEach(arrayElements);
}
}
}
function arrayElements(element, index, array) {
if (!inArray(element, colorListArray)) {
colorListArray.push(element);
}
}
function callPseudoClassesAndElements(elementValue, index, array) {
var domObject = document.querySelectorAll("*");
//console.log(element);
//runs through all dom elements
[].forEach.call(domObject, function(element) {
var propertyLength = window.getComputedStyle(element, elementValue).length;
for (var i = 0; i < propertyLength; i++) {
var propertyName = window.getComputedStyle(element, elementValue).item(i);
var propertyValue = window.getComputedStyle(element, elementValue).getPropertyValue(propertyName);
//console.log(propertyValue);
if (typeof propertyValue != 'undefined' && !!propertyValue) {
inStringRegex(propertyValue);
}
}
});
}
//init
if (document.querySelectorAll && window.getComputedStyle) {
pseudoClassesAndElementsArray.forEach(callPseudoClassesAndElements);
console.log(colorListArray);
}
else {
console.log("Es trat ein Fehler auf");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.