webextension本机应用程序c ++ hello world

ho

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

现在这是我所做的:

manifest.json(webextension)

{
  "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": "[email protected]",
      "strict_min_version": "50.0"
    }
  }

}

background.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": [ "[email protected]" ]
}

main.cpp

#include <iostream>

using namespace std;

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

我也已经在注册表编辑器中创建了正确的密钥。

这就是我进入浏览器控制台的方法:

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

我在这里找到了答案: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;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

本机应用程序和Azure AD-Hello World

来自分类Dev

NativeScript Hello World应用程序大小

来自分类Dev

Strace Hello World程序

来自分类Dev

扩展的Hello World程序

来自分类Dev

C ++的Hello World错误

来自分类Dev

了解程序集Hello World

来自分类Dev

如何编译Scala Hello World应用程序

来自分类Dev

Hello World Android应用程序在启动时崩溃

来自分类Dev

科尔多瓦“ Hello World”应用程序不会显示

来自分类Dev

无法调试AWS SAM Hello World应用程序

来自分类Dev

无法在Android中运行简单的Hello World应用程序

来自分类Dev

Docker - Spring 启动应用程序 - Windows 上的 Hello World

来自分类Dev

“ Hello World” Android应用出现问题

来自分类Dev

图形驱动程序“ hello world”的例子?

来自分类Dev

如何启动我的简单hello world程序?

来自分类Dev

mbed中的Hello World MQTT程序

来自分类Dev

“ Hello World”如何在C中工作

来自分类Dev

Dev-C ++ Hello world不显示

来自分类Dev

Nasm x86-64中的Hello World程序连续打印Hello World

来自分类Dev

如何为ARM交叉编译Hello World C程序

来自分类Dev

我的Win32 C ++“ Hello World程序”无法编译

来自分类Dev

如何为ARM交叉编译Hello World C程序

来自分类Dev

C Hello World程序在变量输出上崩溃

来自分类Dev

Codenvy C++ Hello World 程序无法构建?

来自分类Dev

如何构建在.NET CoreCLR上运行的hello-world控制台应用程序?

来自分类Dev

Scala 2.11在Hello World Spray应用程序中出现错误

来自分类Dev

Android第一个应用程序Hello World Logcat错误

来自分类Dev

在Visual Studio 2012中构建Hello World Gstreamer 1.2.3应用程序

来自分类Dev

Cordova:构建Android Hello World应用程序时的蚂蚁错误代码1

Related 相关文章

热门标签

归档