我正在使用 Chrome 的 native messaging API要连接到我正在 Go 中开发的 native 主机,使用 Cobra library 。 native 应用程序有一个独立的 CLI(使用 Cobra 实现),并且裸命令(不带任何参数)开始通过 stdin 监听 JSON,这意味着 Chrome 可以与之交互的 API。
但是,每次扩展程序向 native 消息传递主机发出请求时都会失败(客户端会立即与进程断开连接)。当我使用 --enable-logging
标志启动 Chrome 时,我可以看到 native 主机因 未知命令“chrome-extension://cnjopnegooahjdngnkhiokognkdjiioc/”(对于“--native-”)出现错误应用程序名称--“
。这是 Cobra 的错误消息,意味着“chrome-extension://cnjopnegooahjdngnkhiokognkdjiioc/”被用作参数,这似乎意味着 Chrome 正在使用 app-name chrome-extension://cnjopnegooahjdngnkhiokognkdjiioc/调用 native 主机
而不仅仅是应用程序名称
。
这是我使用扩展程序调用 native 主机的代码:
var port = chrome.runtime.connectNative('app-name');
port.onMessage.addListener(function(msg) {
console.log(msg);
});
port.onDisconnect.addListener(function() {
console.log("disconnected");
});
port.postMessage({cmd:"ping"});
我找不到任何文档表明 Chrome 将扩展程序地址作为参数发送,或者是否可以阻止。
最佳答案
它是 protocol 的一部分并且无法禁用。 Windows 上的命令行是这样的:
C:\Windows\system32\cmd.exe /c YOURHOSTAPP.exe chrome-extension://.................../ --parent-window=6752474 < \\.\pipe\chrome.nativeMessaging.in.e11ed8be274e1a85 > \\.\pipe\chrome.nativeMessaging.out.e11ed8be274e1a85
The first argument to the native messaging host is the origin of the caller, usually
chrome-extension://[ID of whitelisted extension]
. This allows native messaging hosts to identify the source of the message when multiple extensions are specified in the allowed_origins key in the native messaging host manifest.
On Windows, the native messaging host is also passed a command line argument with a handle to the calling chrome native window:
--parent-window=<decimal handle value>
. This lets the native messaging host create native UI windows that are correctly focused.
Warning: In Windows, in Chrome 54 and earlier, the origin was passed as the second parameter instead of the first parameter.
关于google-chrome - 如何在不带参数的情况下调用Chrome native 消息主机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39683339/