Skip to main content

A package with basic native messaging apis for webextensions

Project description

nativemessaging-ng

A Python package for interfacing with Native Messaging in WebExtensions. Based on the apparently unmaintained version of Rayquaza01, which is in turn based on Native Messaging on MDN and native-messaging on mdn/webextension-examples.

Native Messaging documentation

Installation

pip install nativemessaging-ng

Usage

Methods

The nativemessaging module exposes following methods:

  • log_browser_console(message) adds the given message to the browser log (accessible via ctrl-shift-J)

  • get_message_raw() polls for a message from the browser and returns the result as string. If runtime.connectNative is used, get_message() must be called repeatedly in a loop to poll for messages; if runtime.sendNativeMessage is used, get_message() only needs to be called once.

  • send_message_raw(message) sends the given string message to the browser.

  • get_message() polls for a message from the browser and returns the message as a json structure.

  • send_message(json_message) sends the given json structured message to the browser.

  • install(browsers, manifest_filename) takes a list of browsers to install the manifest and a manifest filename, to install the given manifest in the browser configuration. Supported browsers are 'chrome' and 'firefox'.

  • is_installed(application_name) returns the list of browsers for which the manifest of application_name is installed.

  • uninstall(browsers, application_name) uninstalls the manifest of application_name for all given browsers.

Example

Browser side:

function onReceived(response) {
    console.log(response);
}

// runtime.connectNative
var port = browser.runtime.connectNative("application_name");
port.onMessage.addListener(onReceived);
port.postMessage("hello");

// runtime.sendNativeMessage
browser.runtime.sendNativeMessage("application_name", "hello").then(onReceived);

Python application side:

import nativemessaging

while True:
    message = nativemessaging.get_message()
    if message == "hello":
        nativemessaging.send_message("world")

nativemessaging-install

nativemessaging-install is a command line script to install a manifest in the browser configuration.

usage: nativemessaging-install [-h] [--version] [--manifest MANIFEST]
                               [--appname NAME]
                               {install,verify,uninstall} BROWSER
                               [BROWSER ...]

positional arguments:
  {install,verify,uninstall}
                        action to take, can be install, verify or uninstall
  BROWSER               browser(s) for which the manifest will be installed,
                        valid values are chrome or firefox.

options:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  --manifest MANIFEST   path to the manifest file to install (default: native-
                        manifest.json)
  --appname NAME        application name to be verified or uninstalled

Manifest file

The format of the manifest file must be similar to the native manifest format for Chrome or Firefox, with two main differences:

  • path must be a relative path to the native app in relation to your current working directory.
  • Both allowed_extensions and allowed_origins must be in the manifest to work with both Chrome and Firefox.
{
    "name": "application_name",
    "description": "description",
    "path": "application_name.py",
    "type": "stdio",
    "allowed_extensions": ["extension@id"],
    "allowed_origins": ["chrome-extension://extension-id"]
}

Installed files

  • Windows:

    • <application_name>_firefox.json and <application_name>_chrome.json will be created in <path>
    • A batch file will also be created to run the python application on Windows
    • A registry key is created at HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts\<application_name> or HKEY_CURRENT_USER\Software\Mozilla\NativeMessagingHosts\<application_name>
  • Linux:

    • ~/.config/google-chrome/NativeMessagingHosts/<application_name>.json or ~/.mozilla/native-messaging-hosts/<application_name>.json will be created
  • OS/X:

    • ~/Library/Application Support/Google/Chrome/NativeMessagingHosts/<application_name>.json or ~/Library/Application Support/Mozilla/NativeMessagingHosts/<application_name>.json will be created

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nativemessaging-ng-1.3.1.tar.gz (11.7 kB view details)

Uploaded Source

File details

Details for the file nativemessaging-ng-1.3.1.tar.gz.

File metadata

  • Download URL: nativemessaging-ng-1.3.1.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for nativemessaging-ng-1.3.1.tar.gz
Algorithm Hash digest
SHA256 463b6acd7339525bd8429483ee8ac5e52799fadf1bef5e94c54e8346ee4fd020
MD5 848d60a1cf101928433edb3a0518abb4
BLAKE2b-256 6e7003ad5a44db3d16ea6c52865905049fe8a664058d161682ec23595d584ec7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page