Google Chrome Omnibox API -- automatically select first option on enter
Asked Answered
G

1

13

So I'm trying to build a simple Omnibox extension for Chrome for personal use. It works like any other Omnibox extension: you enter the extension keyword and press tab, which gives the extension control of the omnibox. Then you type in a phrase or whatnot and a list of suggestions pop up below the omnibox. Then you can use the arrow keys or mouse to select a suggestion and then the browser navigates to the page associated with that suggestion. All of that works perfectly fine.

However, what I'd like it to do is that when I press enter in without having selected a suggestion, I'd like the browser to go to the first suggestion from the suggestion list. Instead what happens right now, I get this error page:

chrome error page

I couldn't find any answers in the documentation on this. This is what my code looks like right now (in background.js):

chrome.omnibox.onInputChanged.addListener(
    function(text, suggest)
    {
        text = text.replace(" ", "");
        suggest([
            { content: "http://reddit.com/r/" + text, description: "reddit.com/r/" + text },
            { content: "http://imgur.com/r/" + text, description: "imgur.com/r/" + text }
        ]);
    }
);

chrome.omnibox.onInputEntered.addListener(
    function(text)
    {
        chrome.tabs.getSelected(null, function(tab)
        {
            chrome.tabs.update(tab.id, {url: text});
        });
    }
);

chrome.omnibox.setDefaultSuggestion({ description: "visit /r/%s" });

So is there a way of setting the default action when the enter is pressed without a suggestion being selected? Sort of like the custom search functionality works by default in the Chrome omnibox?

Gonnella answered 20/6, 2013 at 19:59 Comment(0)
N
19

Within chrome.omnibox.onInputChanged.addListener(), you'll want to call chrome.omnibox.setDefaultSuggestion().

So when you type something in the Omnibox, you'll want to make the first suggestion become the default suggestion (so you don't have to press the Down Arrow), and then suggest() any remaining suggestions like normal.


Example:

chrome.omnibox.onInputChanged.addListener(
    function(text, suggest)
    {
        text = text.replace(" ", "");

        // Add suggestions to an array
        var suggestions = [];
        suggestions.push({ content: "http://reddit.com/r/" + text, description: "reddit.com/r/" + text });
        suggestions.push({ content: "http://imgur.com/r/" + text, description: "imgur.com/r/" + text });

        // Set first suggestion as the default suggestion
        chrome.omnibox.setDefaultSuggestion({description:suggestions[0].description});

        // Remove the first suggestion from the array since we just suggested it
        suggestions.shift();

        // Suggest the remaining suggestions
        suggest(suggestions);
    }
);

chrome.omnibox.onInputEntered.addListener(
    function(text)
    {
        chrome.tabs.getSelected(null, function(tab)
        {
            var url;
            if (text.substr(0, 7) == 'http://') {
                url = text;

            // If text does not look like a URL, user probably selected the default suggestion, eg reddit.com for your example
            } else {
                url = 'http://reddit.com/r/' + text;
            }
            chrome.tabs.update(tab.id, {url: url});
        });
    }
);
Nickynico answered 22/6, 2013 at 0:27 Comment(2)
That is what I thought as well, but I can't seem to be able to set a url in the object passed to setDefaultSuggestion(), only a description. Mind showing me an example if possible?Gonnella
Added an example for you! setDefaultSuggestion is just for show, since it doesn't pass in anything meaningful. So if the user's text doesn't look like a URL, you can assume that they chose the default suggestion, eg reddit.com (in your code anyway).Nickynico

© 2022 - 2024 — McMap. All rights reserved.