Question

I have a question regarding browser remote controlling. My primary goal is to read out all open tab URLs from Google Chrome and also focus a tab if a matching URL is found and reload the current page. All this stuff is invoked from the native application (not from Chrome to the native application.

Currently, I've tried to do with Win32-API stuff (FindWindowEx, ...), new .NET Automation stuff, but this just gives me nothing (the former) or only the URL of the current tab (the latter).

So I thought - based on research - it would be the best idea to write a Google Chrome Extension and interact this way. The sad thing about this is: NPAPI extensions are no longer supported and only the JavaScript-kinda stuff is supported right now.

I then tried to communicate using nativeMessaging but I can't get things to work (never gets executed somehow) and the documentation part about this is -censored-.

This leads me to the question if things are even possible and if yes, how are they possible?


Host application, registered in the registry (Windows):

public static void Main(string[] args)
{
    string message = "{\"text\": \"My response\"}";
    byte[] length = BitConverter.GetBytes(message.Length);
    foreach (byte b in length)
    {
        Console.Write((char)b);
    }
    Console.Write(message);
}

Manifest for the host application

{
  "name": "My.ChromeHost",
  "description": "This extension is used for browser interaction with My App.",
  "version": "1.0",
  "path": "C:\\Program Files (x86)\\My Company AG\\My Product\\My.ChromeHost.exe",
  "type": "stdio",
  "allowed_origins": [
    "chrome-extension://bejoalmncckddamdhnfjafipbgflolmk"
  ]
}

Manifest for extension

{
  "manifest_version": 2,
  "name": "My App",
  "description": "This extension is used for browser interaction with My App.",
  "version": "1.0",
  "background": {
    "scripts": [
        "background.js"
    ],
    "persistent": false
  },
  "permissions": [
    "tabs",
    "nativeMessaging"
  ]
}

background.js

// sending a message to the app (altough I need it vice versa)
var port = chrome.runtime.connectNative("My.ChromeHost");
port.onMessage.addListener(onNativeMessage);
port.onDisconnect.addListener(onDisconnected);
port.postMessage({ "text": "My request" });

function onNativeMessage(message) {
    alert("Received message: <b>" + JSON.stringify(message) + "</b>");
}

function onDisconnected() {
    alert("Failed to connect: " + chrome.runtime.lastError.message);
    port = null;
}
Was it helpful?

Solution

I managed to do establish a connection to my native application, but not using native messaging. I switched to SignalR to communicate. A self-hosted SignalR host is running in my application and the extension acts as client which registers once it is started. Then, IPC is possible from both sides and this is enough for my requirements.

Update: Upon request, a small sample application is located here:

http://tooldesign.ch/pub/SignalRChrome.zip

Please check https://developer.chrome.com/extensions for information on how to develop, pack and distribute the Chrome extension. This is not included in my sample project because of the private keys.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top