Created
December 30, 2012 18:03
-
-
Save L1fescape/4414186 to your computer and use it in GitHub Desktop.
Copying CSS
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
$.fn.copyCSS = function () { | |
var sheets = document.styleSheets, | |
o = {}, | |
element = $(this[0]); | |
console.log(element) | |
for ( var i in sheets ) { | |
var rules = sheets[i].rules || sheets[i].cssRules; | |
for ( var r in rules ) { | |
if ( element.is(rules[r].selectorText) ) { | |
o = $.extend(o, css2json(rules[r].style), css2json(element.attr('style'))); | |
} | |
} | |
} | |
return o; | |
} | |
function css2json(css){ | |
var s = {}; | |
if ( !css ) return s; | |
if ( css instanceof CSSStyleDeclaration ) { | |
for ( var i in css ) { | |
if( (css[i]).toLowerCase ) { | |
s[(css[i]).toLowerCase()] = (css[css[i]]); | |
} | |
} | |
} | |
else if( typeof css == "string" ) { | |
css = css.split("; "); | |
for ( var i in css ) { | |
var l = css[i].split(": "); | |
s[l[0].toLowerCase()] = (l[1]); | |
}; | |
} | |
return s; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment