Categories

Request Script

Donate

An Academic Approach to Optimizing CSS

Recently, I finished my undergraduate honors thesis in computer science entitled, “Applying Formal Concept Analysis to Cascading Style Sheets.” It’s a successful attempt at optimizing CSS using a technique called Formal Concept Analysis. Note that this is not a minifier! This algorithm analyzes the structure of the code and makes optimizations on the structure, not just taking out white space and other syntactic “fluff.”

From a very high level, the algorithm rearranges and restructures selector-declaration pairs into different rule blocks and groups selectors and declarations in different ways. Unfortunately, specificity can get nuked sometimes, so the results should be tested to make sure they are still correct. Also, there are multiple ways to run the optimization, each of which produce very different results, so some thought and analysis is still required when using the algorithm to determine which results work best for the particular input.

One noteworthy result is that I ran the algorithm on a very large (150+ kb) file from a real website and the file was reduced by about 20%. On the other hand, optimizing some CSS files with certain options can also increase the file size. Again, additional analysis is needed both before and after the algorithm is run to get ideal results.

Downloads

Paper: Download PDF

Slides for Talk: View (Note that the slides are web-based, so positioning for some diagrams can be a bit off)

This paper really just skims the surface, and if I were to continue on to graduate school, I would definitely have plenty of room to expand and improve the methods I used for this research.

Soon I’ll get around to making the Java implementation I created public and create a nice little interface for running the algorithm on user input. Ideally, this tool could be used in addition to minification to “compile” CSS, just as JavaScript can be minified and obfuscated to produce smaller file sizes.

Grooveshark, Without Ads

Internet radio is becoming more and more popular, and one of the more popular ones is Grooveshark. These free websites are ad-supported, but sometimes you want to maximize your screen real estate and only see the radio player UI. This is a quick tutorial of how to hide the ads on the right side of the screen on Grooveshark.

Update 6/20/2010: Grooveshark seems to have made some updates and the original script does not work. I’ve updated my script to reflect their changes and it works again.

Below is what Grooveshark looks like by default. I’ve highlighted the area on the right dedicated to ads.

Standard Grooveshark
Standard Grooveshark

We are going to hide the ads by running a bit of Javascript to manipulate the page’s DOM. The script we are going to run is:

javascript:function a(){document.getElementById('sidebar').style.display = 'none'; document.getElementById('mainContentWrapper').style.marginRight = '0px';}; a();

Depending on your screen size, the above code may be on multiple lines, but for the script to work, it needs to be one single line of code. Basically what it does is hide the div which contains the ads and then sets the right margin of the div which contains the main content to zero, making it take up the entire width of the screen.

How can you run the above Javascript on the page you ask? All modern browsers allow you to run Javascript directly from the address bar. On the page you are wanting to run it on, just replace the URL with the Javascript you wish to run, and press enter.

Running Javascript From the Address Bar
Running Javascript From the Address Bar

So for this particular example, these are the steps to start up Grooveshark and hide the ads:

  • Open your browser
  • Navigate to http://listen.grooveshark.com/
  • Copy the Javascript above (make sure it’s all one line! If it’s not, paste it into notepad and take any line breaks out)
  • Paste the Javascript into the address bar (make sure to REPLACE the current URL)
  • Press Enter. You’re done!

You can actually run this Javascript at any point, so if you’ve been listening to Grooveshark for a while and suddenly decide you don’t want the ads anymore, just run the Javascript on the page and presto, they’re gone! There is no need to close the browser/tab and reopen it fresh.

Grooveshark without Ads
Grooveshark without Ads

This concludes the quick tutorial of how to hide the Grooveshark ads. This concept can be used on most websites, so if you are somewhat familiar with Javascript and the DOM, you can use this technique to manipulate any website you want.