Communication Between Native-App and Chrome-Extension

communication between native-app and chrome-extension

Marc's answer contains some errors (inherited from the question) and will not work for messages with lengths that do not fit in one byte.

Chrome's protocol, when communicating with native apps is:

  • requests to native app are received through stdin
  • responses to Chrome are sent through stdout
  • Chrome doesn't deal well with Windows style \r\n so avoid that in the messages and set stdin mode to binary (so you can correctly read the request len and \n doesn't 'turn' into \r\n):

    _setmode(_fileno(stdin),_O_BINARY);

The request and response messages are JSON with a 4 byte header (uint32) containing the length of the message:
[length 4 byte header][message]

Reading the request header:

uint32_t reqLen = 0;
cin.read(reinterpret_cast<char*>(&reqLen) ,4);

Writing the response header:

cout.write(reinterpret_cast<char*>(&responseLen),4); 

Communication between Chrome javascript and native Windows code

There's a way to communicate with local processes without using an extension. Websockets aren't restricted to communicating with the same domain as the web page, they can communicate with a WebSocket server on localhost. You have to wrap your native code in a WebSocket server, libraries are available for that though.

Another method is Native Messaging, but it requires a browser extension:

Native messaging enables a WebExtension to exchange messages with a
native application installed on the user's computer.

https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Native_messaging
https://developer.chrome.com/extensions/nativeMessaging

Chrome extension communication with C native app

It is working. I was trying to print the RAW json..

Use console.log(JSON.stringify(result)) to get the JSON in a string format.

Communication between Native App and Chrome Custom Tab (CCT)

There is a nice tutorial here which show how to create it step by step.

  1. Create a new request token
  2. Get the user to authorize the request token
  3. Create a new session id with the authorized request token

The main idea behind the whole thing is Intent-Filters with category browseable which will get called whenever the redirect URL is called.

<intent-filter android:label="@string/filter_view_http_gizmos">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data
android:host="callback_param"
android:scheme="anything"/>


Related Topics



Leave a reply



Submit