npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

native-messaging

v1.0.0

Published

Native Messaging Host Protocol for Browser Extensions

Downloads

20

Readme

native-messaging

Native Messaging Host Protocol for Browser Extensions (Chrome, Firefox)

API

#!/home/s/.nvm/versions/node/v8.11.2/bin/node

// Might be good to use an explicit path to node on the shebang line
// in case it isn't in PATH when launched by Chrome/Firefox

var sendMessage = require('native-messaging')(handleMessage)

function handleMessage (req) {
  if (req.message === 'ping') {
    sendMessage({message: 'pong', body: 'hello from nodejs app'})
  }
}

Tips

  • chrome.runtime.connectNative returns a Port that can be used to establish persistent connection between the browser and the native app
  • the Port's disconnect method can be used to disconnect from the native app
  • the native app can use the OS's machinery to kill its own process at any time
  • chrome.runtime.sendNativeMessage can be used to send single message to the native app, useful with non persistent background pages
  • single native app can communicate with multiple browser extensions
  • single browser extension can communicate with multiple native apps

Things to look for

  1. Add nativeMessaging permission to the manifest.json file of your extension
  2. Load the extension to get its ID generated
  3. Put the native.messaging.example.json in ~/.config/google-chrome/NativeMessagingHosts/ or ~/.mozilla/native-messaging-hosts/
  • The name of the file should be identical to the name field specified in that file
  • Only dots . are allowed as delimiters in the file name
  • The path key should be absolute path to your nodejs script example.js
  • Chrome - the allowed_origins key should contain the extension's ID loaded in step 2
  • Firefox - the allowed_extensions key should contain the extension's ID specified in its manifest.json file
  1. Make sure that example.js is executable chmod 755
  2. Make sure nodejs is reachable from the shebang line in example.js
  3. Use the name specified in native.messaging.example.json to connect to the native app from the extension's background page background.js

Note that the install location for the native.messaging.example.json differs for each OS and browser. In case you plan to support multiple platforms, it's best to use install script. Here is an example of such script.

References

Topic | Chrome | Firefox :-- | :--- | :--- Native Messaging Host Protocol | link | link chrome.runtime.connectNative | link | link Port | link | link chrome.runtime.sendNativeMessage | link | link host.json location | link | link official example | link | link