Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save OlivierPerceboisGarve/469418 to your computer and use it in GitHub Desktop.
Save OlivierPerceboisGarve/469418 to your computer and use it in GitHub Desktop.
(function(s,c,r,i,j){
var out = '', props = [], selector = '';
for(var i=0, ilen=s.length; i<ilen; i++){
c=s[i].cssRules;
for(var j=0,len=c.length; j<len; j++){
r=c[j].cssText;
props.push(r.split('{'));
//console.log('document.styleSheets['+i+'].cssRules['+j+'].cssText = "'+r+'";')
//out+= r+'<br>';
}
}
var results = [], inserted = false;
for (var i=0, len=props.length; i<len; i++){
inserted = false;
for (var j=0; j<results.length;j++){
if (results[j][0] == props[i][1]){
results[j][1].push(props[i][0]);
inserted = true;
}
}
if (!inserted) results.push([props[i][1],[props[i][0]]]);
}
out+= 'number of rules: '+props.length+'<br> compression potential:'+ results.length;
out+= '<table border="1">';
results.sort(function(a,b){
//console.log(a, '-------', b);
return b[1].length-a[1].length;
});
for (var i=0, len=results.length; i<len; i++){
out+= '<tr><td>'+results[i][0]+'</td>';
for (var j=0, jlen=results[i][1].length; j<jlen; j++){
//console.log('j', j);
if (j==0) {
out+= '<td><span style="color: orange;">'+jlen+' rules</span></td><td>';
}
out+= '<span style="color: red;">'+results[i][1][j]+'</span>';
if (j==jlen+1) out+= '</td></tr>';
}
}
out+= '</table>';
var old = document.getElementById('cssCompressor');
if (old) old.parentNode.removeChild(old);
var div = document.createElement('div');
div.innerHTML = out;
div.style.cssText = "background-color: white; position: absolute; top: 0; z-index:1000001; overflow: scrollbar;";
document.body.style.overflow = 'scroll';
document.childNodes[1].style.overflow = 'scroll';
div.setAttribute('id', 'cssCompressor');
document.getElementsByTagName('body')[0].appendChild(div);
})(document.styleSheets);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment