OLD | NEW |
1 <h1 id="create-chrome-app"> | 1 <h1 id="create-chrome-app"> |
2 <span class="h1-step">Step 1:</span> | 2 <span class="h1-step">Step 1:</span> |
3 Create and Run a Chrome App | 3 Create and Run a Chrome App |
4 </h1> | 4 </h1> |
5 | 5 |
6 <p>In this step, you will learn:</p> | 6 <p>In this step, you will learn:</p> |
7 | 7 |
8 <ul> | 8 <ul> |
9 <li>The basic building blocks of a Chrome App package, including the manifest
file and background scripts.</li> | 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> | 10 <li>How to install, run, and debug a Chrome App.</li> |
11 </ul> | 11 </ul> |
12 | 12 |
13 <p> | 13 <p> |
14 <em>Estimated time to complete this step: 10 minutes.</em> | 14 <em>Estimated time to complete this step: 10 minutes.</em> |
15 <br> | 15 <br> |
16 To preview what you will complete in this step, <a href="#launch">jump down to
the bottom of this page ↓</a>. | 16 To preview what you will complete in this step, <a href="#launch">jump down to
the bottom of this page ↓</a>. |
17 </p> | 17 </p> |
18 | 18 |
19 <h2 id="app-components">Get familiar with Chrome App packages</h2> | 19 <h2 id="app-components">Get familiar with Chrome Apps</h2> |
20 | 20 |
21 <p>Chrome Apps are structured similarly to Chrome extensions | 21 <p>A Chrome App contains these components:</p> |
22 so current extension developers will find this process to be familiar.</p> | |
23 | |
24 <p>A packaged Chrome App contains these components:</p> | |
25 | 22 |
26 <ul> | 23 <ul> |
27 <li>The <strong>manifest</strong> specifies the meta information of your app. | 24 <li>The <strong>manifest</strong> specifies the meta information of your app. |
28 The manifest tells Chrome about your app, how to launch it, and any extra pe
rmissions that it requires.</li> | 25 The manifest tells Chrome about your app, how to launch it, and any extra pe
rmissions that it requires.</li> |
29 <li>The <strong>event page</strong>, also called a <strong>background script</
strong>, is | 26 <li>The <strong>event page</strong>, also called a <strong>background script</
strong>, is |
30 responsible for managing the app life cycle. | 27 responsible for managing the app life cycle. |
31 The background script is where you register listeners for specific app event
s such as the launching and closing of the app's window.</li> | 28 The background script is where you register listeners for specific app event
s such as the launching and closing of the app's window.</li> |
32 <li>All <strong>code files</strong> must be included in the Chrome App package
. | 29 <li>All <strong>code files</strong> must be packaged in the Chrome App. |
33 This includes HTML, CSS, JS, and Native Client modules.</li> | 30 This includes HTML, CSS, JS, and Native Client modules.</li> |
34 <li><strong>Assets</strong>, including app icons, should be included in the pa
ckage as well.</li> | 31 <li><strong>Assets</strong>, including app icons, should be packaged in the Ch
rome App as well.</li> |
35 </ul> | 32 </ul> |
36 | 33 |
37 <h3 id="manifest">Create a manifest</h3> | 34 <h3 id="manifest">Create a manifest</h3> |
38 | 35 |
39 <p>Open your favorite code/text editor and create the following file named <stro
ng>manifest.json</strong>:</p> | 36 <p>Open your favorite code/text editor and create the following file named <stro
ng>manifest.json</strong>:</p> |
40 | 37 |
41 <pre data-filename="manifest.json"> | 38 <pre data-filename="manifest.json"> |
42 { | 39 { |
43 "manifest_version": 2, | 40 "manifest_version": 2, |
44 "name": "Codelab", | 41 "name": "Codelab", |
45 "version": "1", | 42 "version": "1", |
46 "icons": { | 43 "icons": { |
47 "128": "icon_128.png" | 44 "128": "icon_128.png" |
48 }, | 45 }, |
49 "permissions": [], | 46 "permissions": [], |
50 "app": { | 47 "app": { |
51 "background": { | 48 "background": { |
52 "scripts": ["background.js"] | 49 "scripts": ["background.js"] |
53 } | 50 } |
54 }, | 51 }, |
55 "minimum_chrome_version": "28" | 52 "minimum_chrome_version": "28" |
56 } | 53 } |
57 </pre> | 54 </pre> |
58 | 55 |
59 <p>Notice how this manifest describes a background script named <em>background.j
s</em>. We'll create that file next.</p> | 56 <p>Notice how this manifest describes a background script named <em>background.j
s</em>. |
| 57 You will create that file next.</p> |
60 | 58 |
61 <pre> | 59 <pre> |
62 "background": { | 60 "background": { |
63 "scripts": ["<b>background.js</b>"] | 61 "scripts": ["<b>background.js</b>"] |
64 } | 62 } |
65 </pre> | 63 </pre> |
66 | 64 |
67 <p>We'll also supply you with an app icon later in this step:</p> | 65 <p>We'll supply you with an app icon later in this step:</p> |
68 | 66 |
69 <pre> | 67 <pre> |
70 "icons": { | 68 "icons": { |
71 "128": "<b>icon_128.png</b>" | 69 "128": "<b>icon_128.png</b>" |
72 }, | 70 }, |
73 </pre> | 71 </pre> |
74 | 72 |
75 <!-- <p>For more detailed information about the manifest file, refer to <a href=
"/apps/manifest">Manifest File Format</a> in the docs.</p> | 73 <!-- <p>For more detailed information about the manifest file, refer to <a href=
"/apps/manifest">Manifest File Format</a> in the docs.</p> |
76 --> | 74 --> |
77 <h3 id="background-script">Create a background script</h3> | 75 <h3 id="background-script">Create a background script</h3> |
(...skipping 17 matching lines...) Expand all Loading... |
95 <p>This background script simply waits for the <a href="/apps/app_runtime.html#e
vent-onLaunched">chrome.app.runtime.onLaunched</a> launch event for the applicat
ion and executes the callback function:</p> | 93 <p>This background script simply waits for the <a href="/apps/app_runtime.html#e
vent-onLaunched">chrome.app.runtime.onLaunched</a> launch event for the applicat
ion and executes the callback function:</p> |
96 | 94 |
97 <pre> | 95 <pre> |
98 <b>chrome.app.runtime.onLaunched</b>.addListener(function() { | 96 <b>chrome.app.runtime.onLaunched</b>.addListener(function() { |
99 //... | 97 //... |
100 }); | 98 }); |
101 </pre> | 99 </pre> |
102 | 100 |
103 <p>When the Chrome App is launched, | 101 <p>When the Chrome App is launched, |
104 <a href="/apps/app_window.html#method-create">chrome.app.window.create()</a> | 102 <a href="/apps/app_window.html#method-create">chrome.app.window.create()</a> |
105 will create a new window using a basic HTML page (<em>index.html</em>) as the so
urce. We'll create the HTML view in the next step.</p> | 103 will create a new window using a basic HTML page (<em>index.html</em>) as the so
urce. |
| 104 You will create the HTML view in the next step.</p> |
106 | 105 |
107 <pre> | 106 <pre> |
108 <b>chrome.app.window.create</b>('<b>index.html</b>', { | 107 <b>chrome.app.window.create</b>('<b>index.html</b>', { |
109 id: 'main', | 108 id: 'main', |
110 bounds: { width: 620, height: 500 } | 109 bounds: { width: 620, height: 500 } |
111 }); | 110 }); |
112 </pre> | 111 </pre> |
113 | 112 |
114 <p>Background scripts may contain additional listeners, windows, post messages, | 113 <p>Background scripts may contain additional listeners, windows, post messages, |
115 and launch data — all of which are used by the event page to manage the ap
p. <!-- For more detailed information about background scripts, refer to <a href
="app_lifecycle.html">Chrome App Lifecycle</a> in the docs. --></p> | 114 and launch data — all of which are used by the event page to manage the ap
p. <!-- For more detailed information about background scripts, refer to <a href
="app_lifecycle.html">Chrome App Lifecycle</a> in the docs. --></p> |
(...skipping 16 matching lines...) Expand all Loading... |
132 | 131 |
133 <p>Just like any other web page, within this HTML file you can include | 132 <p>Just like any other web page, within this HTML file you can include |
134 additional packaged JavaScript, CSS, or assets.</p> | 133 additional packaged JavaScript, CSS, or assets.</p> |
135 | 134 |
136 <h3 id="app-icon">Add an app icon</h3> | 135 <h3 id="app-icon">Add an app icon</h3> |
137 | 136 |
138 <p>Right-click and save this 128x128 image to your project folder as <strong><em
>icon_128.png</em></strong>:</p> | 137 <p>Right-click and save this 128x128 image to your project folder as <strong><em
>icon_128.png</em></strong>:</p> |
139 | 138 |
140 <p align="center"><img src="{{static}}/images/app_codelab/icon_128.png" alt="Chr
ome App icon for this codelab" /></p> | 139 <p align="center"><img src="{{static}}/images/app_codelab/icon_128.png" alt="Chr
ome App icon for this codelab" /></p> |
141 | 140 |
142 <p>We will use this PNG as our application's icon that users will see in the lau
nch menu. <!-- For more detailed information about app icons, refer to <a href="
/apps/manifest/icons">Manifest - Icons</a> in the docs. --></p> | 141 <p>You will use this PNG as our application's icon that users will see in the la
unch menu. <!-- For more detailed information about app icons, refer to <a href=
"/apps/manifest/icons">Manifest - Icons</a> in the docs. --></p> |
143 | 142 |
144 <h3 id="confirm-files">Confirm that you have created all your files</h3> | 143 <h3 id="confirm-files">Confirm that you have created all your files</h3> |
145 | 144 |
146 <p>You should have these 4 files in your project folder now:</p> | 145 <p>You should have these 4 files in your project folder now:</p> |
147 | 146 |
148 <figure> | 147 <figure> |
149 <img src="{{static}}/images/app_codelab/app-tutorial-files.png" alt="File fold
er screenshot"/> | 148 <img src="{{static}}/images/app_codelab/app-tutorial-files.png" alt="File fold
er screenshot"/> |
150 </figure> | 149 </figure> |
151 | 150 |
152 <h2 id="developer-mode">Install a Chrome App in developer mode</h2> | 151 <h2 id="developer-mode">Install a Chrome App in developer mode</h2> |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 | 193 |
195 <p>(We'll cover the <strong>Inspect Background Page</strong> option in <a href="
app_codelab_alarms.html">Step 3</a> with alarms.)</p> | 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 | 195 |
197 <p>The DevTools JavaScript console has access to the same APIs available to your
app. | 196 <p>The DevTools JavaScript console has access to the same APIs available to your
app. |
198 You can easily test an API call before adding it to your code:</p> | 197 You can easily test an API call before adding it to your code:</p> |
199 | 198 |
200 <figure> | 199 <figure> |
201 <img src="{{static}}/images/app_codelab/console-log.png" alt="DevTools console
log"/> | 200 <img src="{{static}}/images/app_codelab/console-log.png" alt="DevTools console
log"/> |
202 </figure> | 201 </figure> |
203 | 202 |
204 <h2 id="recap">Recap APIs referenced in this step</h2> | 203 <h2 id="recap">For more information</h2> |
205 | 204 |
206 <p>For more detailed information about some of the APIs introduced in this step,
refer to:</p> | 205 <p>For more detailed information about some of the APIs introduced in this step,
refer to:</p> |
207 | 206 |
208 <ul> | 207 <ul> |
209 <li> | 208 <li> |
210 <a href="/apps/manifest" title="Read 'Manifest File Format' in the Chrome de
veloper docs">Manifest File Format</a> | 209 <a href="/apps/manifest" title="Read 'Manifest File Format' in the Chrome de
veloper docs">Manifest File Format</a> |
211 <a href="#manifest" class="anchor-link-icon" title="This feature mentioned i
n 'Create a manifest'">↑</a> | 210 <a href="#manifest" class="anchor-link-icon" title="This feature mentioned i
n 'Create a manifest'">↑</a> |
212 </li> | 211 </li> |
213 <li> | 212 <li> |
214 <a href="/apps/manifest/icons" title="Read 'Manifest - Icons' in the Chrome
developer docs">Manifest - Icons</a> | 213 <a href="/apps/manifest/icons" title="Read 'Manifest - Icons' in the Chrome
developer docs">Manifest - Icons</a> |
215 <a href="#app-icon" class="anchor-link-icon" title="This feature mentioned i
n 'Add an app icon'">↑</a> | 214 <a href="#app-icon" class="anchor-link-icon" title="This feature mentioned i
n 'Add an app icon'">↑</a> |
216 </li> | 215 </li> |
217 <li> | 216 <li> |
218 <a href="app_lifecycle.html" title="Read 'Manifest File Format' in the Chrom
e developer docs">Chrome App Lifecycle</a> | 217 <a href="app_lifecycle.html" title="Read 'Manifest File Format' in the Chrom
e developer docs">Chrome App Lifecycle</a> |
219 <a href="#background-script" class="anchor-link-icon" title="This feature me
ntioned in 'Create a background script'">↑</a> | 218 <a href="#background-script" class="anchor-link-icon" title="This feature me
ntioned in 'Create a background script'">↑</a> |
220 </li> | 219 </li> |
221 <li> | 220 <li> |
222 <a href="/apps/app_runtime.html#event-onLaunched" title="Read 'chrome.app.ru
ntime.onLaunched' in the Chrome developer docs">chrome.app.runtime.onLaunched</a
> | 221 <a href="/apps/app_runtime.html#event-onLaunched" title="Read 'chrome.app.ru
ntime.onLaunched' in the Chrome developer docs">chrome.app.runtime.onLaunched</a
> |
223 <a href="#background-script" class="anchor-link-icon" title="This feature me
ntioned in 'Create a background script'">↑</a> | 222 <a href="#background-script" class="anchor-link-icon" title="This feature me
ntioned in 'Create a background script'">↑</a> |
224 </li> | 223 </li> |
225 <li> | 224 <li> |
226 <a href="/apps/app_window.html#method-create" title="Read 'chrome.app.window
.create()' in the Chrome developer docs">chrome.app.window.create()</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> |
227 <a href="#background-script" class="anchor-link-icon" title="This feature me
ntioned in 'Create a background script'">↑</a> | 226 <a href="#background-script" class="anchor-link-icon" title="This feature me
ntioned in 'Create a background script'">↑</a> |
228 </li> | 227 </li> |
229 </ul> | 228 </ul> |
230 | 229 |
231 <p>Ready to continue onto the next step? Go to <a href="app_codelab_import_todom
vc.html">Step 2 - Import an existing web app »</a></p> | 230 <p>Ready to continue onto the next step? Go to <a href="app_codelab_import_todom
vc.html">Step 2 - Import an existing web app »</a></p> |
OLD | NEW |