permalink

108

Auto-saving CSS And JavaScript Changes Locally From The Chrome Developer Tools

 

Today we're going to take a look at how you can greatly simplify how you locally develop for the web.We all have our own workflow when it comes to working on the front-end, but there are some common steps we almost all use:

  • Experiment with changes to styles, scripts or markup using some web developer tools
  • Go back to a text editor to apply those changes and save them
  • Refresh the browser to ensure our changes have been saved
  • Continue debugging

Whilst this workflow works fine, it can be made a lot easier thanks to a new extension for Google Chrome called the Chrome DevTools Autosave (by Nikita Vasilyev).

Chrome DevTools Autosave allows you to make live changes to local CSS and JavaScript files directly from inside the Chrome Developer Tools. Happy with that border-radius you've just tweaked in the browser? Satisfied with that minor JS fix you just tried out? With Autosave, changes you make inside the browser are automatically saved to their corresponding local files. 

This removes the need to constantly switch back and forth between a text-editor and your browser to make such changes and can be a real time-saver.  For all the details on what Autosave is capable of, check out the screencast I've recorded above.

How does it work?

Locally saving to files from Autosave is possible thanks to the onResourceContentCommitted event in Chrome 15+, which fires whenever changes are made to CSS and JavaScript for a page. For those interested, usage of the event itself looks a little like this:

chrome.experimental.devtools.inspectedWindow.onResourceContentCommitted.addListener(function(event) {
    //event.url
    //event.type // 'script', 'stylesheet' or 'document' (happens when you add new CSS rule)
    event.getContent(function(content) {
        //content - all the content of updated file as a string
    });
});

The extension itself relies on a local node server running (which we'll discuss shortly). Changes made from within the Chrome Developer Tools are pushed to the server, which then resolves the local URLs of these files and overwrites the old ones with the new ones.

Note: For those with an earlier version of Chrome, I've been happily using Autosave with the Chrome Canary builds which you can grab from here.

Getting Setup

The installation process for Chrome DevTools Autosave will probably take less than 3-5 minutes.  Assuming you have access to or have installed Chrome 15+, the two setup steps are as follows:

Installing The Extension

  • Open chrome://flags/ and enable Experimental Extension APIs (it won't work without this!)
  • Restart your browser
  • Download the extension and install it

Installing The Server

Autosave uses a local node server to save files. To install, simply complete the following steps:

  1. Install Node.js if you don’t already have it.
  2. Run npm install -g autosave in a terminal/console.

Usage

Run node chrome-devtools-autosave-server/index.js from the command-line and then simply open up a local page you wish to edit. As long as you're accessing it using the file:// scheme, you should be able to easily make changes from inside the Chrome Developer Tools and have them instantly saved to the local CSS or JavaScript copies of those files. It's as simple as that!.

Pro-tips

  • Whenever Autosave updates a local file, a 'Saved a <filetype> to <filename>' notification will be output at the console. You don't need to keep a constant watch for this, but if you'd prefer to avoid refreshing to see if changes have been saved for sure, it's certainly an alternative.
  • Not only does Autosave support what I demonstrated in the screencast, but it can also let you do the following:
    • Adding comments like /*hello world*/
    • Editing multiple-properties at the same time. e.g if you wanted to to add a value for the background-image then add an override for that value if the browser supports CSS3 gradients (the old progressive enhancement trick), this is also supported:  
      background-image: url(some_gradient.png);
      background-image: -webkit-gradient(linear, 0% 100%, 0% 0%, color-stop(0.03, #6E9577), color-stop(0.52, #8FB39B))
    • Update existing JavaScript functions without the need to reload the page at all. Definitely useful.
  • One trick I didn't see documented was this: although your changes may be made to local files instantly, if you go back to the Resources tab after making a change and click on the drop-down arrow next to any modified stylesheets or scripts, you can get a version history of what's you've updated (as long as you haven't refreshed). This isn't specific to this extension, but can come in useful if you accidentally wipe out a bunch of things you'd like to revert back to.

Conclusions

And that's it. I personally found Autosave very useful for working on smaller projects and it does do everything it says on the tin.

If you choose to use it, just keep in mind that although we're typically used to using the developer tools for debugging or experimentation, removing whole rules in your styles (even if just to try something out) will result in them being removed from the local file too.

As long as you're aware of this, it's a really neat tool to have in your utility belt and I'm happy to recommend checking it out. Please help us spread the word if you find it useful.

108 Comments

  1. Hi,
    Thanks for the guide, it’s very useful for me. unfortunately it does not working in my pc os xp ,chrome ver – 22.0.1229.94.

    I implanted the flowing steps:

    1.) Install the extension in Chrome: http://goo.gl/BYwdP
    2.) Install NodeJS: http://goo.gl/3nBP
    3.) Run CMD as Administrator
    4.) Type “npm install -g autosave” (no quotes) and hit [Enter]
    5.) Type “autosave”

    the result in the terminal cmd :
    *****************************************************
    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.

    C:\Documents and Settings\Yossi>autosave
    DevTools Autosave 1.0.0 is running on http://127.0.0.1:9104
    ********************************************************
    6.) Run the nodejs program from Start Button/Programs
    7.) Type this in the nodejs terminal “node chrome-devtools-autosave-server/index.js” and
    hit [Enter]

    the result in the terminal nodejs:
    ********************************************************
    Your environment has been set up for using Node.js 0.8.12 (ia32) and NPM

    C:\Documents and Settings\Yossi>node chrome-devtools-autosave-server/index.js

    module.js:340
    throw err;
    ^
    Error: Cannot find module ‘C:\Documents and Settings\Yossi\chrome-devtools-autos
    ave-server\index.js’
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)

    C:\Documents and Settings\Yossi>
    ********************************************************

    Many thanks for any help!

    • Probably a little too late for you but if someone else haves this problem just execute this in your cmd:

          node "%HOMEPATH%\AppData\Roaming\npm-cache\autosave\1.0.3\package\index.js"
      
  2. Pingback: » Chrome Dev Tools Auto Save - Mike Higginbottom

  3. Pingback: Programmeringsuppslag | Skolnytt

  4. Pingback: Live Edit With Google Chrome Inspect Element – Devtools Autosave | Include Sintax

  5. So, it’s worth noting that Developer Tools will now do this natively via Source Mapping. It will, however, totally throw off your CSS formatting. As well, you have to be careful while you’re live-editing in the browser to make sure your browser doesn’t overwrite code that you’ve written in the file itself. I’ve found myself making the same change as many as 3 times just becuase Chrome has rewritten my styles back to the way they were before. So, if you edit the file, don’t push to your server, and reload the page, you may have obliterated some code.

    Very useful tool, but you have to be aware of it’s weaknesses, limitations, and sharp corners.

Leave a Reply

Required fields are marked *.