How to send message FROM native app TO Chrome extension?
Asked Answered
M

3

11

I have read docs, but still cannot realize. I have desktop application written in C and Chrome extension. I know how to receive this message in my chrome extension:

port.onMessage.addListener(function(msg) {
    console.log("Received" + msg);
});

What should I write in my C application to send a message to my chrome extension? Python/NodeJS examples are also appropriate.

Mcginn answered 8/8, 2013 at 19:39 Comment(2)
Did you read developer.chrome.com/extensions/messaging.html#native-messaging?Departmentalism
I did, and it doesn't answer the question??Negate
M
15

In order for a native messaging host to send data back to Chrome, you must first send four bytes of length information and then send the JSON formatted message as a string/char-array.

Below are two examples for C and C++ respectively that do the same thing in slightly different ways.

C example:

#include <stdio.h>
#include <string.h>

int main(int argc, char* argv[]) {
    // Define our message
    char message[] = "{\"text\": \"This is a response message\"}";
    // Collect the length of the message
    unsigned int len = strlen(message);
    // We need to send the 4 bytes of length information
    printf("%c%c%c%c", (char) (len & 0xff),
                       (char) ((len>>8) & 0xFF),
                       (char) ((len>>16) & 0xFF),
                       (char) ((len>>24) & 0xFF));
    // Now we can output our message
    printf("%s", message);
    return 0;
}

C++ example:

#include <string.h>

int main(int argc, char* argv[]) {
    // Define our message
    std::string message = "{\"text\": \"This is a response message\"}";
    // Collect the length of the message
    unsigned int len = message.length();
    // We need to send the 4 bytes of length information
    std::cout << char(((len>>0) & 0xFF))
              << char(((len>>8) & 0xFF))
              << char(((len>>16) & 0xFF))
              << char(((len>>24) & 0xFF));
    // Now we can output our message
    std::cout << message;
    return 0;
}

(The actual message can be sent at the same time as the length information; it is merely broken out for clarity.)

So following the OP Chrome example, here is how to output the message:

port.onMessage.addListener(function(msg) {
    console.log("Received" + msg.text);
});

In reality, there is no requirement to use "text" as the key returned from your native messaging app; it could be anything. The JSON string passed to the listener from your native messaging app is converted to a JavaScript Object.

For a C++ example of a native messaging app that uses the above technique in combination with jsoncpp (C++ JSON library) and also parses the request sent to the app, see here: https://github.com/kylehuff/libwebpg/blob/22d4843f41670d4fd7c4cc7ea3cf833edf8f1baf/webpg.cc#L4501

Medina answered 8/11, 2013 at 18:45 Comment(1)
Dear @kylehuff, I want to use a c++ dll (winscard.dll) through native messaging for chrome browser extensions, however I could not make this connection. Is it possible to do this communication through native messaging?Lamont
K
8

You can take a look here, this is an example python script which sends and receives messages to the extension: http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/nativeMessaging/host/native-messaging-example-host?revision=227442

As far as I understand it, in order to send the message you need to:

  1. write to console the length of the message as bynary
  2. write three \0 characters
  3. write your message in plain text

this is the C# code that did the job for me:

String str = "{\"text\": \"testmessage\"}";

Stream stdout = Console.OpenStandardOutput();

stdout.WriteByte((byte)str.Length);
stdout.WriteByte((byte)'\0');
stdout.WriteByte((byte)'\0');
stdout.WriteByte((byte)'\0');
Console.Write(str);

And the python code from the above link:

sys.stdout.write(struct.pack('I', len(message)))
sys.stdout.write(message)
sys.stdout.flush()

Interestingly it doesn't explicitly output the three \0 characters, but they seem to appear after outputting the struct.pack, not sure why...

Also note that you need to send the message in JSON format, otherwise it doesn't seem to work.

Katabolism answered 11/10, 2013 at 1:18 Comment(1)
The first 4 bytes are actually and int containing the str length in bytes. Thus your solution will fail if str goes above 256 bytes. Also you might want to specify the output stream encoding to UTF-8, it might be the default but just in case.Maskanonge
M
1

I used function write:

write(1,buf,n);

buf is your message, n is length if your message You also can use printf.

Mcginn answered 25/9, 2013 at 10:32 Comment(2)
I am trying to work out this same problem. I see how I can receive the message but I am unclear on how to send it. Is this post implying that anything written to standard out would be passed along to any chrome app that is listening for messages? My native app is a .Net console application. Would that mean anything sent to Console.WriteLine would end up being passed to Chrome?Firstborn
@Firstborn at first i was also confused then reading the docs explained that it uses the stdio but the host has to be registered and you can specify the extension you allow communication with in the manifest file. developer.chrome.com/extensions/nativeMessagingGlabrate

© 2022 - 2024 — McMap. All rights reserved.