Native app does not work in Chrome extension

天涯浪子 提交于 2019-12-22 01:05:10

问题


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?


回答1:


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;



回答2:


Example of a working script with Native reference. Note the Permissions for nativeMessaging and no direct reference to the external resource in the Manifest.json the reference is later on in the .js script.

{
   "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": "???"
}


来源:https://stackoverflow.com/questions/21326629/native-app-does-not-work-in-chrome-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!