Q:

WebExtension: How can I access the background script in my browser action

I'm totally new to WebExtension (trying to use them under Firefox). I've written a browser action. In order to keep a persistent state I figured that I have to implement a background script.
How can I acccess variables defined in the background script from my browser-action script?
Or is the assumption wrong that the background script can contain the state for the browser action?

A:

Ok, got it. I found a good start here and here.
I use message posting for communication between my browser-action and background script.
Think of a game where you can act in the browser action popup and the game state is in the background script. Here is an example for getting number of coins (player money) from the background script to the browser-action:
browser-action:
var _playerCoins = 0;

// I connect a 'port' with the name 'getCoins'.
var _port = chrome.runtime.connect({name: "getCoins"});

// This is the message that is called if the other side posts a message via the port.
// The background script puts the current amount of coins into the message
_port.onMessage.addListener(function(msg) {
    // Save the number of coins in a local variable
    _playerCoins = msg;
    // Display number of coins on my browser action html page
    document.getElementById("coins").innerHTML="Coins: " + _playerCoins;
});

background script:
// Add a listener for port connections
chrome.runtime.onConnect.addListener(function(port) {
    // If there is a 'getCoins' connection coming in...
    if(port.name == "getCoins") {
        // ...add a listener that is called when the other side posts a message on the port.
        port.onMessage.addListener(function(msg) {
            port.postMessage(_playerCoins);
        });
    }
}

