Native app does not work in Chrome extension
Asked Answered
O

2

0

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?

Obscurant answered 24/1, 2014 at 7:14 Comment(4)
You didn't say where native.exe is located. Is it at D:\connectNative\native.exe?Acrodont
Yes, i try both relative and absolute path of native.exe, both do not work.Obscurant
I think the problem is with registry entry: This is my .reg file content: Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Google\Chrome\NativeMessagingHosts\com.windows.screen] @="C:\\Users\\abc\\Desktop\\Chrome Extension\\manifest.json"Sublittoral
also your native app doesn't send the correct header. refer to this question for a better implementation. #21452983Oribel
S
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;
Strange answered 11/5, 2016 at 9:52 Comment(0)
M
-1

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": "???"
}
Mastic answered 26/4, 2014 at 17:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.