Created
February 6, 2017 02:22
-
-
Save bighappyface/22b0a04293354502645ec624a6a28a59 to your computer and use it in GitHub Desktop.
Javascript for harvesting computed styles on a 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
var cssRules = function(){ | |
var filter = Array.prototype.filter; | |
var map = Array.prototype.map; | |
var rules = map.call(document.querySelectorAll('*'), function(element){ | |
return element.ownerDocument.defaultView.getMatchedCSSRules(element,''); | |
}); | |
var items = rules.map(function(list){ | |
list = list || []; | |
return map.call(list, function(rule){ | |
return rule.cssText; | |
}); | |
}); | |
var flattened = items.reduce(function(a, b) { | |
return a.concat(b); | |
}); | |
var rules = flattened.filter(function(value, index, arr){ | |
return arr.indexOf(value) === index; | |
}); | |
var sheets = filter.call(document.styleSheets, function(sheet){ | |
return sheet.rules; | |
}); | |
var _rules = sheets.map(function(sheet){ | |
return filter.call(sheet.rules, function(rule){ | |
// regex for pseudo - /^[\#\.\w\ ]*\:+:?[\#\.\w\-\(\)]*[\,\{\ ]*$/ | |
return rule.cssText.indexOf(':hover') > -1 || | |
rule.cssText.indexOf(':active') > -1 || | |
rule.cssText.indexOf(':focus') > -1 || | |
rule.cssText.indexOf(':visited') > -1 || | |
rule.cssText.indexOf(':link') > -1 || | |
rule.cssText.indexOf(':first-child') > -1 || | |
rule.cssText.indexOf(':nth-child') > -1 || | |
rule.cssText.indexOf(':last-child') > -1 || | |
rule.cssText.indexOf(':before') > -1 || | |
rule.cssText.indexOf(':after') > -1 || | |
rule.cssText.indexOf('::before') > -1 || | |
rule.cssText.indexOf('::after') > -1 || | |
rule.cssText.indexOf(':first-line') > -1 || | |
rule.cssText.indexOf(':first-letter') > -1 || | |
rule.cssText.indexOf('::first-line') > -1 || | |
rule.cssText.indexOf('::first-letter') > -1 || | |
rule.cssText.indexOf('menu') > -1; | |
}); | |
}); | |
_rules = _rules.reduce(function(a, b) { | |
return a.concat(b); | |
}); | |
_rules = _rules.map(function(rule){ | |
return rule.cssText; | |
}); | |
_rules.forEach(function(item){ | |
rules.push(item); | |
}); | |
return rules; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment