Web app launcher as a browser_action

Paul Kinlan

In my previous post “Creating a New New Tab Page For Chrome” I showed how you can take advantage of the Management API and the Override Pages framework to make your own custom App Launcher.

This in itself was pretty cool, however it didn’t address a real problem – Users still like to keep their existing custom NTP AND use Apps installed from the Webstore. To address this, I made a really simple “browser_action” for a new extension called Quick Launch.

It uses pretty much all of the existing code from the NTP tutorial, but some small changes. First the manifest has been updated to include the “browser_action” and remove the page override as follows:

{
    "name": "Quick Launch",
    "description": "Launches apps, the quick way!",
    "version": "0.0.0.10",
    "background_page": "background.html",
    "permissions" : ["management"],
    "browser_action" : {
        "default_icon" : "go.png",
        "default_title" : "Quick Launch",
        "default_popup": "popup.html"
    }
}

It is pretty simple, we just define an icon, a title and a webpage that will be opened when the user clicks on the button. We then create a file called popup.html and put the following JS in.

document.addEventListener("DOMContentLoaded", function() {
        chrome.management.getAll(getAllCallback);
      });

      var getAllCallback = function(list) {
        var apps = document.getElementById("apps");
        var counter = 0;
        for(var i in list) {
          // we don't want to do anything with extensions yet.
          var extInf = list[i];

          if(extInf.isApp && extInf.enabled) {
            var app = document.createElement("div");

            var img = new Image();
            img.className = "image";
            img.src = find128Image(extInf.icons);
            img.addEventListener("click", (function(ext) {
              return function() {
                chrome.management.launchApp(ext.id);
              };
            })(extInf));

            var name = document.createElement("div");
            name.className = "name";
            name.textContent = extInf.name;

            app.className = "app";
            app.appendChild(img);
            app.appendChild(name);
            apps.appendChild(app);

            if(counter % 2) {
              var row = document.createElement("br");
              apps.appendChild(row);
            }
            counter ++;

          }
        }
      };

      var find128Image = function(icons) {
        for(var icon in icons) {
          if(icons[icon].size == "128") {
            return icons[icon].url;
          }
        }

        return "/noicon.png";
      };

The code is pretty straight forward – when we detect that the DOM has loaded we ask the extension sub-sytem to get a list of all the apps and extension that the user has installed. Once the results are returned, we simply iterate through the list checking to see if it is an app (there is a handy property called “isApp”) and for each app we build a series of DOM objects and add them to the container. That is it, nothing else….. or is there? (well there is a few extra enhancements and styling I have made, but it is all visible in the source)

As always, the source is available on Github

I actually have another extension, that I will blog about a little later that I think you will really like – along the same lines as this.

Paul Kinlan

Trying to make the web and developers better.

RSS Github Medium