mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-16 06:18:35 +02:00
34 lines
925 B
JavaScript
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");
|
|
});
|