Google chrome extension 如何将消息从本机应用发送到Chrome扩展?

Google chrome extension 如何将消息从本机应用发送到Chrome扩展?,google-chrome-extension,google-chrome-devtools,Google Chrome Extension,Google Chrome Devtools,我读过文件,但仍然无法理解。 我有一个用C和Chrome扩展写的桌面应用程序。我知道如何在我的chrome分机中接收此消息: port.onMessage.addListener(function(msg) { console.log("Received" + msg); }); 我应该在我的C应用程序中写些什么来向我的chrome扩展发送消息? Python/NodeJS示例也很合适。我使用了functionwrite: write(1,buf,n); buf是您的消息,n是您的消

我读过文件,但仍然无法理解。 我有一个用C和Chrome扩展写的桌面应用程序。我知道如何在我的chrome分机中接收此消息:

port.onMessage.addListener(function(msg) {
    console.log("Received" + msg);
});
我应该在我的C应用程序中写些什么来向我的chrome扩展发送消息?
Python/NodeJS示例也很合适。

我使用了functionwrite

write(1,buf,n);
buf是您的消息,n是您的消息的长度
您还可以使用printf。

您可以在此处查看,这是一个示例python脚本,用于向扩展发送和接收消息:

据我所知,为了发送信息,您需要:

  • 将消息的长度写为bynary
  • 写三个\0字符
  • 用纯文本写你的信息
  • 这是为我完成任务的C代码:

    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);
    
    以及上面链接中的python代码:

    sys.stdout.write(struct.pack('I', len(message)))
    sys.stdout.write(message)
    sys.stdout.flush()
    
    有趣的是,它没有显式输出这三个\0字符,但它们似乎是在输出struct.pack后出现的,不知道为什么


    还请注意,您需要以JSON格式发送消息,否则它似乎不起作用。

    为了让本机消息主机将数据发送回Chrome,您必须先发送四个字节的长度信息,然后以字符串/字符数组的形式发送JSON格式的消息

    下面是C和C++的两个例子,它们分别以稍微不同的方式做同样的事情。 C例如:

    #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;
    }
    
    实际上,不需要使用“文本”作为从本机消息传递应用程序返回的密钥;它可能是任何东西。从本机消息传递应用程序传递给侦听器的JSON字符串将转换为JavaScript对象


    一个本地消息应用程序的C++示例,它使用了上面的技术和JSONCPP(C++ JSON库)结合,并且解析了发送给应用程序的请求,请参见这里:

    你读过吗?我做了,并且它没有回答这个问题?我正在尝试解决这个问题。我知道如何接收信息,但不清楚如何发送。这篇文章是不是意味着任何写入标准输出的内容都会被传递给任何正在监听消息的chrome应用程序?我的本机应用程序是一个.Net控制台应用程序。这是否意味着发送到Console.WriteLine的任何内容最终都会被传递到Chrome?@Kywillis起初我也感到困惑,然后阅读文档解释说它使用stdio,但主机必须注册,您可以在清单文件中指定允许通信的扩展名。前4个字节实际上是和int,包含以字节为单位的str长度。因此,如果str超过256字节,您的解决方案将失败。另外,您可能希望将输出流编码指定为UTF-8,这可能是默认值,但只是以防万一。@phq感谢您的澄清!亲爱的@ KyHufff,我想通过Chrome浏览器扩展的本地消息传递使用C++ DLL(WiScARD.DLL),但是我不能进行这种连接。是否可以通过
    本机消息传递进行此通信?
    
    #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;
    }
    
    port.onMessage.addListener(function(msg) {
        console.log("Received" + msg.text);
    });