Background
Last updated
Last updated
The background scripts handle events and perform tasks that require access to browser APIs.
The background script creates and listens to alarms. There are currently two alarms:
spawnBalloon
: This alarm goes off at random intervals. When it goes off, the background script sends a content script to one of the active tabs to spawn a balloon. Because you can only specify a specific interval for alarms, e.g. every 5 minutes, the background script creates an alarm that goes off once and then creates a new alarm when that alarm was triggered. This new alarm will have a different random delay. This way, the balloons are spawned at random intervals.
When the spawnBalloon
the background script performs a series of checks before actually spawning a balloon.
Is there a spawn timeout?
Should there be a spawn timeout? if so, set the spawn timeout.
Is the browser ?
Is there already a spawnBalloon
alarm?
If all checks pass, the background script will query all and pick one to send the to.
restart
: When this alarm goes off, is called. This restarts the extension.
A restart
alarm is created when the remote server is not available or when any unexpected error occurs during setup. These alarms are created with a delay of one minute. This way, the extension will try to restart itself after a minute. Why a minute? Because the extension is not supposed to be restarted too often. If the remote server is not available, it probably also isn't available the next second, but maybe it is in one minute.
There is an AlarmName
type in that defines the alarm names. This way, there is type safety and the alarm names are consistent throughout the extension.
updateCounter
: When this message is received, the background script will update the counter in the browser action badge.
incrementCount
: When this message is received, the background will send a request to the remote server with the popped balloo.
setLogLevel
: When this message is received, the background script will set the log level specified in the message.
The background script has a few important methods initalized:
setup()
: The setup function where all setups happen.
spawnBalloon()
: A balloon is sent to a tab.
createSpawnAlarm(name)
: Creates a balloon spawn alarm. Is triggered after spawnBalloon()
was called.
backgroundScript()
: This is the 'main' function. This is the function that is initially called and calls the setup()
function.
These include most of the functionallity of the background.
The background also listens to some events.
Pop-a-loon uses the to communicate between different scripts.
These are the actions for the messages (see ):
There are a few types defined in and exported under the Message
type. They can be distinguished using the Message.action
property.
Alarms with . See .
Messages with . See .