Skip to content

guest271314/NativeMessagingHosts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

224 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NativeMessagingHosts

  • Python
  • C
  • C++
  • QuickJS
  • txiki.js
  • Node.js
  • Deno
  • Bun
  • WebAssembly/WAT/WASI (C, C++, AssemblyScript, Bytecode Alliance Javy, Rust, Zig, Go, loopdive/js2wasm, ComponentizeJS, componentize-qjs, D)
  • Bash
  • SpiderMonkey js shell
  • V8 d8 shell
  • TypeScript
  • Amazon Web Services - Labs LLRT (Low Latency Runtime)
  • Rust
  • tee command (Busybox; GNU Coreutils)
  • Static Hermes
  • AssemblyScript
  • Bytecode Alliance Javy
  • Go
  • Zig
  • Lua
  • Ruby
  • PHP
  • D

Tested echoing new Array(209715) (1 MB in JSON format) from client to host.

Native messaging documentation

Native messaging protocol (Chrome Developers)

Chrome starts each native messaging host in a separate process and communicates with it using standard input (stdin) and standard output (stdout). The same format is used to send messages in both directions; each message is serialized using JSON, UTF-8 encoded and is preceded with 32-bit message length in native byte order. The maximum size of a single message from the native messaging host is 1 MB, mainly to protect Chrome from misbehaving native applications. The maximum size of the message sent to the native messaging host is 64 MiB.

The first argument to the native messaging host is the origin of the caller, usually chrome-extension://[ID of allowed extension]. This allows native messaging hosts to identify the source of the message when multiple extensions are specified in the allowed_origins key in the native messaging host manifest.

Installation and usage

Installation and usage on Chrome and Chromium

  1. Navigate to chrome://extensions.
  2. Toggle Developer mode.
  3. Click Load unpacked.
  4. Select native-messaging-<host> folder, e.g., native-messaging-c.
  5. Note the generated extension ID.
  6. Open, for example, nm_c.json in a text editor, set "path" to absolute path of nm_c.wasm and chrome-extension://<ID>/ using ID from 5 in "allowed_origins" array.
  7. Copy the nm_c.json file to Chrome or Chromium configuration folder, e.g., Chromium on *nix ~/.config/chromium/NativeMessagingHosts; Chrome dev channel on *nix ~/.config/google-chrome-unstable/NativeMessagingHosts.
  8. Make sure wasmtime for WASM host, or the single host file, e.g. when using QuickJS nm_qjs.js, are executable.
  9. To test click service worker link in panel of unpacked extension which is DevTools for background.js in MV3 ServiceWorker, observe echo'ed message from Native Messaging host to client in DevTools console. To disconnect run port.disconnect().

For differences between OS and browser implementations see Chrome incompatibilities. For Firefox or Nightly usage see also Can't get response in Ubuntu #506.

Testing

nm_standalone_test.js tests the Native Messaging hosts outside of the browser. Example usage

deno -A nm_standalone_test.js ./nm_nodejs.js native-messaging-extension://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/

nm_browser_test.js tests the Native Messaging hosts inside of the browser. Add the extension ID of a given extension to each host manifest in "allowed_origins", run the code in DevTools in that given extension. Example usage

// Echo 1 MiB to/from each host 100 times, print average time of each host to complete `N` roundtrips
await nativeMessagingPerformanceTest(100 /* N, default = 10 */);

Examples

  • Capture system and specific audio output, stream output to browser. capture_system_audio
  • Send text or SSML to local eSpeak NG speech synthesis engine, stream output to browser native-messaging-espeak-ng
  • Turn local local server on and off in browser for TTS streaming deno-server
  • WAT embedded in a Bash shell script nm_c_wat.sh (see usage note at 10.)
  • Full duplex streaming from and to the browser over HTTP/2 with WHATWG fetch() and Streams (Deno version, Node.js version)
  • nm_host.js is intended to be JavaScript runtime agnostic, can be run by node, deno, bun, tjs; nm_typescript.ts derived from nm_host.js can be run by node, deno, bun
  • nm_bash_standalone.sh does not use subshells
  • rhasspy/piper local TTS stream native-messaging-piper
  • JavaScript in WASM using Bytecode Alliance's Javy nm_javy.js, see native-messaging-webassembly/README.md
  • Write files to local file system from Web pages native-messaging-file-writer

License

Do What the Fuck You Want to Public License WTFPLv2

About

Native Messaging hosts

Topics

Resources

License

Stars

28 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors