how to write host for chrome extension native message in nodejs

Now, I develop a Chrome Extension need nativeMessage。Doc has a Python host demo, I now need a Nodejs demo.

I find a repo: https://github.com/jdiamond/chrome-native-messaging, but its demo(my_host.js) occur Failed to connect: Native host has exited.

Or,how to tranlate this python demo into Nodejs.

thx

How do I use data received from native app in html page?

I use native messaging extension. I send a string to native app,so encrypt it in my native app and send encrypted string to web page.

This is my content_script.js:

window.addEventListener("message", function(event)  {
  if (event.source == window &&
      event.data &&
      event.data.direction == "from-page-script") {
    alert("send");
    browser.runtime.sendMessage(event.data);
}
});



function Sign(challenge) {            
            var j_Data = {}
            j_Data["command"] = "sign";
            j_Data["challenge"] = challenge;
            var j_Text = JSON.stringify(j_Data)
            window.postMessage
                ({
                    direction: "from-page-script",
                    message: j_Text,
                }, "*");

            //response = document.getElementById("result").value;
        }


/*
Send a message to the page script.
*/
function messagePageScript(request) {
    var str_Text = request["text"];
    var j_Text = JSON.parse(strText);
    var str_Command = jText["command"];

    if(str_Command == "signed")
    {
        var str_Signed_Data = jText["signeddata"];

    }       
    }


browser.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    messagePageScript(request);
});

It work correctly.

when I receive a message messagePageScript(request); is called.

How do I use value of str_Signed_Data in my html page?

start an external application from a Chrome browser

How can we start an external application(exe) from a chrome browser?

I have gone through this link which says we can open it from a chrome extension:
Start an external application from a Google Chrome Extension? as I have found few extensions which when installed starts my external exe(application).

However, I would like to open the application directly from chrome without any extension.

Is it possible? Is Native Messaging something to look in that case?

Chrome Error: “Error when communicating with the native messaging host”

We are using Google Chrome versiyon 61.0.3163.100. We getting an error “Error when communicating with the native messaging host” but we do not get this error constantly. We detected that error is always throwing after “p_open_customer” message:

background.js:195 Sent message: {"message":{"raiseEvent":"document.getElementById(("myiframe0")).contentWindow.document.formsapplet.raiseEvent("p_close_customer","1#");"}}
background.js:195 Sent message: {"message":{"raiseEvent":"document.getElementById(("myiframe0")).contentWindow.document.formsapplet.raiseEvent("p_set_call_status","1#IS_ACTIVE=Y");"}}
background.js:195 Sent message: {"message":{"raiseEvent":"document.getElementById(("myiframe0")).contentWindow.document.formsapplet.raiseEvent("p_open_customer","1#CUSTNO=123456789#SECURITYLEVEL=0#CONNID=20180419111813916511#CALLTYPE=IND#FOOPROPERTY1=0#FOOPROPERTY2=null");"}}
background.js:92 tabs.onRemoved -- tab: 22
background.js:219 Failed to connect: Error when communicating with the native messaging host.
background.js:92 tabs.onRemoved -- tab: 19  

this application is used in call center therefore there are about 1000 users using very actively at the same time. We wonder which cases can trigger this error. I googled for the error from chrome developer site and it explains this error but we couldn’t find which case is suitable for us:

**Error when communicating with the native messaging host.** This is a very common error and indicates an incorrect implementation of the communication protocol in the native messaging host. 
• Make sure that all output in stdout adheres to the native messaging protocol. If you want to print some data for debugging purposes, write to stderr. 
• Make sure that the 32-bit message length is in the platform's native integer format (little-endian / big-endian). 
• The message length must not exceed 1024*1024. 
• The message size must be equal to the number of bytes in the message. This may differ from the "length" of a string, because characters may be represented by multiple bytes. 
• Windows-only: Make sure that the program's I/O mode is set to O_BINARY. By default, the I/O mode is O_TEXT, which corrupts the message format as line breaks (n = 0A) are replaced with Windows-style line endings (rn = 0D 0A). The I/O mode can be set using __setmode. 

our background.js code for chrome extension:

function sendNativeMessage(message) {
    if(port){
        port.postMessage(message);
        console.log("Sent message: " + JSON.stringify(message) + "");
    }
}

function onDisconnected() {
    console.log("Failed to connect: " + chrome.runtime.lastError.message);
    port = null;
}

function connect(message) {
    var hostName = "com.foo.applet";
    console.log("Connecting to native messaging host " + hostName + "");
    port = chrome.runtime.connectNative(hostName);
    port.onMessage.addListener(onNativeMessage);
    port.onDisconnect.addListener(onDisconnected);
    port.postMessage(message);
}

Any suggestions about that case?

Install chrome native client (nacl)

I am trying to install chrome native client.
I went to this https://developer.chrome.com/native-client/sdk/download page to download and install the sdk. I followed the instructions in this page however when I tried to execute the naclsdk list commnad or any naclsdk command I got an error –

third_party.fancy_urllib.InvalidCertificateException: Host storage.googleapis.com returned an invalid certificate ([SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)):
To learn more, see http://code.google.com/appengine/kb/general.html#rpcssl
Usage: sdk_update_main.py [options]

what is the problem here? ,is there a problem with a certificate?

thanks

Native Messaging in Chrome WebExtension: Waiting for async function

I am developing a WebExtension for Chrome. The purpose of this extension is to detect redirects and sending the redirect url to a native application. The native application decides if the redirect url should be called or should be modified. If the native application responded with “modify”, the redirect url should be changed, e.g., to about:blank. Otherwise the redirect should be called normally.

I am using the onHeadersReceived event to detect redirects with status code 302. The redirect url is handed over to a native application via native messaging.

chrome.webRequest.onHeadersReceived.addListener( ( details ) =>
{
    if ( details.statusCode == 302 )
    {
        var respHeaders = details.responseHeaders;
        //var calledUrl = details.url; // url called by the user
        var redirectUrl;
        blockingResponse = {};

        // Get redirectUrl
        var index = 0;
        for( var i = 0; i < respHeaders.length; ++i )
        {
            if( respHeaders[i].name == 'Location' )
            {
                redirectUrl = respHeaders[i].value;
                index = i;
                break;
            }
        }

        chrome.runtime.sendNativeMessage( "myNativeApp", { text : redirectUrl },
        function( response )
        {
            responseOfApp = response.text;
            if ( responseOfApp == "modify" )
            {
                    details.responseHeaders[index].value = "about:blank";
                    blockingResponse.responseHeaders = details.responseHeaders;
                    blockingResponse.redirectUrl = "about:blank";
            }
        });
    }

    return blockingResponse;

},
{urls: [ "" ]},['responseHeaders','blocking']);

The following problem is facing me:

The chrome.runtime.sendNativeMessage call is asynchronous. If the response is “modify” the blockingResponse is returned before “about:blank” is set in the blockingResponse.

How can be ensured that blockingResponse is returned not until the chrome.runtime.sendNativeMessage call finished?

Any help will be appreciated, thanks!

Limit Google Chrome extension to select platforms

I have an extension which talks to a native application. This application only works on macOS or Windows. But when I add the extension to my browser, it is also synchronized automatically to platforms where it does not make sense such as Linux or ChromeOS.

I’d figured there would be an option in the manifest.json to limit the app to specific platforms but I can’t find anything like that. Am I correct? Or is there a solution?

chrome.runtime.connectNative is undefined

I’m building a chrome extension which should connect to a console application.

This is my manifest file:

{
  "name": "my extension",
  "version": "0.0.1",
  "manifest_version": 2,
  "description": "my extension",
  "icons": {
    "16": "icons/icon16.png",
    "48": "icons/icon48.png",
    "128": "icons/icon128.png"
  },
  "default_locale": "en",
  "permissions": [
    "nativeMessaging"
  ],
  "content_scripts": [
    {
      "matches": [
        ""
      ],
      "js": [
        "src/inject/inject.js"
      ]
    }
  ]
}

I have this line in inject.js:

chrome.runtime.connectNative("org.my_company.my_application");

which throws a type error because connectNative is undefined, how could it be undefined when I have the permission for native messaging ? what should I do to solve the problem ?

By the way I’ve seen this question: Google Chrome Native Messaging Example returns: Uncaught TypeError: chrome.runtime.connectNative is not a function but the question is for chrome app and I’m building an extension.

my chrome version is 64 on windows 7 x64.

Chrome Native Messaging does not work on Windows if the user is not an administrator

I developed a Chrome extension that uses Native Messaging communication.

I am working in a Windows environment. Everything works fine if I am logged in as an administrator user. But when I log in as a non-administrator user, the extension does not connect to the host application anymore. It simply does not work.

No error messages are shown at the extension’s background page log.

If I enable chrome logging, by launching Chrome from the terminal using the --enable-logging --v=1 flag, no messages are shown when the extension tries to connect to the host application.

My manifest file:

{
  "description": "App",
  "path": "app.bat",
  "name": "com.example.app",
  "type": "stdio",
  "allowed_origins": [
    "chrome-extension:///"
  ]
}

My app.bat file:

@echo off

call java -jar app.jar

Is anyone able to make Native Messaging work when logged in as a non-administrator user?

Can we publish a chrome extension with only native messaging feature included?

I created an extension that can communicate with my native C # application using Google Chrome native messaging.

However, Google’s Chrome Webstore policy states: “Do not publish an app whose primary functionality is to install or launch another app, theme, or extension, for example, if the primary function of your app is to launch a desktop application. installed, this is not allowed Another example is a packaged application that launches a website. “

So, how can I use native messaging and publish my extension on the webstore?

Thanks