Opening tabs remotely in Google Chrome
February 24th, 2010
Now that I use Google Chrome almost exclusively, I miss the fact that a running Firefox instance could be controlled from the command line so that Emacs could call for a new tab when I clicked on a URL. It would run a command something like this:
firefox -remote 'openURL(http://example.com/, new-tab)'
But after a few months of manually cutting and pasting URLs into Chrome — which wasn't actually that bad, since the address bar in Chrome is such a convenient and large target — I decided that I needed a real solution. After not finding anything like a -remote option, I discovered that Chrome can at least be run with a debugging port open:
google-chrome --remote-shell-port=9222
The protocol that Chrome speaks is primitive enough that it was quick work to implement a small client in Python. Rather than merely cutting and pasting its code here on my blog, or even be satisfied with making it available on bitbucket, I decided to place the code inside of a new Python package and make it generally available on PyPI as chrome_remote_shell.
Thanks to this simple package, a four-line program (not counting the shebang and comment) is now all that I need to ask Google Chrome to open a new tab:
#!/usr/bin/env python # Name this file "google-chrome-open-url" import sys import chrome_remote_shell shell = chrome_remote_shell.open() shell.open_url(sys.argv[-1])
To teach Emacs to start using Google Chrome when I clicked on a link, I only needed to supply it with two new settings:
(setq browse-url-browser-function
'browse-url-generic)
(setq browse-url-generic-program
"google-chrome-open-url")
And now everything works. I hope that these notes prove useful to someone else. Enjoy!
Posted:
Wednesday, February 24th, 2010 at 11:07 pm
Categories: Computing, Emacs, Python
You can leave a response, or trackback from your own site.
I use the closed-source Chromium rather than the Google-branded version. When I run ‘chromium-browser $url’ in a shell it opens that URL in a new tab in my existing window.
February 25th, 2010 at 4:47 amI meant *open*-source Chromium, of course. It’s too early in the morning. I need coffee!
February 25th, 2010 at 4:48 amMarius —
Well, drat, the open-source version already does what I wanted? :-)
I had looked at the Chromium project, but the only package for Ubuntu that I found for it was a nightly build that would have been wanting to update my machine all of the time. Google, by contrast, had a stable .deb of Chrome that I could install once and not be hammered by upgrades.
Is there an alternative to the Chromium nightly .deb that would not involve my having to compile the package for myself?
February 25th, 2010 at 9:15 amI don’t know how to launch Chrome from the command line on mac osx, but with python’s webbrowser.open(“http://example.com”) (or any application that open Google Chrome), the page appears in a new tab.
February 25th, 2010 at 4:16 pmDamien — Wow! Where did that Python module come from? Obviously I need to be watching the standard library more closely; that might come in handy some day.
But, it won’t work in my current case because of a complication that I omitted: the Emacs process with which I read my email actually runs over on my email server — a separate machine — and running “webbrowser.open()” over there (I just tried) opens a new browser window over on that machine. So, for my actual use case, my solution above still stands: I just run ssh with a “-R” option, and Emacs over on my server can talk back through the port forward to tell my local Chrome to open a page.
February 25th, 2010 at 4:24 pmHere’s the chromium beta PPA for ubuntu you’re looking for:
https://launchpad.net/~chromium-daily/+archive/beta
March 8th, 2010 at 1:48 pmI’d like to use Chrome in the –kiosk mode for homeautomation touchscreens. But the problem with Chrome is that with any of the above solutions it always opens a new tab. With a wall mounted touch PC being given remote commands to open URL’s, that would over time have the potential of opening many tabs and wasting resources.
Is there a way to specify to Chrome to open in the same tab or window?
March 26th, 2010 at 11:49 amRick —
If you look at the open_url() function in my package, you will see that it is a simple wrapper around the evaluate_javascript debugging command. It selects the 0th (“first”, counting from zero) Chrome tab and runs the JavaScript:
window.open('http://.../', '_blank');I think it’s the string ‘_blank’ that tells JavaScript to open a new tab. If you will write your own version of open_url() that calls the JavaScript open() method differently, then I think you can ask Chrome to open a URL in the current tab rather than another one (maybe by just omitting the second argument, but I cannot quite remember).
March 26th, 2010 at 12:27 pmThanks for the quick reply Brandon.
I found that simply changing the ‘_blank’ to ‘_self’ does
the trick, always the same tab!
Appreciate the help.
March 26th, 2010 at 12:44 pmYour debugging thing scared me. Fortunately, I found that I needed only set browse-url-generic to google-chrome and browse-url-browser-function to browse-url-generic. See http://learn.occ.utk.edu/emacs-chrome for a longer version of this comment.
April 15th, 2010 at 4:12 pmJay —
Yes! If you are dealing with the more common case of an Emacs and Chrome running on the same machine, then there are more standard Emacs settings for making the connection between them. In my case they are on separate computers, so the TCP connection is the only way to get them talking.
April 15th, 2010 at 10:07 pm