23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Helium is a tool for discovering unused CSS across many pages on a web site.
2 |
3 | The tool is javascript-based and runs from the browser.
4 |
5 | Helium accepts a list of URLs for different sections of a site then loads and parses each page to build up a list of all stylesheets. It then visits each page in the URL list and checks if the selectors found in the stylesheets are used on the pages. Finally, it generates a report that details each stylesheet and the selectors that were not found to be used on any of the given pages.
6 |
7 |
8 |
9 |
10 | ##### Note: You really should only run Helium on a local, development, or otherwise privately accessible version of your site. If you run this on your public site, every visitor will see the Helium test environment.
11 |
12 | ##### PLEASE READ THE "IMPORTANT STUFF" SECTION BELOW!!
13 |
14 | ### Installation
15 |
16 | 1. Add a script element somewhere on your site that is loaded into every page that will be tested. This is typically a header or footer section. The element looks like:
17 |
18 | ```html
19 |
20 | ```
21 | ##### Note: path/to/helium.js needs to reflect the path of where you place the javascript file.
22 |
23 | If you would like to use a CDN hosted version of Helium, checkout https://cdnjs.com/libraries/helium-css.
24 |
25 | 2. Helium is initiated by calling the method "helium.init()". This has to be placed somewhere on the page where it gets called after page load. An example of this is:
26 |
27 | ```html
28 |
35 | ```
36 | ##### Note: Depending on the javascript loading strategy your site employs, you may wish to place "helium.init()" within a location that executes javascript after page load.
37 |
38 |
39 | ### Usage
40 |
41 | 1. Once Helium is setup, when you load your site you will see a box with a textarea where you input your URL list.
42 |
43 | 2. After you paste your list of links, click Start (lower left) to begin the process. Clicking "Reset to Beginning" clears the textarea and stored data.
44 |
45 | 3. The test will proceed to load and process each url you gave. When it is finished, you are presented with a report window that lists each stylesheet URL that was detected. Under each stylesheet, it will list the CSS selectors that were not detected to be in use on any page.
46 |
47 | 4. The selectors are color-coded.
48 |
49 | * ##### Green: Unmatched selectors.
50 | These are the primary ones that were not detected as in-use.
51 |
52 | * ##### Black: Matched selectors that are grouped with non-matched selectors.
53 | Basically this means that multiple selectors were defined together like "h1,h2,h3{}". All selectors are tested individually so these are displayed to make them easier to find in the stylesheets later.
54 |
55 | * ##### Red: Malformed selectors.
56 | These are likely to be rare. This means that when the browser tried testing for a selector, it can't parse the syntax of how it was written. This could be like ".classname# idname{}" or a "CSS hack" often used for Internet Explorer.
57 |
58 | * ##### Blue: Pseudo-class selector.
59 | These are selectors like ".div:hover" or "input:focus". These are selectors that require user interaction to activate. Currently, Helium can't simulate the interactions required to see if these are found or not. It is the developer's responsibility to test for these manually.
60 |
61 | ### Browser Support:
62 |
63 | Any modern browser that supports LocalStorage and document.querySelector.
64 |
65 | I have decided I will never adapt Helium to support IE6 or 7.
66 |
67 | ### IMPORTANT STUFF:
68 | 1. No cross-domain stylesheets: Helium has to load the stylesheets on your site via XHR in order to parse out the selectors to test. This means that all stylesheets URLs have to be on the same domain as the pages being tested. There's currently no back-end server to proxy requests, but this might be an option in the future.
69 |
70 | 2. No javascript errors on your pages: If Helium is run on a page that has one or more javascript errors, it can easily prevent Helium and other scripts from running on the page. This will stop your tests dead in their tracks. Verify ahead of time that all of the URLs you are testing do not generate any javascript errors. If you aren't sure, try running some Helium tests and see what page it stops at. Check out your error consoles on such pages.
71 |
72 | 3. No sitemap XML support: Right now, the URL list has to be line separated. No CSV or sitemap XML format is currently supported, though it will be in a future release.
73 |
74 | ### Running the tests:
75 | Helium doesn't have actual unit tests yet. I've been meaning to add them for a while (years) and will soon.
76 |
77 | To run the test pages, run a local server from the project root directory and hit http://localhost/test/test1.html.
78 |
79 | For a very simple server example using Ruby (because its on most systems):
80 |
81 | ruby -run -e httpd . -p 5000
82 |
--------------------------------------------------------------------------------
/helium.js:
--------------------------------------------------------------------------------
1 | /* global alert, ActiveXObject */
2 | // Helium-css v1.1
3 | // License: MIT License(http://opensource.org/licenses/mit-license.php)
4 | // Copyright 2010 - 2014, Charles Lawrence http://twitter.com/geuis
5 | // Release: 1/13/10
6 | // Last update: 8/29/2014
7 |
8 | var helium = {
9 |
10 | callback: undefined,
11 |
12 | data: {
13 | //timeout
14 | //status,
15 | //findinglist,
16 | //pagelist,
17 | //currenturl,
18 | //pages,
19 | //stylesheets
20 | },
21 |
22 | init: function () {
23 |
24 | // Callback on init or default to running the report.
25 | helium.callback = (arguments.length > 0) ? arguments[0] : helium.report;
26 |
27 | //silently fail if localStorage is not available
28 | if (window.localStorage) {
29 |
30 | //load page data
31 | helium.load();
32 |
33 | helium.data.timeout = 3000;
34 | helium.save();
35 |
36 | helium.checkstatus();
37 |
38 | } else {
39 | throw new Error('localStorage API not found');
40 | }
41 |
42 | },
43 |
44 | checkstatus: function () {
45 | //determine state
46 | //0: not started
47 | //1: finding stylesheets on all pages
48 | //2: loading/parsing stylesheets
49 | //3: check if selectors found on pages
50 | //4: finished, show report
51 |
52 | if (typeof helium.data.status === 'undefined') {
53 | helium.data.status = 0;
54 | }
55 |
56 | if (helium.data.status === 0) {
57 | //not started
58 |
59 | //prompt for list of pages. Build and display html overlay
60 | var html = [
61 | '
Paste a list of pages on your site you want to test
',
62 | '',
63 | ''
64 | ];
65 |
66 | helium.generateCSS();
67 |
68 | var div = document.createElement('div');
69 | div.id = 'cssdetectID';
70 | div.innerHTML = html.join('');
71 |
72 | helium.$('body')[0].appendChild(div);
73 |
74 | //add listener to save list and start processing
75 | helium.on(helium.$('#cssdetectStart'), 'click', function () {
76 |
77 | //currently based on new-line separated values. Eventually supports sitemap XML format and comma-delineated.
78 | var list = document.getElementById('cssdetectTextarea').value.split('\n');
79 |
80 | helium.setPageList(list);
81 |
82 | //set status to 'finding'
83 | helium.data.status = 1;
84 |
85 | //copy pagelist
86 | helium.data.findinglist = helium.data.pagelist.slice(0); //copy not reference
87 | helium.save();
88 |
89 | //navigate to first page
90 | helium.nav(helium.data.findinglist);
91 |
92 | }, false);
93 |
94 | //add listener to reset all data
95 | helium.on(helium.$('#cssdetectRestart'), 'click', function () {
96 |
97 | //clear stored pages
98 | document.getElementById('cssdetectTextarea').value = '';
99 |
100 | helium.reset();
101 |
102 | }, false);
103 |
104 | return false;
105 | }
106 |
107 | if (helium.data.status === 1) {
108 |
109 | //finding stylesheets
110 | helium.findstylesheets();
111 |
112 | return false;
113 | }
114 |
115 | if (helium.data.status === 2) {
116 | //loading & parsing stylesheets
117 | helium.getcss();
118 |
119 | return false;
120 | }
121 |
122 | if (helium.data.status === 3) {
123 |
124 | //check if selectors found on pages
125 | helium.checkcss();
126 |
127 | return false;
128 | }
129 |
130 | if (helium.data.status === 4) {
131 |
132 | //Finished, issue report
133 | helium.callback();
134 | }
135 |
136 | },
137 |
138 | //display final report for unused selectors
139 | report: function () {
140 |
141 | var flip = false;
142 | var html = [
143 | '
Black: matched selectors that are defined with non-matched selectors
',
149 | '
Red: a malformed selector. ** Note: not all malformed selectors are bad. Chrome won\'t parse -moz extensions for example.
',
150 | '
Blue: a selector with a pseudo-class. You must test these manually.
',
151 | '
'
152 | ];
153 | var download_report = [
154 | '{G} Green: unmatched selectors',
155 | '{B} Black: matched selectors that are defined with non-matched selectors',
156 | '{R} Red: a malformed selector. ** Note: not all malformed selectors are bad. Chrome won\'t parse -moz extensions for example.',
157 | '{BL} Blue: a selector with a pseudo-class. You must test these manually.'
158 | ];
159 |
160 | //loop through stylesheets
161 | for (var i = 0; i < helium.data.stylesheets.length; i++) {
162 |
163 | //add stylesheet link
164 | html.push('