Monthly Archives: March 2011

Using jQuery in a Chrome extension

I’ve gotten so used to using jQuery that when suddenly I’m reduced to using vanilla javascript I feel like something is wrong. So when I was trying to write a chrome extension and realized I was going to have to use the basic methods to create elements and traverse the DOM, I felt a little squicked out.

There are basically two places you might want to use jQuery in an extension: within the background page, and within a tab. But in both cases, you’re probably going to want to have a local copy of jQuery, which you can get from jquery.com. You could pull it from a CDN but then you’re depending on your internet connectivity and the CDN.

In a background page

This is the simpler of the two. Just include the javascript file in your background.html (or whatever you’ve named it) file in the same way as you would normally.

<script src='jquery-1.5.1.min.js'></script>

In a tab

If you want to use it in a script that runs on each page, you have to modify your manifest.json file to add it to the content_scripts list:

{
  "name": "My Extension",
  "version": "0.1",
  "content_scripts": [
    {
      "matches": ["http://*/*"],
      "js": ["jquery-1.5.1.min.js", "myScript.js"]
    }
  ],
  "background_page": "background.html"
}

Make sure that you put the jquery file first in the list so it is available to your other javascript files, otherwise you will get errors like “$ does not exist”, which plagued me until I realized this.

Notes:

Chrome Extension Dev Guide

Capistrano keeps asking for password

When deploying with Capistrano, you probably want to do it with a minimum of kicking and screaming. One of the obstacles that lies in your way is the constant requests for passwords which pop up if you haven’t set everything up properly.
* executing `invoke'
  * executing "echo $LOGNAME"
    servers: ["verticallabs.ca"]
Enter passphrase for /home/paul/.ssh/id_rsa:

How do you avoid this? It turns out there are a couple of common problems that cause Cap to keep asking you for your password.

Accessing source control with SSH

Cap has to pull your data out of source control, and it may need to ssh somewhere to get it (check your :repository setting). Even if you’re using the same username to access source control (and you probably are), it still has to ssh from your remote machine to the repo – even though it might be located on the same remote machine. The solution for this is pretty simple: you need to tell Capistrano to use ssh user forwarding.

set :ssh_options, {:forward_agent => true}

You can read about agent forwarding here if you’re not familiar with it.

Missing public key

Even if you have password-less access to the remote machine in question using a private key, you may still be asked for a password because Capistrano requires you to have the public key which matches that private key. (Thanks to thebaldwin for pointing this out) If your ~/.ssh folder only contains the id_rsa, you need to either put the id_rsa.pub in there too, or recreate it like this:

ssh-keygen -y -f ~/.ssh/id_rsa > ~/.ssh/id_rsa.pub

Problems with Bundler and the Ruby 1.9.2 Yaml parser

As pointed out by Cyril Rohr here, there are issues with the default YAML parser for Bundler, Psych. So:
defaults: &defaults
  adapter: yaml
 
development:
  <<: *defaults
 
test:
  <<: *defaults
 
production:
  <<: *defaults

Even though you’d expect the different environments to inherit the defaults they will not, so ‘adapter’ will never be defined. This is because of an issue with merging YAML keys in Psych, apparently.

Cyril suggests using an initializer and switching the parser to Syck, but that didn’t work for me. According to this closed issue on bundler, the correct place to put it is right after the require for bundler (which is in boot.rb).

require 'rubygems'
  
# Set up gems listed in the Gemfile.
gemfile = File.expand_path('../../Gemfile', __FILE__)
begin
  ENV['BUNDLE_GEMFILE'] = gemfile 
  require 'bundler'
    
  STDERR.puts "Temporarily changing yaml engine to syck"
  YAML::ENGINE.yamler = "syck"
  Bundler.setup 
rescue Bundler::GemNotFound => e 
  STDERR.puts e.message
  STDERR.puts "Try running `bundle install`."
  exit!
end if File.exist?(gemfile)

And all is well. Hopefully this Psych issue will be fixed soon!

Update:

Apparently the Psych issue with merge keys has been fixed, but because Psych is a part of the actual Ruby distribution, you either have to build your own Ruby from head, or wait until the next patch of Ruby 1.9.2.