原生应用在Chrome扩展程序中无法使用 [英] Native app does not work in Chrome extension
问题描述
原生应用的Manifest.json:
{
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:\\\\\\\\\\\\\\\\\\\\\\\\\' >
我在Chrome扩展程序manifest.json中将nativeMessaging添加到权限。
$ b 原生应用程序cpp:
#include< iostream>
#include< string>
#include< fstream>
使用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<<消息< endl;
getline(cin,input);
cout<< 您输入了:<<输入< ENDL;
ofstream myfile;
myfile.open(example.txt);
myfile<< 将此文件写入文件。\ n;
myfile<<输入;
myfile.close();
返回0;
}
完成之后,我尝试使用我的Chrome扩展程序:
var testport = chrome.runtime.connectNative('app.native');
testport.onMessage.addListener(function(msg){
console.log(Received+ msg);
});
testport.onDisconnect.addListener(function(){
console.log(Disconnected);
});
它无法收到任何消息,并始终打印已断开连接。
我试图连接到一个不存在的应用程序,它仍然打印断开连接,所以我知道这个本地应用程序没有配置正确。
任何人都可以指出什么是错的或我错过了什么? 解决方案
默认情况下,cout是一个文本流,发送null作为你的大小的第一个4字节的一部分)尽早结束你的文本流。
在Windows上,你可以通过更改底层stdout来将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 ::冲洗;
I am trying Chrome Native Messaging API for Chrome extension.
Manifest.json for native app:
{
"name": "app.native",
"description": "Native Message API Test.",
"path": "native.exe",
"type": "stdio",
"allowed_origins": ["chrome-extension://kembignchdjhopkkcolnamikcenaocdm/"]
}
Windows Registry value:
HKEY_LOCAL_MACHINE\SOFTWARE\Google\Chrome\NativeMessagingHosts\app.native=D:\connectNative\manifest.json
I also tried D:\\\\connectNative\\\\manifest.json
And I add "nativeMessaging" to "permissions" in Chrome extension manifest.json.
Native app 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;
}
After all is done, i try in my Chrome extension:
var testport = chrome.runtime.connectNative('app.native');
testport.onMessage.addListener(function(msg) {
console.log("Received" + msg);
});
testport.onDisconnect.addListener(function() {
console.log("Disconnected");
});
It cannot receive any message and always print "Disconnected".
I try to connect to a non-existing app, it still print "Disconnected", so I know this native app is not configured right.
Can anyone point out what is wrong or what i missed?
By default cout is a text stream, sending null (that happens as part of your size first 4 bytes) ends your text stream early.
On Windows you can update cout to be binary by changing the underlying stdout, and don't forget to flush...
_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;
这篇关于原生应用在Chrome扩展程序中无法使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!