Show me the code! – By Davanum Srinivas

Web Services, Apache, Websphere, IBM, etc.

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

with 2 comments

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: "http://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"));
    }
})

Written by Davanum Srinivas

September 1, 2008 at 10:15 pm

Posted in Uncategorized

2 Responses

Subscribe to comments with RSS.

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

    any ideas?

    ian holsman

    September 2, 2008 at 5:16 pm

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

    Davanum Srinivas

    September 2, 2008 at 9:01 pm


Leave a Reply