Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Javascript for harvesting computed styles on a page
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