Thursday 15 September 2011

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


So I'm trying to create a simple omnibox extension for Chrome for personal use. It works like any other omnibox extension: You enter expansion keywords and press tabs, which gives extension control of the Omnibox. Then you type a phrase or whatnot and pop up a list of suggestions below the omnibox, then you can use the arrow keys or the mouse to select the suggestion and then navigate the browser to the page related to that suggestion. All this work is perfectly fine.

However, what I should do is that when I enter without selecting a suggestion, I want to go to the suggestion before the browser suggestion list. Instead, what happens now, I get this error page:

I did not find any answer in the document about this. This looks like my code now (background.js):

  chrome.omnibox.onInputChanged.addListener (function (text, suggestion) {text = text.replace ("", "" Suggestions ([{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.get selected (empty, function (tab) {chrome.tabs.update (tab.id, {url: text});});}); Chrome.omnibox.setDefaultSuggestion ({description: "visit / r /% s"});   

Is there a way to set a default address when a suggestion is pressed without selecting? Does Custom Search functionality work by default in the Chrome omnibox? Thanks for any help.

inside chrome.omnibox.onInputChanged.addListener () , you Chrome.omnibox.setDefaultSuggestion () .

, so when you type something in the Omnibox, you will want to make the first suggestion the default suggestion (so that you press down arrow), and then suggestions () < Any remaining suggestions like / code> normal. Example:

  chrome.omnibox.onInputChanged.addListener (function (text, suggestion) {text = text.replace ("", ""); // Add suggestion in 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 the first suggestion as the default suggestion chrome.omnibox .setDefaultSuggestion ({description: suggestions [0] .description}); // Remove the first suggestion fr since the ohm array we have just suggested. Shift (); // remaining suggestions suggestion suggestions (suggestions);}); Chrome.omnibox.onInputEntered.addListener (task (text) {chrome.tabs.get selected (empty, function (tab) {var url; if (text.substr (0, 7) == 'http: //') { Url = text; // If the text does not look like a URL, then the user has selected the default suggestion, like your example reddit.com} else {url = 'http://reddit.com/r/' + Text;} Chrome .tabs.update (tab.id, {url: url});});});    

No comments:

Post a Comment