Stuart.Sklinar
Stuart.Sklinar

Reputation: 3761

C# NativeMessaging to Chrome

I am trying to create a plugin/listener to have an external application pump messages into my SPA page/app.

I've created manifests, JS files, and added a reg entry, but to no avail, my listeners aren't getting triggered.

I've got:

// JavaScript source code

Native.js - Plugin

chrome.runtime.onMessageExternal.addListener(
  function (request, sender, sendResponse) {
      console.log(request);
      console.log(sender);
      console.log(sendResponse);
  });

Manifest.json - Plugin { "manifest_version": 2,

    "name": "Native Messaging Example",
    "version": "1.0",

    "permissions": [
        "nativeMessaging"
    ],
    "background": {
        "scripts": [ "Native.js" ]
    },
     "externally_connectable": {
        "matches": [ "*://localhost/*", "*://casetest/*", "*://case/*" ]
    }
}

C#

Program.js

using System;
using System.IO;

namespace ChromeNativeMessaging
{
    class Program
    {
        static void Main(string[] args)
        {
            OpenStandardStreamOut("data");
            Console.ReadLine();
        }

        private static void OpenStandardStreamOut(string stringData)
        {
            String str = "{\"text\": \"" + stringData + "\"}";
            //String str = stringData;
            Stream stdout = Console.OpenStandardOutput();

            stdout.WriteByte((byte)str.Length);
            stdout.WriteByte((byte)'\0');
            stdout.WriteByte((byte)'\0');
            stdout.WriteByte((byte)'\0');
            Console.Write(str);
        }
    }
}

Manifest.json

    {
    "name": "com.example.nativeMessage",
    "description": "Hello World App",
    "path": "C:\\Users\\sas\\Documents\\visual studio 2013\\Projects\\ChromeNativeMessaging\\ChromeNativeMessaging\\bin\\Debug\\ChromeNativeMessaging.exe",
    "type": "stdio",
    "allowed_origins": [
        "chrome-extension://gbdadncpjaecammkmeolpbembeedjohb/"
    ]
}

Upvotes: 1

Views: 1460

Answers (1)

Xan
Xan

Reputation: 77502

Chrome never listens to native connections from outside (onMessageExternal does not apply), nor does running your C# code magically contact Chrome.

Only your JavaScript side can initiate the connection (by running a new copy of the Native Host), after which you can keep the connection open (if you do it with connect()).

So if you want to be notified of events that happen outside the browser, you need to start the native host from your extension side and then process those events in the native host.

All in all, you should (re-)read the documentation in its entirety: https://developer.chrome.com/extensions/nativeMessaging

Upvotes: 1

Related Questions