1 <h1 id=
"create-chrome-app">
2 <span class=
"h1-step">Step
1:
</span>
3 Create and Run a Chrome App
6 <p>In this step, you will learn:
</p>
9 <li>The basic building blocks of a Chrome App, including the manifest file and background scripts.
</li>
10 <li>How to install, run, and debug a Chrome App.
</li>
14 <em>Estimated time to complete this step:
10 minutes.
</em>
16 To preview what you will complete in this step,
<a href=
"#launch">jump down to the bottom of this page
↓</a>.
19 <h2 id=
"app-components">Get familiar with Chrome Apps
</h2>
21 <p>A Chrome App contains these components:
</p>
24 <li>The
<strong>manifest
</strong> specifies the meta information of your app.
25 The manifest tells Chrome about your app, how to launch it, and any extra permissions that it requires.
</li>
26 <li>The
<strong>event page
</strong>, also called a
<strong>background script
</strong>, is
27 responsible for managing the app life cycle.
28 The background script is where you register listeners for specific app events such as the launching and closing of the app's window.
</li>
29 <li>All
<strong>code files
</strong> must be packaged in the Chrome App.
30 This includes HTML, CSS, JS, and Native Client modules.
</li>
31 <li><strong>Assets
</strong>, including app icons, should be packaged in the Chrome App as well.
</li>
34 <h3 id=
"manifest">Create a manifest
</h3>
36 <p>Open your favorite code/text editor and create the following file named
<strong>manifest.json
</strong>:
</p>
38 <pre data-filename=
"manifest.json">
40 "manifest_version":
2,
49 "scripts": [
"background.js"]
52 "minimum_chrome_version":
"28"
56 <p>Notice how this manifest describes a background script named
<em>background.js
</em>.
57 You will create that file next.
</p>
61 "scripts": [
"<b>background.js</b>"]
65 <p>We'll supply you with an app icon later in this step:
</p>
69 "128":
"<b>icon_128.png</b>"
73 <!-- <p>For more detailed information about the manifest file, refer to <a href="/apps/manifest">Manifest File Format</a> in the docs.</p>
75 <h3 id=
"background-script">Create a background script
</h3>
77 <p>Create the following file and save it as
<strong><em>background.js
</em></strong>:
</p>
79 <pre data-filename=
"background.js">
81 * Listens for the app launching then creates the window
83 * @see http://developer.chrome.com/apps/app.window.html
85 chrome.app.runtime.onLaunched.addListener(function() {
86 chrome.app.window.create('index.html', {
88 bounds: { width:
620, height:
500 }
93 <p>This background script simply waits for the
<a href=
"/apps/app_runtime.html#event-onLaunched">chrome.app.runtime.onLaunched
</a> launch event for the application and executes the callback function:
</p>
96 <b>chrome.app.runtime.onLaunched
</b>.addListener(function() {
101 <p>When the Chrome App is launched,
102 <a href=
"/apps/app_window.html#method-create">chrome.app.window.create()
</a>
103 will create a new window using a basic HTML page (
<em>index.html
</em>) as the source.
104 You will create the HTML view in the next step.
</p>
107 <b>chrome.app.window.create
</b>('
<b>index.html
</b>', {
109 bounds: { width:
620, height:
500 }
113 <p>Background scripts may contain additional listeners, windows, post messages,
114 and launch data
— all of which are used by the event page to manage the app.
<!-- For more detailed information about background scripts, refer to <a href="app_lifecycle.html">Chrome App Lifecycle</a> in the docs. --></p>
116 <h3 id=
"html-view">Create an HTML view
</h3>
118 <p>Create a simple web page to display a
"Hello World" message to the screen and save it as
<strong><em>index.html
</em></strong>:
</p>
120 <pre data-filename=
"index.html">
121 <!DOCTYPE html
>
124 <meta
charset=
"utf-8">
127 <h1
>Hello, let's code!
</h1
>
132 <p>Just like any other web page, within this HTML file you can include
133 additional packaged JavaScript, CSS, or assets.
</p>
135 <h3 id=
"app-icon">Add an app icon
</h3>
137 <p>Right-click and save this
128x128 image to your project folder as
<strong><em>icon_128.png
</em></strong>:
</p>
139 <p align=
"center"><img src=
"{{static}}/images/app_codelab/icon_128.png" alt=
"Chrome App icon for this codelab" /></p>
141 <p>You will use this PNG as our application's icon that users will see in the launch menu.
<!-- For more detailed information about app icons, refer to <a href="/apps/manifest/icons">Manifest - Icons</a> in the docs. --></p>
143 <h3 id=
"confirm-files">Confirm that you have created all your files
</h3>
145 <p>You should have these
4 files in your project folder now:
</p>
148 <img src=
"{{static}}/images/app_codelab/app-tutorial-files.png" alt=
"File folder screenshot"/>
151 <h2 id=
"developer-mode">Install a Chrome App in developer mode
</h2>
153 <p>Use
<strong>developer mode
</strong> to quickly load and launch your app without having to finalize your app as a distribution package.
</p>
156 <li>Access
<strong>chrome://extensions
</strong> from the Chrome omnibox.
</li>
157 <li>Check off the
<strong>Developer mode
</strong> check box.
</li>
161 <img src=
"{{static}}/images/app_codelab/enable-developer-mode.gif" alt=
"Enable developer mode"/>
165 <li>Click
<strong>Load unpacked extension
</strong>.
</li>
166 <li>Using the file picker dialog, navigate to your app's project folder and select it to load your app.
</li>
170 <img src=
"{{static}}/images/app_codelab/load-unpacked-extensions.gif" alt=
"Load unpacked extensions"/>
173 <h2 id=
"launch">Launch your finished Hello World app
</h2>
175 <p>After loading your project as an unpacked extension, click
<strong>Launch
</strong> next to your installed app. A new standalone window should open up:
</p>
178 <img src=
"{{static}}/images/app_codelab/step1-completed.png" alt=
"The finished Hello World app after Step 1"/>
181 <p>Congratulations, you've just created a new Chrome App!
</p>
183 <h2 id=
"devtools-debug">Debug a Chrome App with Chrome DevTools
</h2>
185 <p>You can use the
<a href=
"/devtools">Chrome Developer Tools
</a> to inspect, debug, audit, and test your
186 app just like you do on a regular web page.
</p>
188 <p>After you make changes to your code and reload your app (
<b>right-click
> Reload App
</b>), check the DevTools console for any errors (
<b>right-click
> Inspect Element
</b>).
</p>
191 <img src=
"{{static}}/images/app_codelab/inspect-element.png" alt=
"Inspect Element dialog box"/>
194 <p>(We'll cover the
<strong>Inspect Background Page
</strong> option in
<a href=
"app_codelab_alarms.html">Step
3</a> with alarms.)
</p>
196 <p>The DevTools JavaScript console has access to the same APIs available to your app.
197 You can easily test an API call before adding it to your code:
</p>
200 <img src=
"{{static}}/images/app_codelab/console-log.png" alt=
"DevTools console log"/>
203 <h2 id=
"recap">For more information
</h2>
205 <p>For more detailed information about some of the APIs introduced in this step, refer to:
</p>
209 <a href=
"/apps/manifest" title=
"Read 'Manifest File Format' in the Chrome developer docs">Manifest File Format
</a>
210 <a href=
"#manifest" class=
"anchor-link-icon" title=
"This feature mentioned in 'Create a manifest'">↑</a>
213 <a href=
"/apps/manifest/icons" title=
"Read 'Manifest - Icons' in the Chrome developer docs">Manifest - Icons
</a>
214 <a href=
"#app-icon" class=
"anchor-link-icon" title=
"This feature mentioned in 'Add an app icon'">↑</a>
217 <a href=
"app_lifecycle.html" title=
"Read 'Manifest File Format' in the Chrome developer docs">Chrome App Lifecycle
</a>
218 <a href=
"#background-script" class=
"anchor-link-icon" title=
"This feature mentioned in 'Create a background script'">↑</a>
221 <a href=
"/apps/app_runtime.html#event-onLaunched" title=
"Read 'chrome.app.runtime.onLaunched' in the Chrome developer docs">chrome.app.runtime.onLaunched
</a>
222 <a href=
"#background-script" class=
"anchor-link-icon" title=
"This feature mentioned in 'Create a background script'">↑</a>
225 <a href=
"/apps/app_window.html#method-create" title=
"Read 'chrome.app.window.create()' in the Chrome developer docs">chrome.app.window.create()
</a>
226 <a href=
"#background-script" class=
"anchor-link-icon" title=
"This feature mentioned in 'Create a background script'">↑</a>
230 <p>Ready to continue onto the next step? Go to
<a href=
"app_codelab_import_todomvc.html">Step
2 - Import an existing web app
»</a></p>