Create Basic App

There are three core pieces to any Chrome App:

  • The manifest that describes meta-information about your application: name, description, version number and how to launch your application
  • The background script, which sets up how your application responds to system events such as the user installing and launching the app and the system suspending it
  • The view (which is optional, but you normally need to show the user something)

Let's look at each of these components at their simplest level.

Tip: If you use the Sublime Text editor with our plugin, you can create these three files with a click (Chrome -> New App -> Hello World).

Create manifest

In an empty directory (let's call it <myappdir>), create the manifest file: manifest.json

{
  "manifest_version": 2,
  "name": "My first app",
  "version": "1",
  "app": {
    "background": {
      "scripts": ["main.js"]
    }
  }
}

Create background script

In the same directory, create the background script: main.js

chrome.app.runtime.onLaunched.addListener(function() {
  chrome.app.window.create('index.html', {
    bounds: {
      width: 500,
      height: 309
    }
  });
});

Create view

Create the user interface: index.html

<html>
  <head>
      <meta charset="utf-8">
      <title>Hello World</title>
  </head>
  <body>
      <h1>Hello, World!</h1>
  </body>
</html>

Install and execute sample app

  • Go to chrome://extensions.
  • Load unpacked extension...
  • Select the <myappdir> directory.
  • Open a new Chrome tab.
  • Click on the "My First App" icon.

Debug, fix, and reload

Tip: If you have enabled Developer mode in chrome://extensions, your apps can be inspected and debugged using the Chrome Developer Tools. Just like any standard web page, right-click on page and select Inspect Element. For the background page which doesn't have UI, you can either right-click on any app window and select Inspect Background Page or go to chrome://extensions and click on Inspect Views...

  1. Change the text "Hello world" to "My first app" in index.html.

  2. Change the main.js background script to create two windows instead of one. Don't bother to create another html. For now, you can open index.html on both.

  3. After changing code, right-click on your app and select Reload App to reload the changed files. All Developer Tools windows will be reopened when you reload your app.

  4. Launch the app in a new tab page. Move the top window and you will see the second window behind it.

Takeaways

  • Chrome Apps have three basic pieces. The first and foremost is the manifest, which describes your app, requests special permissions, defines important meta information and much more. The second part is the background script, which contains all logic not tied to a specific user interface. The last part is the user interface: HTML, CSS, JavaScripts related to the interface, images, etc.
  • Chrome Apps can be debugged just like standard web pages using the Chrome Developer Tools. But since an app doesn't have the Reload control of a browser, a Reload App option has been added when you run in Developer mode.

You should also read

What's next?

In 3 - Create MVC, you will use either pure JavaScript or AngluarJS to build your app's model, view, and controller.