如何在没有参数的情况下调用 Chrome 本地消息主机?
How to invoke Chrome native message host without arguments?
我正在使用 Chrome 的 native messaging API to connect to a native host I'm developing in Go with the Cobra library。本机应用程序有一个独立的 CLI(使用 Cobra 实现),并且裸命令(没有任何参数)开始通过标准输入侦听 JSON,这意味着 API 用于 Chrome与之互动。
但是,每次扩展程序向本机消息传递主机发出请求时都会失败(客户端会立即与进程断开连接)。当我使用 --enable-logging
标志启动 Chrome 时,我可以看到本机主机出现 unknown command "chrome-extension://cnjopnegooahjdngnkhiokognkdjiioc/" for "--native-app-name--"
错误。这是 Cobra 的错误消息,表示 "chrome-extension://cnjopnegooahjdngnkhiokognkdjiioc/" 被用作参数,这似乎意味着 Chrome 正在使用 app-name chrome-extension://cnjopnegooahjdngnkhiokognkdjiioc/
而不是 app-name
调用本机主机。
这是我在扩展程序中用来调用本机主机的代码:
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.
我正在使用 Chrome 的 native messaging API to connect to a native host I'm developing in Go with the Cobra library。本机应用程序有一个独立的 CLI(使用 Cobra 实现),并且裸命令(没有任何参数)开始通过标准输入侦听 JSON,这意味着 API 用于 Chrome与之互动。
但是,每次扩展程序向本机消息传递主机发出请求时都会失败(客户端会立即与进程断开连接)。当我使用 --enable-logging
标志启动 Chrome 时,我可以看到本机主机出现 unknown command "chrome-extension://cnjopnegooahjdngnkhiokognkdjiioc/" for "--native-app-name--"
错误。这是 Cobra 的错误消息,表示 "chrome-extension://cnjopnegooahjdngnkhiokognkdjiioc/" 被用作参数,这似乎意味着 Chrome 正在使用 app-name chrome-extension://cnjopnegooahjdngnkhiokognkdjiioc/
而不是 app-name
调用本机主机。
这是我在扩展程序中用来调用本机主机的代码:
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.