Table Of ContentProgramming Chrome Apps
Put your web app design skills to work by learning how to create powerful “ Programming Chrome P P rogramming
and portable Chrome Apps. With this practical book, you’ll learn how to build Apps is a great guide to r
o
Google’s unique apps to behave just like native apps so they can interact
the exciting new world g
with hardware devices, access external files, and send notifications.
of software engineering r C hrome
Author Marc Rochkind takes you through a hands-on, objective tour of
a
made possible by
Chrome Apps, which run on any platform that supports the Chrome
m
browser—including OS X, Windows, Linux, as well as Android and iOS. Chrome Apps and the
If you know how to work with HTML, CSS, JavaScript, and the DOM, you’re Chrome platform.” m
ready to get started.
—Stephen Fluin
i
Chief Strategy and Innovation Officer, n Apps
■ Learn how to build, run, and debug Chrome Apps step by step MentorMate, Inc. g
■ Use Chrome Apps to access Local Files, Sync Files, and External
C
Files
■ Take advantage of key-value-pair APIs, including Sync Storage h
and IndexedDB r
o
■ Use WebSockets, Google Cloud Messaging, and other
m
networking methods
DEVELOP CROSS-PLATFORM
■ Display graphics and images with Canvas, SVG, and the e
APPS FOR CHROME
mediaGalleries API
A
■ Use alarms, context menus, location, the camera, Bluetooth,
USB, and other APIs p
■ Publish apps to the Chrome Web Store with the Chrome Dev p
Editor s
Marc Rochkind, a consultant and developer of apps for iOS, OS X, and Windows,
worked for Bell Labs on Unix, including the Source Code Control System. He
is the author of Expert PHP and MySQL: Application Design and Development
(Apress) and Advanced UNIX Programming (Addison-Wesley).
R
o
c
h
k
in
d
WEB DEVELOPMENT Twitter: @oreillymedia
facebook.com/oreilly
US $39.99 CAN $41.99
ISBN: 978-1-491-90428-2
Marc Rochkind
Programming Chrome Apps
Put your web app design skills to work by learning how to create powerful “ Programming Chrome P P rogramming
and portable Chrome Apps. With this practical book, you’ll learn how to build Apps is a great guide to r
o
Google’s unique apps to behave just like native apps so they can interact
the exciting new world g
with hardware devices, access external files, and send notifications.
of software engineering r C hrome
Author Marc Rochkind takes you through a hands-on, objective tour of
a
made possible by
Chrome Apps, which run on any platform that supports the Chrome
m
browser—including OS X, Windows, Linux, as well as Android and iOS. Chrome Apps and the
If you know how to work with HTML, CSS, JavaScript, and the DOM, you’re Chrome platform.” m
ready to get started.
—Stephen Fluin
i
Chief Strategy and Innovation Officer, n Apps
■ Learn how to build, run, and debug Chrome Apps step by step MentorMate, Inc. g
■ Use Chrome Apps to access Local Files, Sync Files, and External
C
Files
■ Take advantage of key-value-pair APIs, including Sync Storage h
and IndexedDB r
o
■ Use WebSockets, Google Cloud Messaging, and other
m
networking methods
DEVELOP CROSS-PLATFORM
■ Display graphics and images with Canvas, SVG, and the e
APPS FOR CHROME
mediaGalleries API
A
■ Use alarms, context menus, location, the camera, Bluetooth,
USB, and other APIs p
■ Publish apps to the Chrome Web Store with the Chrome Dev p
Editor s
Marc Rochkind, a consultant and developer of apps for iOS, OS X, and Windows,
worked for Bell Labs on Unix, including the Source Code Control System. He
is the author of Expert PHP and MySQL: Application Design and Development
(Apress) and Advanced UNIX Programming (Addison-Wesley).
R
o
c
h
k
in
d
WEB DEVELOPMENT Twitter: @oreillymedia
facebook.com/oreilly
US $39.99 CAN $41.99
ISBN: 978-1-491-90428-2
Marc Rochkind
Programming Chrome Apps
Marc Rochkind
Programming Chrome Apps
by Marc Rochkind
Copyright © 2015 Marc Rochkind. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/
institutional sales department: 800-998-9938 or corporate@oreilly.com.
Editors: Brian MacDonald and Rachel Roumeliotis Indexer: WordCo Indexing Services, Inc.
Production Editor: Matthew Hacker Cover Designer: Ellie Volckhausen
Copyeditor: Bob Russell, Octal Publishing, Inc. Interior Designer: David Futato
Proofreader: Eileen Cohen Illustrator: Rebecca Demarest
January 2015: First Edition
Revision History for the First Edition:
2014-12-12: First release
See http://oreilly.com/catalog/errata.csp?isbn=9781491904282 for release details.
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Programming Chrome Apps, the cover
image of a crested screamer, and related trade dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark
claim, the designations have been printed in caps or initial caps.
While the publisher and the author have used good faith efforts to ensure that the information and instruc‐
tions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors
or omissions, including without limitation responsibility for damages resulting from the use of or reliance
on this work. Use of the information and instructions contained in this work is at your own risk. If any code
samples or other technology this work contains or describes is subject to open source licenses or the intel‐
lectual property rights of others, it is your responsibility to ensure that your use thereof complies with such
licenses and/or rights.
ISBN: 978-1-491-90428-2
[LSI]
To Valerie, Gillian, and Claire
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
1. Your First Chrome App. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
From Web Apps to Chrome Apps 1
Running a Chrome App 4
Using the Chrome Dev Editor 6
Differences Among Chrome Apps, Chrome Extensions, and Hosted Apps 9
Chrome App Restrictions 11
More on the Manifest 12
Permissions 13
Debugging Chrome Apps 14
Chrome Release Channels 18
Publishing a Chrome App 18
Self-Publishing 18
Publishing on the Chrome Web Store 19
Chapter Summary 20
2. Local Files, Sync Files, and External Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Local Files 22
FileSystem and DirectoryEntry 24
Showing Transient Messages 26
Getting DirectoryEntrys 28
Getting FileEntrys and Creating FileWriters 29
Writing a File with the FileWriter API 29
Reading a File 31
Auto-Saving and Idle Events 32
Sync Files 34
How Syncing Works 34
Making the Note App Sync 36
v
Listening for the FileStatusChanged Event 38
Showing File Status 41
Finding Remote Copies on Google Drive 42
External Files 42
A Simple Editor 43
Choosing Files and Directories 47
Adding Backup by Using Retained File Entries 50
Chapter Summary 54
3. Local Storage, Sync Storage, and IndexedDB. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Local and Sync Storage 55
Setting and Getting Local Storage 56
Implementing setParams and getParams 57
Adding Color Options to the Simple Editor App 58
Sync Storage 63
Removing Items and Clearing Storage 64
IndexedDB 64
IndexedDB Objects and Method Chaining 65
Opening a Database 65
Adding Objects in a Transaction 66
Retrieving Objects by Using an Index 68
Mailing List Example 68
Handling Forms 69
Adding and Updating Records 71
Deleting Records 72
Counting Records 72
Searching and Ranges 73
Deleting a Database 76
Importing Data 77
Exporting Data 78
Chapter Summary 80
4. Networking and Messaging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Socket API 81
How BSD Sockets Works 82
Chrome Socket API 82
Modules and Module Loading 86
Wrapping the Socket API as an Object in a Module 88
A Weather Report App 91
The XMLHttpRequest API 94
Identity API 96
Accessing the Facebook API 97
vi | Table of Contents
A Facebook Photos App 102
Accessing Google APIs 106
WebSockets 109
WebSockets API 110
Example WebSocket Server 110
Example WebSocket Client 111
Google Cloud Messaging 114
Example Application Server 116
Example Client 116
Summary of IDs, Numbers, and Keys 120
App-to-App Messaging 121
Notifications 123
Chapter Summary 125
5. Graphics and Imaging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Outputting Graphics 127
Calendar Example 127
Rendering the Calendar as Text 131
Rendering the Calendar as a Table 133
Rendering the Calendar as Positioned HTML 140
HTML5 Canvas 142
A Simple Canvas Example 143
Drawing the Calendar by Using a Canvas 147
Scalable Vector Graphics (SVG) 149
Another SVG Example 150
Differences Between Canvas and SVG 152
Drawing the Calendar by Using SVG 154
Creating PDFs 157
A Simple PDF Example 158
Outputting the Calendar as a PDF 164
Working with Images 167
Accessing Image Thumbnails and Metadata 167
Image Rendering Quality 171
The mediaGalleries API 173
The Principal mediaGalleries API Methods 173
A Media Browser 176
Chapter Summary 185
6. Miscellaneous APIs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Alarms 187
Context Menus 190
Location 192
Table of Contents | vii
Text-to-Speech 194
System Queries 197
Camera 200
Bluetooth, USB, and Serial APIs 202
Internationalization and Localization 207
Internationalizing and Localizing Strings 208
Internationalizing Numbers 212
Internationalizing Dates 214
Chapter Summary 217
A. Modal Dialogs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
B. Registering Chrome Apps with Google. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
C. Using Google Cloud Messaging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
D. Using Cordova to Build Chrome Apps for Mobile Devices. . . . . . . . . . . . . . . . . . . . . . . . . 243
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
viii | Table of Contents
Description:O'Reilly Media, 2015. — 274 p. — ISBN-10: 1491904283, ISBN-13: 978-1491904282.На англ. языке.Author Marc Rochkind takes you through a hands-on, objective tour of Chrome Apps, which run on any platform that supports the Chrome browser—including OS X, Windows, Linux, as well as Androi