C# – Chrome extension / App communicate using port / sendNativeMessage

C#

private static string OpenStandardStreamIn()
{
//// We need to read first 4 bytes for length information
Stream stdin = Console.OpenStandardInput();
int length = 0;
byte[] bytes = new byte[4];
stdin.Read(bytes, 0, 4);
length = System.BitConverter.ToInt32(bytes, 0);

string input = “”;
for (int i = 0; i < length;i++ )
{
input += (char)stdin.ReadByte();
}

return input;
}

private static void OpenStandardStreamOut(string stringData)
{
//// We need to send the 4 btyes of length information
int DataLength = stringData.Length;
Stream stdout = Console.OpenStandardOutput();
stdout.WriteByte((byte)((DataLength >> 0) & 0xFF));
stdout.WriteByte((byte)((DataLength >> 8) & 0xFF));
stdout.WriteByte((byte)((DataLength >> 16) & 0xFF));
stdout.WriteByte((byte)((DataLength >> 24) & 0xFF));
//Available total length : 4,294,967,295 ( FF FF FF FF )

Console.Write(stringData);
}

“OpenStandardStremIn()” returns a string which contains JSON type data.

and OpenStandardStreamOut( string stringData ) must start with a string which belongs in JSON.

 

Chrome

// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var port = null;

var getKeys = function(obj){
var keys = [];
for(var key in obj){
keys.push(key);
}
return keys;
}
function appendMessage(text) {
document.getElementById(‘response’).innerHTML += “<p>” + text + “</p>”;
}

function updateUiState() {
if (port) {
document.getElementById(‘connect-button’).style.display = ‘none’;
document.getElementById(‘input-text’).style.display = ‘block’;
document.getElementById(‘send-message-button’).style.display = ‘block’;
} else {
document.getElementById(‘connect-button’).style.display = ‘block’;
document.getElementById(‘input-text’).style.display = ‘none’;
document.getElementById(‘send-message-button’).style.display = ‘none’;
}
}

function sendNativeMessage() {
message = {“text”: document.getElementById(‘input-text’).value};
port.postMessage(message);
appendMessage(“Sent message: <b>” + JSON.stringify(message) + “</b>”);
}

function onNativeMessage(message) {
appendMessage(“Received message: <b>” + JSON.stringify(message) + “</b>”);
}

function onDisconnected() {
appendMessage(“Failed to connect: ” + chrome.runtime.lastError.message);
port = null;
updateUiState();
}

function connect() {
var hostName = “com.google.chrome.example.echo”;
appendMessage(“Connecting to native messaging host <b>” + hostName + “</b>”)
port = chrome.runtime.connectNative(hostName);
port.onMessage.addListener(onNativeMessage);
port.onDisconnect.addListener(onDisconnected);
updateUiState();
}

document.addEventListener(‘DOMContentLoaded’, function () {
document.getElementById(‘connect-button’).addEventListener(
‘click’, connect);
document.getElementById(‘send-message-button’).addEventListener(
‘click’, sendNativeMessage);
updateUiState();
});

 

Chrome extension / app side script comes from Chromium project NativeMessaging example