Coder Social home page Coder Social logo

simov / native-messaging Goto Github PK

View Code? Open in Web Editor NEW
47.0 2.0 12.0 6 KB

Native Messaging Host Protocol for Browser Extensions

License: MIT License

JavaScript 100.00%
nodejs javascript chrome firefox chrome-extension firefox-extension firefox-addon browser-extension native-messaging

native-messaging's Introduction

native-messaging

npm-version

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

native-messaging's People

Contributors

simov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

native-messaging's Issues

Send message from nodejs without port

Hi,
sorry for my english.
Is there a way to send a message from nodejs without having a port.
I would like to run a nodejs script to send message to the extension.
Is it possible?
I would appreciate any kind of help.

Issue handling the message from the browser

Hi, sorry for my english.
Sometimes the module can handle the request like so:
normal
other times it splits the message in two parts and can't pass your if statement like so:
issue.
PS. This happens in the same process with the same postMessage the only thing that change is the time ๐Ÿ˜„.

Does not send messages from host to browser equal or to 65536 length

Modify the following:

example.js

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

background.js

port.postMessage(new Array(13107))

function handleMessage (req) {
  // if (req.message === 'pong') {
  //  console.log(req)
  // }
  console.log(req);
}

Notice the message is not sent to the browser.

See

Support for Windows

The messaging host file uses a shebang which is not supported on windows. How do I get this working on windows?

Is there a way to detect in the node process that the extension has been disabled/uninstalled?

When I'm using only a node process and reload the chrome extension, it also reloads (so there is still only 1 node process after each extension reload).
But when I try to spawn a python process from the node process, this node process isn't reloaded alongside the extension and continues to exist, which results in many node and python processes in Task Manager. Deleting the node process manually removes a python process attached to it.
Is there a way to detect in the node file that the extension has been uninstalled/disabled?

Problem handling response

Hi,
sorry for my english.
The nodejs file is called but it handles the response only the first time the browser sends a message.
Moreover the nodejs file can't send back a response, it receives the request but can't comunicate back to the extension, it doesn't throw any errors and the extension doesn't fire "disconnect" event on the port.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.