Native messaging enables an extension to exchange messages with a native application, installed on the user's computer. The native messaging serves the extensions without additional accesses over the web.
Password managers: The native application manages, stores, and encrypts passwords. Then the native application communicates with the extension to populate web forms.
Native messaging also enables extensions to access resources that are not accessible through WebExtension APIs (e.g., particular hardware).
The native application is not installed or managed by the browser. The native application is installed, using the underlying operating system's installation machinery. Create a JSON file called the "host manifest" or "app manifest". Install the JSON file in a defined location. The app manifest file will describe how the browser can connect to the native application.
The extension must request the "nativeMessaging"
permission or optional permission in the manifest.json
file. Also, the native application must grant permission for the extension by including the ID in the "allowed_extensions"
field of the app manifest.
After installing, the extension can exchange JSON messages with the native application. Use a set of functions in the runtime
API. On the native app side, messages are received using standard input (stdin
) and sent using standard output (stdout
).
Support for native messaging in extensions is mostly compatible with Chrome, with two main differences:
- The app manifest lists
allowed_extensions
as an array of app IDs, while Chrome listsallowed_origins
, as an array of"chrome-extension"
URLs. - The app manifest is stored in a different location compared to Chrome.
There's a complete example in the "native-messaging
" directory of the "webextensions-examples"
repository on GitHub. Most example code in this article is taken from that example.