Google chrome extension 本机应用程序在Chrome扩展中不工作
我正在为Chrome扩展尝试Chrome本机消息API 本机应用程序的Manifest.json:Google chrome extension 本机应用程序在Chrome扩展中不工作,google-chrome-extension,chrome-native-messaging,Google Chrome Extension,Chrome Native Messaging,我正在为Chrome扩展尝试Chrome本机消息API 本机应用程序的Manifest.json: { "name": "app.native", "description": "Native Message API Test.", "path": "native.exe", "type": "stdio", "allowed_origins": ["chrome-extension://kembignchdjhopkkcolnamikcenaocdm/"] } Windo
{
"name": "app.native",
"description": "Native Message API Test.",
"path": "native.exe",
"type": "stdio",
"allowed_origins": ["chrome-extension://kembignchdjhopkkcolnamikcenaocdm/"]
}
Windows注册表值:HKEY_LOCAL_MACHINE\SOFTWARE\Google\Chrome\NativeMessagingHosts\app.native=D:\connectNative\manifest.json
我还尝试了D:\\\\connectNative\\\\manifest.json
我在Chrome扩展manifest.json的“权限”中添加了“nativeMessaging”
本机应用程序cpp:#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(int argc, char* argv[]) {
string input = "";
string message="{\"text\": \"This is a response message\",\"num\": \"three\"}";
unsigned int len = message.length();
cout << char(((len>>0) & 0xFF))
<< char(((len>>8) & 0xFF))
<< char(((len>>16) & 0xFF))
<< char(((len>>24) & 0xFF));
cout << message <<endl;
getline(cin, input);
cout << "You entered: " << input << endl;
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile << input;
myfile.close();
return 0;
}
它无法接收任何消息并始终打印“断开”
我尝试连接到一个不存在的应用,它仍然打印“断开”,所以我知道这个本机应用配置不正确
有人能指出哪里错了或我遗漏了什么吗?带有本机引用的工作脚本示例。请注意
nativeMessaging
的权限,并且在Manifest.json
中不直接引用外部资源。该引用稍后在.js
脚本中
{
"background": {
"scripts": [ "common.js", "filler.js", "background.js" ]
},
"browser_action": {
"default_icon": "r.png",
"default_title": "Click this button to show commands"
},
"content_scripts": [ {
"all_frames": true,
"js": [ "common.js", "content.js", "filler.js" ],
"matches": [ "http://*/*", "https://*/*", "file:///*" ],
"run_at": "document_start"
} ],
"description": "For Google Chrome",
"homepage_url": "http://www.app.com",
"icons": {
"128": "r.png",
"16": "r.png",
"32": "r.png",
"48": "r.png"
},
"key": "???",
"manifest_version": 2,
"name": "???",
"options_page": "options.html",
"permissions": [ "tabs", "bookmarks", "webRequest", "webRequestBlocking", "nativeMessaging", "downloads", "http://*/*", "https://*/*" ],
"update_url": "https://clients2.google.com/???/",
"version": "???"
}
默认情况下,cout是一个文本流,发送null(作为前4个字节大小的一部分)会提前结束文本流 在Windows上,您可以通过更改基础标准输出将cout更新为二进制,并且不要忘记刷新
_setmode(_fileno(stdout), _O_BINARY);
int len = msg.length();
std::cout << char(len >> 0)
<< char(len >> 8)
<< char(len >> 16)
<< char(len >> 24);
std::cout << msg << std::flush;
\u setmode(\u fileno(stdout),\u O\u BINARY);
int len=msg.length();
标准::cout>0)
> 8)
> 16)
> 24);
std::难道你没有说native.exe在哪里吗。是在D:\connectNative\native.exe吗?是的,我尝试了native.exe的相对路径和绝对路径,两者都不起作用。我认为问题在于注册表项:这是我的.reg文件内容:Windows注册表编辑器版本5.00[HKEY\U LOCAL\U MACHINE\SOFTWARE\Google\Chrome\NativeMessagingHosts\com.Windows.screen]@=“C:\\Users\\abc\\Desktop\\Chrome Extension\\manifest.json”您的本机应用程序也没有发送正确的头。有关更好的实现,请参阅此问题。
_setmode(_fileno(stdout), _O_BINARY);
int len = msg.length();
std::cout << char(len >> 0)
<< char(len >> 8)
<< char(len >> 16)
<< char(len >> 24);
std::cout << msg << std::flush;