gpt4 book ai didi

google-chrome-extension - 如何在 native 应用程序中处理 chrome.runtime.sendNativeMessage()

转载 作者:行者123 更新时间:2023-12-01 09:27:45 27 4
gpt4 key购买 nike

我正在本地消息传递主机上工作。我可以使用 api 启动我的自定义应用程序

var port = chrome.runtime.connectNative('com.my_company.my_application');

我可以使用 api 将消息发布到我的自定义应用程序
port.postMessage({ text: "Hello, my_application" });

我知道他们使用输入/输出流来发送和接收消息。
我的 native 应用程序(c 或 c++ exe)应该如何收到有关收到消息的通知
我应该处理哪个函数/事件来接收消息。

最佳答案

更新:

关于如何监听原生应用上的消息,它们被发送到stdio(目前这是Chrome扩展程序和原生应用之间唯一可用的通信 channel )。
看看这个 sample app 具有本地消息传递主机 implemented in python .

您监听在端口的 上注册监听器的消息 onMessage event .

使用 sendNativeMessag() 仅当您想要一次性通信(不是持久端口)时。在这种情况下,不要使用 chrome.runtime.connectNative(...) .相反,请执行以下操作:

var msg = {...};
chrome.runtime.sendNativeMessage("<your_host_id>", msg, function(response) {
if (chrome.runtime.lastError) {
console.log("ERROR: " + chrome.runtime.lastError.message);
} else {
console.log("Messaging host sais: ", response);
}
});

关于 的文档部分Native Messaging 非常详细,是一个很好的信息来源。

关于google-chrome-extension - 如何在 native 应用程序中处理 chrome.runtime.sendNativeMessage(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20237603/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com