OLD | NEW |
(Empty) | |
| 1 <h1 id="create-chrome-app"> |
| 2 <span class="h1-step">Step 1:</span> |
| 3 Create and Run a Chrome App |
| 4 </h1> |
| 5 |
| 6 <p>In this step, you will learn:</p> |
| 7 |
| 8 <ul> |
| 9 <li>The basic building blocks of a Chrome App package, including the manifest
file and background scripts.</li> |
| 10 <li>How to install, run, and debug a Chrome App.</li> |
| 11 </ul> |
| 12 |
| 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 ↓</a>. |
| 17 </p> |
| 18 |
| 19 <h2 id="app-components">Get familiar with Chrome App packages</h2> |
| 20 |
| 21 <p>Chrome Apps are structured similarly to Chrome extensions |
| 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 |
| 26 <ul> |
| 27 <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> |
| 29 <li>The <strong>event page</strong>, also called a <strong>background script</
strong>, is |
| 30 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> |
| 32 <li>All <strong>code files</strong> must be included in the Chrome App package
. |
| 33 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> |
| 35 </ul> |
| 36 |
| 37 <h3 id="manifest">Create a manifest</h3> |
| 38 |
| 39 <p>Open your favorite code/text editor and create the following file named <stro
ng>manifest.json</strong>:</p> |
| 40 |
| 41 <pre data-filename="manifest.json"> |
| 42 { |
| 43 "manifest_version": 2, |
| 44 "name": "Codelab", |
| 45 "version": "1", |
| 46 "icons": { |
| 47 "128": "icon_128.png" |
| 48 }, |
| 49 "permissions": [], |
| 50 "app": { |
| 51 "background": { |
| 52 "scripts": ["background.js"] |
| 53 } |
| 54 }, |
| 55 "minimum_chrome_version": "28" |
| 56 } |
| 57 </pre> |
| 58 |
| 59 <p>Notice how this manifest describes a background script named <em>background.j
s</em>. We'll create that file next.</p> |
| 60 |
| 61 <pre> |
| 62 "background": { |
| 63 "scripts": ["<b>background.js</b>"] |
| 64 } |
| 65 </pre> |
| 66 |
| 67 <p>We'll also supply you with an app icon later in this step:</p> |
| 68 |
| 69 <pre> |
| 70 "icons": { |
| 71 "128": "<b>icon_128.png</b>" |
| 72 }, |
| 73 </pre> |
| 74 |
| 75 <!-- <p>For more detailed information about the manifest file, refer to <a href=
"/apps/manifest">Manifest File Format</a> in the docs.</p> |
| 76 --> |
| 77 <h3 id="background-script">Create a background script</h3> |
| 78 |
| 79 <p>Create the following file and save it as <strong><em>background.js</em></stro
ng>:</p> |
| 80 |
| 81 <pre data-filename="background.js"> |
| 82 /** |
| 83 * Listens for the app launching then creates the window |
| 84 * |
| 85 * @see http://developer.chrome.com/apps/app.window.html |
| 86 */ |
| 87 chrome.app.runtime.onLaunched.addListener(function() { |
| 88 chrome.app.window.create('index.html', { |
| 89 id: 'main', |
| 90 bounds: { width: 620, height: 500 } |
| 91 }); |
| 92 }); |
| 93 </pre> |
| 94 |
| 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> |
| 96 |
| 97 <pre> |
| 98 <b>chrome.app.runtime.onLaunched</b>.addListener(function() { |
| 99 //... |
| 100 }); |
| 101 </pre> |
| 102 |
| 103 <p>When the Chrome App is launched, |
| 104 <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> |
| 106 |
| 107 <pre> |
| 108 <b>chrome.app.window.create</b>('<b>index.html</b>', { |
| 109 id: 'main', |
| 110 bounds: { width: 620, height: 500 } |
| 111 }); |
| 112 </pre> |
| 113 |
| 114 <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> |
| 116 |
| 117 <h3 id="html-view">Create an HTML view</h3> |
| 118 |
| 119 <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 |
| 121 <pre data-filename="index.html"> |
| 122 <!DOCTYPE html> |
| 123 <html> |
| 124 <head> |
| 125 <meta charset="utf-8"> |
| 126 </head> |
| 127 <body> |
| 128 <h1>Hello, let's code!</h1> |
| 129 </body> |
| 130 </html> |
| 131 </pre> |
| 132 |
| 133 <p>Just like any other web page, within this HTML file you can include |
| 134 additional packaged JavaScript, CSS, or assets.</p> |
| 135 |
| 136 <h3 id="app-icon">Add an app icon</h3> |
| 137 |
| 138 <p>Right-click and save this 128x128 image to your project folder as <strong><em
>icon_128.png</em></strong>:</p> |
| 139 |
| 140 <p align="center"><img src="{{static}}/images/app_codelab/icon_128.png" alt="Chr
ome App icon for this codelab" /></p> |
| 141 |
| 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> |
| 143 |
| 144 <h3 id="confirm-files">Confirm that you have created all your files</h3> |
| 145 |
| 146 <p>You should have these 4 files in your project folder now:</p> |
| 147 |
| 148 <figure> |
| 149 <img src="{{static}}/images/app_codelab/app-tutorial-files.png" alt="File fold
er screenshot"/> |
| 150 </figure> |
| 151 |
| 152 <h2 id="developer-mode">Install a Chrome App in developer mode</h2> |
| 153 |
| 154 <p>Use <strong>developer mode</strong> to quickly load and launch your app witho
ut having to finalize your app as a distribution package.</p> |
| 155 |
| 156 <ol> |
| 157 <li>Access <strong>chrome://extensions</strong> from the Chrome omnibox.</li> |
| 158 <li>Check off the <strong>Developer mode</strong> check box.</li> |
| 159 </ol> |
| 160 |
| 161 <figure> |
| 162 <img src="{{static}}/images/app_codelab/enable-developer-mode.gif" alt="Enable
developer mode"/> |
| 163 </figure> |
| 164 |
| 165 <ol start="3"> |
| 166 <li>Click <strong>Load unpacked extension</strong>.</li> |
| 167 <li>Using the file picker dialog, navigate to your app's project folder and se
lect it to load your app.</li> |
| 168 </ol> |
| 169 |
| 170 <figure> |
| 171 <img src="{{static}}/images/app_codelab/load-unpacked-extensions.gif" alt="Loa
d unpacked extensions"/> |
| 172 </figure> |
| 173 |
| 174 <h2 id="launch">Launch your finished Hello World app</h2> |
| 175 |
| 176 <p>After loading your project as an unpacked extension, click <strong>Launch</st
rong> next to your installed app. A new standalone window should open up:</p> |
| 177 |
| 178 <figure> |
| 179 <img src="{{static}}/images/app_codelab/step1-completed.png" alt="The finished
Hello World app after Step 1"/> |
| 180 </figure> |
| 181 |
| 182 <p>Congratulations, you've just created a new Chrome App!</p> |
| 183 |
| 184 <h2 id="devtools-debug">Debug a Chrome App with Chrome DevTools</h2> |
| 185 |
| 186 <p>You can use the <a href="/devtools">Chrome Developer Tools</a> to inspect, de
bug, audit, and test your |
| 187 app just like you do on a regular web page.</p> |
| 188 |
| 189 <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> |
| 190 |
| 191 <figure> |
| 192 <img src="{{static}}/images/app_codelab/inspect-element.png" alt="Inspect Elem
ent dialog box"/> |
| 193 </figure> |
| 194 |
| 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> |
| 196 |
| 197 <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> |
| 199 |
| 200 <figure> |
| 201 <img src="{{static}}/images/app_codelab/console-log.png" alt="DevTools console
log"/> |
| 202 </figure> |
| 203 |
| 204 <h2 id="recap">Recap APIs referenced in this step</h2> |
| 205 |
| 206 <p>For more detailed information about some of the APIs introduced in this step,
refer to:</p> |
| 207 |
| 208 <ul> |
| 209 <li> |
| 210 <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> |
| 212 </li> |
| 213 <li> |
| 214 <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> |
| 216 </li> |
| 217 <li> |
| 218 <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> |
| 220 </li> |
| 221 <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
> |
| 223 <a href="#background-script" class="anchor-link-icon" title="This feature me
ntioned in 'Create a background script'">↑</a> |
| 224 </li> |
| 225 <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> |
| 227 <a href="#background-script" class="anchor-link-icon" title="This feature me
ntioned in 'Create a background script'">↑</a> |
| 228 </li> |
| 229 </ul> |
| 230 |
| 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> |
OLD | NEW |