Files
webextensions-examples/native-messaging/add-on/background.js
2024-07-12 19:10:09 -07:00

34 lines
925 B
JavaScript

/*
On startup, connect to the "ping_pong" app.
*/
let port = browser.runtime.connectNative("ping_pong");
/*
Listen for messages from the app and log them to the console.
*/
port.onMessage.addListener((response) => {
console.log("Received: " + response);
});
/*
Listen for the native messaging port closing.
*/
port.onDisconnect.addListener((port) => {
if (port.error) {
console.log(`Disconnected due to an error: ${port.error.message}`);
} else {
// The port closed for an unspecified reason. If this occurred right after
// calling `browser.runtime.connectNative()` there may have been a problem
// starting the the native messaging client in the first place.
console.log(`Disconnected`, port);
}
});
/*
When the extension's action icon is clicked, send the app a message.
*/
browser.browserAction.onClicked.addListener(() => {
console.log("Sending: ping");
port.postMessage("ping");
});