Chapter 6. Miscellaneous APIs

This chapter discusses a grab-bag of Chrome APIs, none of which warrants a full chapter of its own. Some of them are pretty useful, and others you’ll want to read about but will probably never use.

Alarms

JavaScript has always had a way to set one-time or periodic alarms by using the setInterval and setTimeout functions, but their lifetime was limited to the lifetime of the program. When it stops, they’re gone.

With the Chrome alarm APIs, you can set an alarm that lasts as long as the app is installed, even if its background page goes inactive. You create such an alarm with chrome.alarms.create:

chrome.alarms.create(name, alarmInfo)

The second argument is an object that has the following three properties:

when
When the alarm should first fire, in milliseconds past the current time (Date.now())
delayInMinutes
How long to delay until the alarm first fires
periodInMinutes
How long to wait between firings

One of the when and delayInMinutes properties is required, and periodInMinutes should be present only if you want the alarm to fire more than once.

When an alarm fires, it triggers a chrome.alarms.onAlarm event, the callback function of which receives an Alarm object as its argument, indicating which alarm fired. Its most important property is name.

You clear an alarm by using chrome.alarms.clear:

chrome.alarms.clear(name, callback)

The callback function has a Boolean argument that indicates whether the alarm was cleared.

There are also APIs to get information ...

Get Programming Chrome Apps now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.