Archive for the 'Work' Category

JavaScript Singletons in Firefox Add-ons

Occasionally when developing add-ons for Firefox, you want your JavaScript to only run once for the lifetime of the application. Normally, you would place your code in a browser overlay, but this causes the code to run every time a new window is opened. There are several ways to get around this. You could write an XPCOM service or use a JavaScript module. These are both valid methods and work very well if your code is Firefox specific. However, if you are also writing your add-on for Chrome, you might want the code to be able to run in both places. Here is a nice little hack that uses the hidden window so that you can run your Chrome background page within an iframe in Firefox.


  function getBackgroundPage(id, src) {

    // get the firefox hidden window
    var win = Components.classes["@mozilla.org/appshell/appShellService;1"].
                   getService(Components.interfaces.nsIAppShellService).
                   hiddenDOMWindow;

    // if the iframe was previously loaded store it and callback
    var iframe = win.document.getElementsById(id);
    if (iframe)
      return iframe.contentWindow;

    // create the iframe
    iframe = win.document.createElement("iframe");
    iframe.setAttribute("id", id);
    iframe.setAttribute("style", "display:none;");

    // load the source
    iframe.setAttribute("src", src);
    win.document.documentElement.appendChild(iframe);

    // return the content window
    return iframe.contentWindow;

Now you can access your background page by including the script in your overlay and doing:

  var bgp = getBackgroundPage("id for background page", "url of the background page");

2009 Year in Review

I know I’m a little late getting this out, but I thought I would put together a “Holiday Letter”/”Year in Review” for my family.

This was big year for the family. Back in May we found out we were going to have another child. In August we found out we were having a boy and at the end of December Jackson was born. Both Noah and Wyatt are loving being big brothers. Noah turned four this year and started preschool. He goes three afternoons a week and he is really loving it. Wyatt is also enjoying every minute of his brother being gone. He turned three this year and is getting very big. Rachel continues to become more involved in the community. Along with her deaconess duties, she has also become a member of the Warren County Child Abuse Prevention Council. This year was my fifteen year high school reunion. Planning was much smoother than the ten year reunion and we had a pretty successful turn out.

Even with the recent layoff announcements by AOL, I am still gainfully employed with them and really enjoying my work. KwiClick development continues to go well and has gotten quite a bit of good press recently. Although Forecastfox has slipped slightly in it’s ranking on Firefox, we were able to rewrite the code from scratch this year and get a version out for Chrome (which has been very warmly received). This new code base should allow us to iterate faster and get versions out for Firefox, Fennec, and others. I started the company Web Tech Studios this year to cover the contract work I do. To celebrate the creation of the company I created the "Near Me" add-on and entered it into the Extend Mobile Firefox contest. It was chosen as one of the winners and I received a Nokia N900 as the prize.

Now with three sons and lots of work to do, 2010 looks like it’s going to be a very exhausting happy year.

Web Tech Studios

Published by Rich on December 23, 2009 under Work

After much trial and error the website for my company “Web Tech Studios” is now live. Please, look it over and let me know what you think. You can also follow me on twitter @webtechstudios.

Diamond in the HTML “Rough”

Published by Rich on December 8, 2009 under Work

KwiClick Search CloversFor the latest version of KwiClick we introduced a feature called search clovers. When you highlight a word on a page, a single diamond shaped “clover” appears. When you hover over the diamond, 3 more diamonds appear creating a 4 leaf clover configuration. To create these diamonds we use two html tags, an img surrounded by a div. Clover OverlapIn Firefox versions prior to 3.5 we used a background image on the div to create the look of a the diamond. The corners of the images overlapped each other causing problems when a user tried to hover or click on one of them.

With Firefox 3.5 we were able to fix this by creating the diamond using only html and some specialized css tags. Using the tags: -moz-transform, -moz-border-radius, and -moz-box-shadow we rotate the div 45 degrees and add a shadow to them.

  display: inline-block !important;
  border: 2px solid #494949 !important;
  background: none !important;
  background-color: white !important;
  -moz-transform: rotate(45deg) !important;
  -moz-border-radius: 4px !important;
  -moz-box-shadow: grey 2px 2px 2px !important;

This also rotates the content within the div. To fix that we rotate the image back 45 degrees.

  -moz-transform: rotate(-45deg) !important;

And viola, a diamond with all the html event goodness, made with only html and css. Safari and Chrome support these transforms with -webkit-transform so this same effect can be used there.

Caching Parsed Django Templates

Published by Rich on December 1, 2009 under Work

Standard Django rendering parses a template every time it is rendered. Storing the parsed template can be a nice little speed up for your Django site. This code snippet does a great job of doing that for all templates. One drawback to this approach though is that the server needs restarted whenever a template changes. I’ve taken a slightly different approach to this where I create a separate method for caching and rendering templates. I only call this when rendering several templates in a view and the speed up is absolutely necessary. Some places you might consider doing this is when sending out bulk emails or when rendering a long list of objects and each object uses the same template.

First create a file called template.py in your application. Then we create a method that will cache a template when called and return the rendered template.

from django.template import Context, loader

template_cache = {}
def render_cached(template_name, dictionary=None):
    global template_cache

    t = template_cache.get(template_name, None)
    if not t or settings.DEBUG:
        template_cache[template_name] = t = loader.get_template(template_name)

    dictionary = dictionary or {}
    context_instance = Context(dictionary)
    return t.render(context_instance)

This method takes two arguments the template name and a dictionary to use for the template context. It first gets the template out of the cache. It then looks if the template was retrieved or if your debug setting is on. If either of these cases exist, it uses the base django loader to retrieve the parsed template and store it in the cache dictionary. Then we create a context instance for the dictionary and return the rendered code.

To be able to use the method you just import and call it.

from template import render_cached
html = render_cached("sometemplate.html", {})

KwiClick 2.4.1

Published by Rich on November 29, 2009 under Work

KwiClick 2.4.1 was released on AMO this week. This is a pretty large update from the last AMO release so make sure to check it out. Here is a list of what is new in this release:

Here is the video for the 2.4 release which was submitted to the extend firefox contest. This is a pretty good explanation of the things you can do with KwiClick.

FAIL: libIDL on Mac OS X 10.6 (Snow Leopard)

Published by Rich on October 27, 2009 under Work

Since I upgraded my laptop to Snow Leopard, I’ve been trying to setup my development environment so that I can compile idl files. I need to do this for one of the Firefox extension I work on. I’ve been able to put this off for a while since my interfaces haven’t changed in a while. I’ve recently changed an interface so now I need to get the idl compiling setup. When I’ve set this up in the past, I haven’t had any problems. I install the gecko sdk, install libidl, and I’m ready to roll. This time I can’t make it work so I’m turning to you, the interweb, to help me figure this out. Here is what I’ve go so far.

I install the Gecko 1.9.1 SDK from developer.mozilla.org. I then install libidl using the following commands in MacPorts:

sudo port selfupdate
sudo port sync
sudo port install libidl

Now, when I run my build script I get the error:

     [exec] dyld: Library not loaded: /opt/local/lib/libIDL-2.0.dylib
     [exec]   Referenced from: /Developer/xulrunner-sdk-1.9.1/bin/xpidl
     [exec]   Reason: no suitable image found.  Did find:
     [exec] 	/opt/local/lib/libIDL-2.0.dylib: mach-o, but wrong architecture

Figuring that libidl is compiled as 64 bit and the gecko sdk is 32 bit, I go searching the web. This leads me to a forum post on compiling Camino on Snow Leopard.

Mac OS 10.6 is 64-bit as native, while Camino is far from 64-bit ready. So cross-compiling to 32-bit is required.
First hurdle was to cross-compile and install the required libIDL library, in 32-bit. Luckily MacPorts contain a universal libIDL, so after I got the hint about “versions” I got that going using the command – “sudo port install libIDL +universal”.

Alright, so now I’m trying to install the universal libIDL file and I hit another error.

Command output:       _IDL_inhibit_get in libIDL_2_la-parser.o
      _IDL_queue_new_ident_comment in libIDL_2_la-parser.o
      _IDL_file_set in libIDL_2_la-parser.o
      ___IDL_do_pragma in libIDL_2_la-parser.o
      _IDL_ns_scope_levels_from_here in libIDL_2_la-ns.o
      _IDL_tree_properties_copy in libIDL_2_la-util.o
      _IDL_tree_walk2 in libIDL_2_la-util.o
      _IDL_tree_to_IDL in libIDL_2_la-util.o
      _IDL_tree_get_scope in libIDL_2_la-util.o
      _IDL_tree_remove_inhibits in libIDL_2_la-util.o
      _IDL_tree_property_remove in libIDL_2_la-util.o
      _IDL_tree_property_get in libIDL_2_la-util.o
      _IDL_tree_property_set in libIDL_2_la-util.o
  "_g_log", referenced from:
      _IDL_check_type_cast in libIDL_2_la-util.o
      _IDL_tree_walk_real in libIDL_2_la-util.o
      _IDL_tree_walk_real in libIDL_2_la-util.o
      _IDL_emit_IDL_literal in libIDL_2_la-util.o
      _IDL_tree_get_node_info in libIDL_2_la-util.o
      _IDL_tree_get_scope in libIDL_2_la-util.o
      _IDL_tree_remove_empty_modules in libIDL_2_la-util.o
      _IDL_tree_remove_inhibits in libIDL_2_la-util.o
      _IDL_tree_process_forward_dcls in libIDL_2_la-util.o
      _load_inhibits in libIDL_2_la-util.o
      _IDL_tree_free in libIDL_2_la-util.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make[2]: *** [libIDL-2.la] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2

Here is where I’m stuck. I have no idea what symbols are missing. Please, leave me a comment if you have any ideas on how to make this work.

State of Broadband in Rural Iowa

Published by Rich on August 17, 2009 under Family, Me, Rant, Work

I work full time for AOL, LLC and telecommute to work. Because of this I need to have broadband in my home. We currently live in the smallish town of Indianola, Iowa and we use Mediacom for our broadband access. The current high speed package we have is $50 a month (7MB download, 512KB upload, and no usage cap). I typical average about 300MB up and 2GB down a day. We’ve been in the process of building a new home less than ten miles away, about 5 miles outside of the city limits. I’ve been trying to find out what kind of broadband options are available in the area so I can figure out what carrier we would have for the home office. This has lead me to discover just how abysmal coverage is in rural areas and just how difficult it is to even find out what’s available in my area. Here is what I’ve been able to find out:


View House Locations in a larger map

An interesting side note: I started working on this post over the weekend. OM Malik has two stories today about the state of broadband in the US.

Improving Django Cache – Part III

Published by Rich on August 11, 2009 under Work

We’ve setup a custom cache backend and modified it to fix cache keys. Now, we are going to tackle a much more complicated problem; dogpiling.

Dogpiling occurs when an entry in the cache expires. If you get multiple requests for that cached item before it’s new value can be calculated, you end up with attempts from each of those requests to refresh the cache. To prevent this, we are going to work from the MintCache implementation that stores slightly stale data to pass on to requests while the first request for expired data calculates the new value to store in the cache.
Read more »

Improving Django Cache – Part II

Published by Rich on August 4, 2009 under Work

In part I of this series we setup a custom django cache backend based on the current memcached implementation. For the second installment in this series we are going to start modifying our custom cache backend.

Memcached has a couple of limitations it sets on keys. Currently the length limit of a key is set at 250 characters and the key must not include control characters or whitespace. These limitations can be a problem when dealing with long permalinks or rendered template nodes. First thing we need to do is create a method that will fix keys.
Read more »

Next Page »