Apps to Bookmarks Bar + omnibox API = <3

Back in March, I posted about the Apps to Bookmarks Bar extension I created, and someone asked in the reviews if it was possible to add existing apps to the bookmarks bar. I’ve been meaning to get around to adding that for a couple weeks now, and finally updated it today.

The new version uses the omnibox API, which basically lets Chrome users turn the omnibox into a command line interface. In this case, you can use it to add bookmark icons for all your currently installed Chrome web apps to your bookmarks bar, just by typing ‘a2b import’ in the omnibox. If you install it and try it out, you’ll see how easy it is. I’m giving you fair warning though — if you don’t want all your installed apps to show up on your bookmarks bar, you probably shouldn’t try it unless you don’t mind deleting them afterwards.

In the manifest.json file, I added a new line to tell the extension that I want to use the omnibox API and what keyword to look out for.

"omnibox": { "keyword" : "a2b" },

Then in a2b.js, I added two new event listeners. The first is for the onInputChanged event, which is fired when a user updates the text in the omnibox and a keyword is active. What I’m doing here is providing a suggestion to type ‘import’ in order to import apps that are already installed as icons in the bookmarks bar.

chrome.omnibox.onInputChanged.addListener(
  function(text, suggest) {
    suggest([
      {content: text + " import", description: "Import your existing apps to the bookmarks bar."}
    ]);
  });

Next comes the listener for the onInputEntered event. This event gets fired when a user has entered and accepted input for a keyword into the omnibox. The code checks that they entered the word import, and then loops through all the installed apps and adds a bookmark icon for each one to the bookmarks bar.

chrome.omnibox.onInputEntered.addListener(
  function(text) {
    if (text.toLowerCase() == 'import') {
      chrome.management.getAll(function(items) {
        for (var i = 0; i < items.length; i++) {
          if (items[i].isApp) {
	    // Using an id of 1 is a "reasonably safe" way of getting to
	    // the Bookmarks Bar according to http://crbug.com/21330
	    chrome.bookmarks.create({'parentId': '1',
			             'url': items[i].appLaunchUrl}); 				
          }	
        }			
      });
    }
  });

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

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