Show me the code! – By Davanum Srinivas

September 8, 2008

[Ubiquity] Go 3.0 script – the power of nouns in ubiquity

Filed under: Uncategorized — Davanum Srinivas @ 9:20 am

Many many thanks to fern on #ubiquity. With this version, you can view all the URL’s as you type them and view the preview at the same time as well. Basically fern’s code generates a list of URL’s which are available as nouns to Ubiquity and Ubiquity displays them as suggestions. Take a Look how simple the command itself is now as compared to the previous version/

Here’s the screenshot:

Here’s the code:

var noun_type_url = {
    _name: "url",
    _getHistoryLinks: function(partialSearch, onSearchComplete) {
        function AutoCompleteInput(aSearches) {
            this.searches = aSearches;
        }
        AutoCompleteInput.prototype = {
            constructor: AutoCompleteInput,
            searches: null,
            minResultsForPopup: 0,
            timeout: 10,
            searchParam: "",
            textValue: "",
            disableAutoComplete: false,
            completeDefaultIndex: false,
            get searchCount() {
                return this.searches.length;
            },
            getSearchAt: function(aIndex) {
                return this.searches[aIndex];
            },
            onSearchBegin: function() {},
            onSearchComplete: function() {},
            popupOpen: false,
            popup: {
                setSelectedIndex: function(aIndex) {},
                invalidate: function() {},
                // nsISupports implementation
                QueryInterface: function(iid) {
                    if (iid.equals(Ci.nsISupports) || iid.equals(Ci.nsIAutoCompletePopup)) return this;
                    throw Components.results.NS_ERROR_NO_INTERFACE;
                }
            },
            // nsISupports implementation
            QueryInterface: function(iid) {
                if (iid.equals(Ci.nsISupports) || iid.equals(Ci.nsIAutoCompleteInput)) return this;
                throw Components.results.NS_ERROR_NO_INTERFACE;
            }
        }

        var controller = Components.classes["@mozilla.org/autocomplete/controller;1"].getService(Components.interfaces.nsIAutoCompleteController);
        var input = new AutoCompleteInput(["history"]);
        controller.input = input;
        input.onSearchComplete = function() {
            onSearchComplete(controller);
        };

        controller.startSearch(partialSearch);
    },
    suggestions: [],
    _onSearchComplete: function(controller) {
        suggs = [];
        var max_suggs = 5;
        for (var i = 0; i < controller.matchCount && i <= max_suggs; i++) {
            var url = controller.getValueAt(i);
            suggs.push(CmdUtils.makeSugg(url));
        }
        noun_type_url.suggestions = suggs;
    },
    suggest: function(fragment) {
        this._getHistoryLinks(fragment, this._onSearchComplete);
        var regexp = /(ftp|http|https):\/\/(\w+:{01}\w*@)?(\S+)(:&#91;0-9&#93;+)?(\/|\/(&#91;\w#!:.?+=&%@!\-\/&#93;))?/;
        // Magic words "page" or "url" result in the URL of the current page
        if (fragment == "page" || fragment == "url") {
            var url = Application.activeWindow.activeTab.document.URL;
            this.suggestions.push(CmdUtils.makeSugg(url));
        }
        // If it's a valid URL, suggest it back
        if (regexp.test(fragment)) {
            this.suggestions.push(CmdUtils.makeSugg(fragment));
        } else if (regexp.test("http://" + fragment)) {
            this.suggestions.push(CmdUtils.makeSugg("http://" + fragment));
        }
        return this.suggestions;
    }
};

CmdUtils.CreateCommand({
    name: "go",
    takes: {
        "search terms": noun_type_url
    },
    preview: function(pblock, theWords) {
        if (theWords.text.length == 0) {
            pblock.innerHTML = "<b>Please enter a search term</b>";
            return;
        }
        var msg = "";
        pblock.innerHTML = msg;
    },
    execute: function(theWords) {
        Utils.openUrlInBrowser(theWords.text);
    },
})

6 Comments »

  1. Very nice. I’m loving the not-built-in commands that developers have created.

    Here is a collection of the best of them: http://aceonlineschools.com/not-built-in-ubiquity-commands/

    Comment by John — September 9, 2008 @ 1:43 pm

  2. Very good, now this command is complete!
    Thanks a lot

    Comment by **Juanito** — September 15, 2008 @ 4:47 pm

  3. how do i install this script to ubiquity?

    Comment by pvinis — December 6, 2008 @ 9:47 pm

  4. This is fantastic — I can finally get rid of the navigation bar! Unfortunately, though, the preview causes Firefox to break when using the “go” command with Gmail.

    Comment by Az — March 8, 2009 @ 3:35 pm

  5. Fantastic article:D Hope to come back soon

    Comment by imiguergiecof — May 20, 2009 @ 3:12 pm

  6. […] what I learned, I took a Goto Script that used to work fantastically (originally by Davanum Srinivas) and modified it to work with version 0.5’s API. I’m rather amazed at how simple it was […]

    Pingback by Ramble ramble ramble… » Ubiquity Goto Command — July 29, 2009 @ 1:59 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: