native application’s lifecycle, the Chrome environment controls a web appli-
cation’s lifecycle. It provides the web applications with important events, such
as alarms or signals.
The
background.js
file is where you connect your Chrome app to the Chrome
runtime environment. You can register for many events, but you at least have
to specify what happens when your application gets launched. Here’s how
you can do that:
ChromeApps/SimpleApp/background.js
chrome.app.runtime.onLaunched.addListener(function() {
chrome.app.window.create('main.html', {
id: 'main',
bounds: { width: 200, height: 100 }
});
});
The preceding file adds a new listener function to Chrome’s runtime environ-
ment. Specifically, it registers a listener function for the
onLaunched
event—that
is, the function will be called when the Chrome app gets launched.
The listener opens a new Chrome app window by calling the
chrome.app.win-
dow.create
function. This function expects the name of an HTML document to
be opened and some optional arguments, such as the window’s ID and its
bounds. For our first Chrome app, the HTML document looks as follows:
ChromeApps/SimpleApp/main.html
<!DOCTYPE html>
<html>
<head>
<title>My First Chrome App</title>
</head>
<body>
<p>Hello, world!</p>
</body>
</html>
These three files (
manifest.json
,
background.js
, and
main.html
) are all you need for a
basic Chrome app. In the next section, you’ll learn how to run the application
for the first time.
Starting the Chrome App
During development, it’d be tedious to create zip archives every time you
wanted to try your latest changes to a Chrome app. That’s why the Chrome
browser supports the execution of unzipped applications. Point the browser
Appendix 4. Controlling the Arduino with a Browser • 270
report erratum • discuss
www.it-ebooks.info