Hack #42. Make External Stylesheets Clickable

Ever want to see a page's stylesheets? Stop digging through source code to find them.

Have you ever seen a standards-based site that used CSS in an innovative way, and you asked yourself, "How did they do that?" Then you had to view source, scan through all those angle brackets, find the link to the stylesheet, and load it manually in your browser. Make it easier on yourself! This hack adds a navigation bar along the top of each page with links to each of the page's stylesheets.

The Code

This user script runs on all web pages. It relies on the fact that Firefox maintains a global list of stylesheets, document.styleSheets (note the camelCase capitalization).

There is just one problem: if the page defines additional styles inline, such as with a <style> element in the <head> of the page, or in a style attribute on one specific element, Firefox creates a separate entry for each style in the document.styleSheets list, using the page's URL as the address of the stylesheet (which is technically true, but unhelpful for our purposes). As we loop through document.styleSheets, we need to check for this condition and filter out stylesheets that point back to the current page.

Save the following user script as showstylesheets.user.js:

 // ==UserScript== // @name Show Stylesheets // @namespace http://diveintomark.org/projects/greasemonkey/ // @description adds links to all of page's stylesheets // @include http://* // @include https://* // ==/UserScript== ...

Get Greasemonkey Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.