Show me the code! – By Davanum Srinivas

September 1, 2008

[Ubiquity] use Google’s built-in calculator function

Filed under: Uncategorized — Davanum Srinivas @ 10:15 pm

Here’s a script to plugin google’s calculator. Does conversions too (Try “5 feet in inches” for example).

Here’s a screen shot:

Here’s the script:

CmdUtils.CreateCommand({
    name: "mycalculator",
    takes: {"statusText": noun_arb_text},
    description: "Calculates the value of a mathematical expression.",
    icon: "chrome://ubiquity/content/icons/calculator.png",
    homepage: "https://davanum.wordpress.com/",
    author: { name: "Davanum Srinivas", email: "davanum AT gmail.com"},
    help: "Try it out: issue "calc 22/7 - 1".",

    preview: function(pblock, statusText) {
        if (statusText.text != "") {
            var url = 'http://www.google.com/search?q=' + encodeURI(statusText.text).replace("+", "%2B");
            jQuery.get(url, null, function(page) {
                result = page.match(/<h2 class=r><font size=\+1><b>.*<\/b><\/h2>/i);
                    pblock.innerHTML = 'Result: <h3>  ' + (result == null ? "" : result) + '</h3>';
            });
        }
    },
    execute: function(statusText) {
        Utils.openUrlInBrowser('http://www.google.com/search?q=' + encodeURI(statusText.text).replace("+", "%2B"));
    }
})

2 Comments »

  1. hey Dav.
    how come ubiquity doesnt bring up a popup or something to load these from?

    any ideas?

    Comment by ian holsman — September 2, 2008 @ 5:16 pm

  2. ian, I haven’t set up the herd functionality yet.

    Comment by Davanum Srinivas — September 2, 2008 @ 9:01 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: