A list of display styles only, not custom styles that don't apply
I tried many things to get the styles applied to the element, but came up with an empty one.
Please do not quote getComputedStyleas a solution if you cannot solve the problem of returning spam.
The main problem is that it window.getComputedStyle(document.querySelector('ANY ELEMENT')).fillwill return "rgb(0, 0, 0)", which is not the right style in almost all cases and does not have a visible recognition method if it is really applied or not.
The above example is not the only problematic case; There are many rules returned getComputedStylethat are incorrect and will radically change the appearance of the page if they apply.
Static parsing is not an option, as there are cases where .css files are on another server without cross-origin headers; which also hides styles commonly found in document.styleSheets.
Is there a way to get a list of applicable styles and nothing more?
As requested, this code will demonstrate the problem (in Chrome):
var all = document.getElementsByTagName('*');
for(var i in all)
if (all[i].style) all[i].style.cssText = window.getComputedStyle(all[i]).cssText;
EDIT: my answer has code that works in all browsers. I save above to keep the stream of comments.
source
share