All | 43 Folders | Accessibility | BoingBoing | Books | Computer Related | Family | Films | General | Hacking | Hobbies | Humor | Java | Links | Omni | OpenSolaris | Puzzles and Games

« OLPC Order Cancelled | Main | Working GreaseMonkey... »
20080205 Tuesday February 05, 2008

Trying To Convert A GreaseMonkey Script Into A Firefox Extension

To save potential users of my GreaseMonkey scripts having to first install GreaseMonkey, I thought I'd investigate how easy it was to automatically turn them into Firefox extensions.

I came across two potential solutions and failed with both of them.

The first one was written almost three years ago and clearly states on the blog entry that "there's no support yet for the proprietary Greasemonkey JavaScript functions", but I was curious to see what would happen.

I had to change the minimum and maximum Firefox versions supported in the script. From this, running:

% python greasemonkey_compiler.py

creates a Google Suggest Firefox extension which I was able to install and successfully run.

I then wrote a simple script to take one of my existing GreaseMonkey scripts and convert it to a Firefox extension. There is a small configurable section, so it could easily be adjusted to read and convert any GreaseMonkey script. Note that this is a Quick Hack. If I was writing a proper application I'd find a nicer simpler way of getting the user's input parameters.

#!/usr/bin/env python
#
# make_ext.py - v0.1
#
# Copyright (c) 2008 - Rich Burridge - Sun Microsystems Inc.
# All Rights Reserved.
#
# Usage: python make_ext.py
#
# Script to turn a GreaseMonkey script into a Firefox extension.
# It uses the GreaseMonkey compiler written by Adrian Holovaty
# (http://www.holovaty.com).
#
# Compiler source code originally at:
# http://www.letitblog.com/code/python/greasemonkey.py.txt
#

from greasemonkey_compiler import UserScript

# ----- Start of configurable section -------

grease_monkey_script_name = "listsofbests.user.js"
extension_name            = "listofbests.xpi"

creator  = u'Rich Burridge'
homepage = u'http://blogs.sun.con/richb/'
version  = u'0.5'
guid     = u'{4d52f0b8-eaed-4d5f-9c2e-e2ec19510737}'

# ----- End of configurable section -------

if __name__ == "__main__":
    fin = open(grease_monkey_script_name, 'r')
    gm_script = fin.readlines()

    e = UserScript("".join(gm_script), creator, version, guid, homepage)
    f = open(extension_name, 'w')
    e.write_xpi(f)
    f.close()

This happily generated a listofbests.xpi file which I was able to install, but when I went to a sample Lists of Best book list, nothing seemed to happen and Firebug didn't report any problems.

I then removed that extension, restarted Firefox and tried the second one. I pasted in the same listsofbests.user.js script and the online web page generated a listsofbests.xpi extension.

After installing it and restarting Firefox, when I went to the same sample list, an alert dialog came up with:

Error: missing } after function body

Unfortunately I don't know PHP at the moment, so I'm unable to debug it.

Oh well, enough for one day. Back to RealWork™

[]

[]

( Feb 05 2008, 09:37:11 AM PST ) [Listen] Permalink Comments [7]

Comments:

Now you've got some experience with greasemonkey - how would you recommend debugging a greasemonkey script. In particular, I'd like to work out how to get the gmail colouriser working again

Posted by Stu on February 05, 2008 at 10:31 AM PST #

Hi Stu. I use a combination of two things:

1/ A load of gm_log(...) calls whilst I initially
debugging it.

2/ Firebug extension (see link in the post today).
It's just great.

Posted by Rich Burridge on February 05, 2008 at 10:42 AM PST #

When working with Firebug, it can sometimes be better to use "console.log" instead of GM_log, since the former writes to the Firebug console. Then you don't have to switch back and forth from the error console.

Posted by Tyler Trafford on February 05, 2008 at 10:51 AM PST #

Re: GM Compiling

Take a look at this:

http://svn.devjavu.com/greasemonkey/branches/compiler/

Posted by Tyler Trafford on February 05, 2008 at 10:56 AM PST #

Thanks! Is there a tarball of a stable release or
an svn command I can use to get those files
(rather than a wget command)?

Posted by Rich Burridge on February 05, 2008 at 11:08 AM PST #

I should add that I got the files via wget, but I'm
not sure how to drive it. Is there some README
file or documentation i should be looking at?

Thanks.

Posted by Rich Burridge on February 05, 2008 at 11:12 AM PST #

(svn co http://svn.devjavu.com/greasemonkey/branches/compiler)

I've never used it myself... looks like there is a Ruby script xpi-build/gm2xpi.rb that you need to run.

./xpi-build/gm2xpi.rb "Your Name" YOURURL \
VERSIONNUMBER GUID blah.user.js

(this compiler is a work in progress, but should work.)

Posted by Tyler Trafford on February 05, 2008 at 11:28 AM PST #

Post a Comment:

Comments are closed for this entry.