cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / chrome / common / extensions / docs / templates / articles / app_codelab_basics.html
blob3049535d571993fc8c917b7f0e85a3639d69f0dc
1 <h1 id="create-chrome-app">
2 <span class="h1-step">Step 1:</span>
3 Create and Run a Chrome App
4 </h1>
6 <p>In this step, you will learn:</p>
8 <ul>
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>
11 </ul>
13 <p>
14 <em>Estimated time to complete this step: 10 minutes.</em>
15 <br>
16 To preview what you will complete in this step, <a href="#launch">jump down to the bottom of this page &#8595;</a>.
17 </p>
19 <h2 id="app-components">Get familiar with Chrome Apps</h2>
21 <p>A Chrome App contains these components:</p>
23 <ul>
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>
32 </ul>
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,
41 "name": "Codelab",
42 "version": "1",
43 "icons": {
44 "128": "icon_128.png"
46 "permissions": [],
47 "app": {
48 "background": {
49 "scripts": ["background.js"]
52 "minimum_chrome_version": "28"
54 </pre>
56 <p>Notice how this manifest describes a background script named <em>background.js</em>.
57 You will create that file next.</p>
59 <pre>
60 "background": {
61 "scripts": ["<b>background.js</b>"]
63 </pre>
65 <p>We'll supply you with an app icon later in this step:</p>
67 <pre>
68 "icons": {
69 "128": "<b>icon_128.png</b>"
71 </pre>
73 <!-- <p>For more detailed information about the manifest file, refer to <a href="/apps/manifest">Manifest File Format</a> in the docs.</p>
74 -->
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">
80 /**
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', {
87 id: 'main',
88 bounds: { width: 620, height: 500 }
89 });
90 });
91 </pre>
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>
95 <pre>
96 <b>chrome.app.runtime.onLaunched</b>.addListener(function() {
97 //...
98 });
99 </pre>
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>
106 <pre>
107 <b>chrome.app.window.create</b>('<b>index.html</b>', {
108 id: 'main',
109 bounds: { width: 620, height: 500 }
111 </pre>
113 <p>Background scripts may contain additional listeners, windows, post messages,
114 and launch data &mdash; 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 &lt;!DOCTYPE html&gt;
122 &lt;html&gt;
123 &lt;head&gt;
124 &lt;meta charset="utf-8"&gt;
125 &lt;/head&gt;
126 &lt;body&gt;
127 &lt;h1&gt;Hello, let's code!&lt;/h1&gt;
128 &lt;/body&gt;
129 &lt;/html&gt;
130 </pre>
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>
147 <figure>
148 <img src="{{static}}/images/app_codelab/app-tutorial-files.png" alt="File folder screenshot"/>
149 </figure>
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>
155 <ol>
156 <li>Access <strong>chrome://extensions</strong> from the Chrome omnibox.</li>
157 <li>Check off the <strong>Developer mode</strong> check box.</li>
158 </ol>
160 <figure>
161 <img src="{{static}}/images/app_codelab/enable-developer-mode.gif" alt="Enable developer mode"/>
162 </figure>
164 <ol start="3">
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>
167 </ol>
169 <figure>
170 <img src="{{static}}/images/app_codelab/load-unpacked-extensions.gif" alt="Load unpacked extensions"/>
171 </figure>
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>
177 <figure>
178 <img src="{{static}}/images/app_codelab/step1-completed.png" alt="The finished Hello World app after Step 1"/>
179 </figure>
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 &gt; Reload App</b>), check the DevTools console for any errors (<b>right-click &gt; Inspect Element</b>).</p>
190 <figure>
191 <img src="{{static}}/images/app_codelab/inspect-element.png" alt="Inspect Element dialog box"/>
192 </figure>
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>
199 <figure>
200 <img src="{{static}}/images/app_codelab/console-log.png" alt="DevTools console log"/>
201 </figure>
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>
207 <ul>
208 <li>
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'">&#8593;</a>
211 </li>
212 <li>
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'">&#8593;</a>
215 </li>
216 <li>
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'">&#8593;</a>
219 </li>
220 <li>
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'">&#8593;</a>
223 </li>
224 <li>
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'">&#8593;</a>
227 </li>
228 </ul>
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 &raquo;</a></p>