google-chrome-extension Getting started with google-chrome-extension Background Page


Example

Background pages are implicit pages which contain background scripts. A background script is a single long-running script to manage some task or state. It exists for the lifetime of your extension, and only one instance of it at a time is active.

You can declare it like this in your manifest.json:

"background": {
    "scripts": ["background.js"]
}

A background page will be generated by the extension system that includes each of the files listed in the scripts property.

You have access to all permitted chrome.* APIs.

There are two types of background pages: persistent background pages which is always open, and event pages that is opened and closed as needed.

If you want your background page to be non-persistent, you just have to set the persistent-flag to false:

"background": {
    "scripts": ["eventPage.js"],
    "persistent": false
}

This background script is only active if an event is fired on which you have a listener registered. In general you use a addListener for registration.

Example: The app or extension is first installed.

chrome.runtime.onInstalled.addListener(function() {
   console.log("The Extension is installed!");
});