Ok, hab es. Ich fand einen guten Start here und here.
Ich benutze eine Nachricht für die Kommunikation zwischen meinem Browser-Aktion und Hintergrund-Skript.
Denken Sie an ein Spiel, in dem Sie im Popup-Fenster des Browsers agieren können und der Spielstatus im Hintergrundskript ist. Hier ist ein Beispiel für das Erhalten Anzahl von Münzen (Spieler Geld) aus dem Hintergrund Skript an die Browser-Aktion:
Browser-Aktion:
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;
});
Hintergrund Skript:
// 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);
});
}
}