gpt4 book ai didi

javascript - webextension native 应用程序 c++ hello world

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:10:28 24 4
gpt4 key购买 nike

我想在 firefox 上创建一个 webextension,它与一个简单的 c++ 应用程序通信,我想向 c++ 应用程序发送一条消息,并接收一条简单的消息(例如 Hello world)。

现在这就是我所做的:

manifest.json(网络扩展)

{
"manifest_version": 2,
"name": "nameofwebextension",
"version": "1.0",

"description": "test",
"permissions": [
"tabs",
"activeTab",
"downloads",
"downloads.open",
"nativeMessaging"
],
"icons": {
"48": "icons/border-48.png"
},
"content_scripts": [
{
"matches": ["http://www.testwebsite.com/*"],
"js": ["content.js"]
}
],
"background": {
"scripts": ["background.js"]
},
"browser_action": {
"default_icon": "icons/beasts-32.png",
"default_title": "test",
"default_popup": "popup/test.html",
"browser_style": false
},

"applications": {
"gecko": {
"id": "name@example.org",
"strict_min_version": "50.0"
}
}

}

背景.js

var port = browser.runtime.connectNative("nameofapp");
console.log("Sending: ping");
port.postMessage("ping");
port.onMessage.addListener((response) => {
console.log("Received: " + response);
});

manifest.json(C++ 应用程序)

{
"name": "nameofapp",
"description": "Example host for native messaging",
"path": "bin/Debug/nameofapp.exe",
"type": "stdio",
"allowed_extensions": [ "name@example.org" ]
}

主要.cpp

#include <iostream>

using namespace std;

int main()
{
cout << "Hello world!" << endl;
//cin.get();
return 0;
}

我还在注册表编辑器中创建了正确的 key 。

这是我进入浏览器控制台的内容:

Sending:  ping  
Native application tried to send a message of 1819043144 bytes,
which exceeds the limit of 1048576 bytes.

最佳答案

我在这里找到了答案:https://groups.google.com/a/chromium.org/forum/#!msg/chromium-extensions/Y5RckbPVnr8/xe5w9RC6O5sJ

#include <iostream>

int main(int argc, char* argv[])
{
std::cout.setf( std::ios_base::unitbuf ); //instead of "<< eof" and "flushall"
unsigned int a, c, i, t=0;
std::string inp;
bool bCommunicationEnds = false;

do {

inp="";
t=0;
// Sum the first 4 chars from stdin (the length of the message passed).
for (i = 0; i <= 3; i++)
{
// t += getchar();
t += std::cin.get();
}
// Loop getchar to pull in the message until we reach the total
// length provided.
for (i=0; i < t; i++)
{
//c = getchar();
c = std::cin.get();
//if(c == EOF)
if(c == 65)
{
bCommunicationEnds = true;
i = t;
}
else
{
inp += c;
}
}

if(!bCommunicationEnds)
{
//Collect the length of the message
unsigned int len = inp.length();
//unsigned int len = strJson.length();
//// We need to send the 4 btyes of length information
std::cout << char(((len>>0) & 0xFF))
<< char(((len>>8) & 0xFF))
<< char(((len>>16) & 0xFF))
<< char(((len>>24) & 0xFF));
//// Now we can output our message
std::cout << inp;

}
}while(!bCommunicationEnds);
return 0;
}

关于javascript - webextension native 应用程序 c++ hello world,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42412525/

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