Programming Chrome Apps
Marc Rochkind
Beijing Cambridge Farnham Kln Sebastopol Tokyo
Dedication
To Valerie, Gillian, and Claire
Preface
Almost nobody reads past the first sentence or two of a preface, so Ill say the most important thing right away: this book is about programming Chrome Apps, not Chrome Extensions or web apps in general.
Still here? Theres some more important stuff to know, so please continue.
explains in detail what a Chrome App is, but briefly, its relationship to Chrome is the same as a Windows app to Windows, or a Mac app to OS X: an app that you install on the platform and that makes use of application program interfaces (APIs) unique to that platform. This would be obvious but for the fact that the platform in this case, Chrome, is better known as a web browser and most of the apps that you run on it are really just fancy web pages. Im thinking of Gmail, Facebook, Amazon, and a zillion others. Hence the confusion.
Why consider programming a Chrome App at all? Here are two good reasons:
- Theyre portable across all of the major operating systems: Windows, Mac OS X, Linux, Chrome OS (for Chromebooks and Chromeboxes), and, with some restrictions, even Android and iOS.
- Although web apps are equally as portable, they cant access the local computer as Chrome Apps can. The most important example of this is that Chrome Apps can access the computers filesystem, just as native apps can.
Im unable to think of any other platform that offers these two advantages.
With the good comes the not-so-good. Here are the chief disadvantages of Chrome Apps:
- On desktop operating systems, they run only under Chrome (although they can have their own launch icon and they appear to run standalone).
- Although the Chrome API is extensive and growing, it isnt even close to encapsulating every native API.
A disclaimer: Im not with Google, never have been, dont even know anyone who works on Chrome at Google, and Google didnt authorize this book. So, even though you wont get any inside info in this book, you will get an entirely objective viewpoint. If I think something is screwed up, Ill let you know.
What You Should Already Know
In the world of web-programming books, this ones fairly advanced, as I assume you already know JavaScript, HTML, CSS, and your way around the Document Object Model (DOM), which is a JavaScript object that represents the HTML elements of a web page. That said, my use of those technologies is fairly basic, so if youre rusty or dont mind jumping off a metaphorical cliff, you can read up on what Im assuming you know as you encounter it.
Some popular technologies sometimes used to write Chrome Apps, such as jQuery and the AngularJS framework, arent covered in this book, because they arent specific to Chrome Apps and because they would obscure the example code. That code might be clearer to those who know these tools, but it would greatly inconvenience everyone else. Rather, Ive tried to write the examples in the simplest possible way, using plain, unadulterated JavaScript. Another point is that cross-browser portability, an important benefit of these technologies ordinarily, is irrelevant when youre writing a Chrome App.
Why I Wrote This Book
I first wrote a Chrome Appthen called a Packaged Appa few years ago when they were first released by Google. At that time, they were an offshoot of Chrome Extensions, and the documentation had the terminology for Apps and Extensions throughly confused. I got my app running, but it was obvious that apps for Chrome had a long way to go.
By late 2013, things had been cleaned up a lot. They were now called Chrome Apps, there were a lot more APIs, and the documentation was better, though still not well edited.
Meanwhile, Samsung had introduced a $250 Chromebook. I bought one and realized that for most of what I did when I wasnt downstairs in my office, it was just as good as my MacBook Air. I wasnt the only one to feel this way: today, Chromebooks are at or near the top of Amazons laptop best-seller list.
So, heres the book I wish Id had when I started with Chrome Apps. Now that its in your hands, you can start way ahead of where I was.
Navigating This Book
At this point most prefaces describe whats in each chapter, but Ive always thought that was silly because the same information is in the Table of Contents. I will say this much: read all of carefully, even if youre in the habit of skipping the first chapter of any programming book, because thats where I explain how Chrome Apps are different from other things you run in a browser and, most important, their limitations relative to ordinary web pages. After that, Ive written the rest to be read sequentially because later chapters build on earlier ones, but you can certainly skip around if you want.
Theres lots of code in this book, but I usually explain it in small bites so that you can more easily digest it. That might make it hard for you to see whats going on in the app as a whole, so, as I go through an app, keep its code open in a text editor as a reference. See the section a bit later in this Preface for information on how to get the code.
Online Resources
Ill refer to URLs for documentation throughout this book, but the most important one is Googles official Chrome App site at developer.chrome.com/apps .Youll want to look around that site right away, and youll likely keep going back to it as you read this book.
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic Indicates new terms, URLs, email addresses, filenames, and file extensions. Constant width
Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.
Note
This element signifies a general note.
Using Code Examples
You can download supplemental material (code examples, exercises, and so on) at https://github.com/oreillymedia/programming_chrome_apps.
This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless youre reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from OReilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your products documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: Programming Chrome Apps by Marc Rochkind (OReilly). Copyright 2015 Marc Rochkind, 978-1-491-90428-2.
If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .
Safari Books Online
Note
Safari Books Online is an on-demand digital library that delivers expert content in both book and video form from the worlds leading authors in technology and business.