最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Chrome Omnibox API -- automatically select first option on enter - Stack Overflow

programmeradmin0浏览0评论

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:

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: "/" + text, description: "reddit/r/" + text },
            { content: "/" + text, description: "imgur/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?

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:

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?

Share Improve this question edited Jul 18, 2018 at 11:19 Gaurang Tandon 6,75311 gold badges50 silver badges94 bronze badges asked Jun 20, 2013 at 19:59 Felix McFelix Mc 5031 gold badge5 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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});
        });
    }
);
发布评论

评论列表(0)

  1. 暂无评论