How to send message FROM native app TO Chrome extension?

后端 未结 3 1330
醉话见心
醉话见心 2020-12-03 06:13

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:

3条回答
  •  一生所求
    2020-12-03 06:27

    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.

提交回复
热议问题