Chrome and Emacsclient

Posted on Sat 12 December 2009 by alex in geek

I had a number of things planned to do today, one of which was to go pick up some parcels that the local delivery service failed to do. However some workmen started digging up the road opposite my house. As the un-controlled single lane alternating between inbound and outbound traffic is right outside my drive I decided I couldn't face the horror and stayed in to tinker.

I've switched my default browser settings to Chromium on all my machines. It's just so much faster to launch and get around. I've sacrificed the security of NoScript but I'm less worried about Flash on my netbook now as each tab kills any CPU sucking plugins when deleted. The only major problem left is finding an alternative to It's All Text so I don't have to fire up Firefox every time I post something to the web (like this blog entry).

I decided to have a look into what was involved in getting a Chrome extension running that can call an external editor. I checked the group before I started and stumbled on Dave Hilley's rough cut solution. It's almost exactly what I need so I've started toying with it.

Due to the way Chrome works it doesn't seem possible to spawn additional processes from Chrome itself (unless you want to use the NSAPI sledgehammer). Dave's solution is a simple server script that the Chrome extension can make calls to using XMLHttpRequest's. The server is written in Python but my first thought is there is no reason why the local emacs --daemon couldn't terminate the calls directly. Certainly the example echo server on the Emacs Wiki doesn't look too complicated to hack up.

The other thing that could be improved is the detection of text areas. Currently the Javascript just searches the DOM for text areas and selects the first one as a candidate for editing. I'm fairly sure it would be possible to detect either which text area currently has focus or do something more akin to It's All Text and wrap unique <div>'s around each text area and have a edit content button.

Unfortunately I seem to be having trouble getting the Chrome side to work. Trying to set breakpoints in the extension scripts keeps causing the extension to "crash" so it's hard to follow the sequence and tweak it. I also have to wrap my mind around Chrome's distinction between "content" and extension scripts. Still at least it's something to work from. Of course if anyone else wants to jump in with ideas (or even better a pointer to a better piece of emacs server code for handling XMLHttpRequests) then don't let my stumbling hack attempts hold you back :-)