Pop-a-loon Docs
GithubWebstores
  • Pop-a-loon documentation
  • Architecture
  • Adding a new balloon
  • architecture
    • Background
    • Content scripts
    • Popup
  • balloons
    • Confetti
    • Default
    • Fast
    • Ghost
    • Gold
Powered by GitBook
On this page
  • Alarms
  • Alarm Types
  • Messages
  • Message Types
  • Important methods
  • Event listeners
Edit on GitHub
  1. architecture

Background

PreviousarchitectureNextContent scripts

Last updated 9 months ago

The background scripts handle events and perform tasks that require access to browser APIs.

Alarms

The background script creates and listens to alarms. There are currently two alarms:

  1. 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.

    1. Is there a spawn timeout?

    2. Should there be a spawn timeout? if so, set the spawn timeout.

    3. Is the browser ?

    4. Is there already a spawnBalloon alarm?

    If all checks pass, the background script will query all and pick one to send the to.

  2. 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.

Alarm Types

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.

Messages

  1. updateCounter: When this message is received, the background script will update the counter in the browser action badge.

  2. incrementCount: When this message is received, the background will send a request to the remote server with the popped balloo.

  3. setLogLevel: When this message is received, the background script will set the log level specified in the message.

Message Types

Important methods

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.

Event listeners

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 .

idle
active tabs
spawn-balloon script
runtime.reload
const.ts
browser messaging API
const.ts
onStartup
onInstalled
message types
onAlarm
alarms
onMessage
messages