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;
}