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?
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});
});
}
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With