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); });<span class="keyword">var</span> <span class="function">getAllCallback</span> = <span class="keyword">function</span>(list) { <span class="keyword">var</span> apps = document.getElementById(<span class="string"><span class="delimiter">"</span><span class="content">apps</span><span class="delimiter">"</span></span>); <span class="keyword">var</span> counter = <span class="integer">0</span>; <span class="keyword">for</span>(<span class="keyword">var</span> i <span class="keyword">in</span> list) { <span class="comment">// we don't want to do anything with extensions yet.</span> <span class="keyword">var</span> extInf = list[i]; <span class="keyword">if</span>(extInf.isApp && extInf.enabled) { <span class="keyword">var</span> app = document.createElement(<span class="string"><span class="delimiter">"</span><span class="content">div</span><span class="delimiter">"</span></span>); <span class="keyword">var</span> img = <span class="keyword">new</span> Image(); img.className = <span class="string"><span class="delimiter">"</span><span class="content">image</span><span class="delimiter">"</span></span>; img.src = find128Image(extInf.icons); img.addEventListener(<span class="string"><span class="delimiter">"</span><span class="content">click</span><span class="delimiter">"</span></span>, (<span class="keyword">function</span>(ext) { <span class="keyword">return</span> <span class="keyword">function</span>() { chrome.management.launchApp(ext.id); }; })(extInf)); <span class="keyword">var</span> name = document.createElement(<span class="string"><span class="delimiter">"</span><span class="content">div</span><span class="delimiter">"</span></span>); name.className = <span class="string"><span class="delimiter">"</span><span class="content">name</span><span class="delimiter">"</span></span>; name.textContent = extInf.name; app.className = <span class="string"><span class="delimiter">"</span><span class="content">app</span><span class="delimiter">"</span></span>; app.appendChild(img); app.appendChild(name); apps.appendChild(app); <span class="keyword">if</span>(counter % <span class="integer">2</span>) { <span class="keyword">var</span> row = document.createElement(<span class="string"><span class="delimiter">"</span><span class="content">br</span><span class="delimiter">"</span></span>); apps.appendChild(row); } counter ++; } } }; <span class="keyword">var</span> <span class="function">find128Image</span> = <span class="keyword">function</span>(icons) { <span class="keyword">for</span>(<span class="keyword">var</span> icon <span class="keyword">in</span> icons) { <span class="keyword">if</span>(icons[icon].size == <span class="string"><span class="delimiter">"</span><span class="content">128</span><span class="delimiter">"</span></span>) { <span class="keyword">return</span> icons[icon].url; } } <span class="keyword">return</span> <span class="string"><span class="delimiter">"</span><span class="content">/noicon.png</span><span class="delimiter">"</span></span>; };</pre></div>
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.
I lead the Chrome Developer Relations team at Google.
We want people to have the best experience possible on the web without having to install a native app or produce content in a walled garden.
Our team tries to make it easier for developers to build on the web by supporting every Chrome release, creating great content to support developers on web.dev, contributing to MDN, helping to improve browser compatibility, and some of the best developer tools like Lighthouse, Workbox, Squoosh to name just a few.
I love to learn about what you are building, and how I can help with Chrome or Web development in general, so if you want to chat with me directly, please feel free to book a consultation.
I'm trialing a newsletter, you can subscribe below (thank you!)