如何从本机应用程序向 Chrome 扩展程序发送消息? [英] How to send message FROM native app TO Chrome extension?

查看:31
本文介绍了如何从本机应用程序向 Chrome 扩展程序发送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了文档,但仍然无法实现.我有用 C 和 Chrome 扩展编写的桌面应用程序.我知道如何在我的 chrome 扩展程序中接收此消息:

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

我应该在我的 C 应用程序中写什么来向我的 chrome 扩展程序发送消息?Python/NodeJS 示例也适用.

解决方案

为了让本地消息传递主机将数据发送回 Chrome,您必须先发送 4 个字节的长度信息,然后将 JSON 格式的消息作为字符串发送/char-array.

下面分别是 C 和 C++ 的两个示例,它们以略有不同的方式做同样的事情.

C 示例:

#include #include int main(int argc, char* argv[]) {//定义我们的消息char message[] = "{"text": "这是一条回复消息"}";//收集消息的长度unsigned int len = strlen(message);//我们需要发送4个字节的长度信息printf("%c%c%c%c", (char) (len & 0xff),(char) ((len>>8) & 0xFF),(char) ((len>>16) & 0xFF),(char) ((len>>24) & 0xFF));//现在我们可以输出我们的消息printf("%s", 消息);返回0;}

C++ 示例:

#include int main(int argc, char* argv[]) {//定义我们的消息std::string message = "{"text": "这是一条响应消息"}";//收集消息的长度unsigned int len = message.length();//我们需要发送4个字节的长度信息std::cout <<char(((len>>0) & 0xFF))<<char(((len>>8) & 0xFF))<<char(((len>>16) & 0xFF))<<char(((len>>24) & 0xFF));//现在我们可以输出我们的消息std::cout <<信息;返回0;}

(实际消息可以与长度信息同时发送;为了清楚起见,将其分开.)

所以按照 OP Chrome 示例,这里是如何输出消息:

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

实际上,没有要求使用text"作为从您的本地消息传递应用程序返回的键;它可以是任何东西.从您的本机消息传递应用程序传递给侦听器的 JSON 字符串将转换为 JavaScript 对象.

有关将上述技术与 jsoncpp(C++ JSON 库)结合使用并解析发送到应用程序的请求的本机消息传递应用程序的 C++ 示例,请参见此处:https://github.com/kylehuff/libwebpg/blob/22d4843f47a3cf8f8f1baf/webpg.cc#L4501">https://github.com/kylehuff/libwebpg/blob/22d4843f4fed67f>cc8f4167f8c5a3p3p5a3p3p8f8c5a3cf833edf8f1baf/

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.

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

这篇关于如何从本机应用程序向 Chrome 扩展程序发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆