This is a very simple example of how to use native messaging to exchange messages between a WebExtension and a native application.
The WebExtension, which can be found under "add-on", connects to the native application and listens to messages from it. It then sends a message to the native application when the user clicks on the WebExtension's browser action. The message payload is just "ping".
The native application, which can be found under "app", listens for messages from the WebExtension. When it receives a message, the native application sends a response message whose payload is just "pong". The native application is written in Python.
Setup
To get this working, there's a little setup to do.
Linux/macOS setup
-
(macOS) Make sure that you did not copy this extension into the Desktop, Documents, or Downloads folders in your home directory. macOS has access restrictions on these directories that will prevent the Python script from executing as expected.
-
Make sure that you have Python 3 installed, and that your system's PATH environment variable includes the path to Python by executing the following command:
> which python3 /usr/local/bin/python3If you don't see the path of the application as above, you most likely need to install Python 3 on your computer. See Using Python on Unix platforms or Using Python on a Mac. Restart the web browser after making this change in order for Firefox to pick up the new PATH environment variable.
-
Make sure that the file permissions for
app/ping_pong.pyinclude theexecutepermission. See this article by RedHat for more information. -
Update the
"path"field inapp/ping_pong.jsonto be the full path to yourapp/ping_pong.pyfile.For example, if you cloned this repository into
/Users/MDN/webextensions-examples/, you would update the file like this:"path": "/Users/MDN/webextensions-examples/native-messaging/app/ping_pong.py" -
Copy
app/ping_pong.jsonto the correct location on your computer. There are too many options to list here; see the Linux and macOS secitons of App manifest location to find the correct location for your OS and personal preference.
Windows setup
-
Make sure that you have Python 3 installed and that your system's PATH environment variable includes the path to Python. See Using Python on Windows. You'll need to restart the web browser after making this change, or the browser won't pick up the new environment variable.
-
Update the
"path"field inapp\ping_pong.jsonto use the full path ofapp\ping_pong_win.baton your computer. Be aware that you'll need to escape the Windows directory separator (\).For example, if you cloned this repository into
C:\Users\MDN\webextensions-examples\, you would update the JSON file like this:"path": "C:\\Users\\MDN\\webextensions-examples\\native-messaging\\app\\ping_pong_win.bat" -
Update
app\ping_pong_win.batto use the full path ofapp\ping_pong.pyon your computer. -
Add a registry key containing the full path of
app\ping_pong.jsonon your computer. See App manifest location to find details of the registry key to add.
To assist in troubleshooting on Windows, there is a script next to this README file named check_config_win.py. Running this in a command shell should help you discover of any problems.
Testing the example
First, install the add-on. Visit about:debugging#/runtime/this-firefox or, from about:debugging click "This Firefox" (or "This Nightly" in the Nightly version of Firefox), click "Load Temporary Add-on", and open the add-on's manifest.json.
Now, open the extension's console using the "Inspect" button - this is where you'll see communication between the browser and native app.
You should see a new browser action icon in the toolbar. Click it. You should see output like this in the console:
Sending: ping
Received: pong3
If you don't see this output, see the Troubleshooting guide for ideas.