20 lines of code implementation of a CSS coverage test script _javascript Tips

Source: Internet
Author: User

Document.stylesheets holds a collection of all the CSS rules on the current page. It allows you to traverse all selector defined in the page <style>, and access the matching rules for the Selectortext property to the selector. The rule rule is then passed to the Document.queryselectorall to get a list of elements within the page that match this rule.
Here we just ask for the coverage of the CSS rules, so we can access Queryselectorall (). length. You can see each CSS usage by sorting.
The code is simple.

Copy Code code as follows:

var usage = [];
var sheets = document.stylesheets;

for (var i = sheets.length-1 i!=-1; i--) {
var rules = Sheets[i].rules;

for (var j = rules.length-1 J!=-1; j--) {
var rule = rules[j];
var text = Rule.selectortext;

Usage.push ({name:text, count:document.querySelectorAll (text). length});
}
}
Usage.sort (function (A, b) {return a.count-b.count});

for (var i = usage.length-1 i!=-1; i--) {
Console.log ("selector:" + usage[i].name + "\n\t Match number:" + usage[i].count);
}

Exhale the F12, stick the code in the console and enter.

Of course, due to permissions issues, the external import of CSS can not be accessed in any case, for the time being not considered. As for not supporting the stylesheets of the broken ie, you can consider using expression or BEHAVIOUR.HTC, another day to try.
Pure JS implementation on this. In the future with the local program to achieve external CSS analysis.

By the way, put a test result:

Which CSS is useless to a glance:

Of course, a 0 match doesn't mean it's useless. The most typical example is: hover, only the mouse to move up to match. There are also through classname control, [attr=], #动态ID, dynamic elements .... And so on and so on the style is not easy to match to.

So the meaning of the above code is not large, and the current mainstream browsers are built-in profiles function, and can be real-time tracking selector matching element number, so do an IE version of only some significance:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.