Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Native messaging with PHP

I am trying to build a PHP class that can communicate with a Chrome Extention through Native Messaging.

I can connect to my code, but at initiation Chrome sends

chrome-extension://lkjcciocnocjjgpacggbaikjehbfedbl/ --parent-window=1837060

To my PHP console app (The Host). What do I reply to make the connection working? Below my PHP code. Yes its dirty because its a POC project and I am very new to Chrome Extensions especially with the current updates.

function out($data = ""){
    $fp = fopen("php://stdout", "w");
    if($fp){
        $response = array("text" => "Ok");
        $message = json_encode($response);
        fwrite($fp, $message);
        fflush($fp);
        slog("[OUTPUT] " . json_encode($response));
        fclose($fp);
        exit(0);
    }else{
        slog("Can't open output stream.");
        exit(1);
    }
}

function err($data){
    $fp = fopen("php://stderr", "w");
    if($fp){
        fwrite($fp, $data);
        fflush($fp);
        fclose($fp);
    }
    return;
}

function in(){
    $data = "";
    $fp = fopen("php://stdin", "r");
    if($fp){
        $data = fgets($fp);
        fclose($fp);       
    }else{
        slog("Can't open input stream.");
        exit(1);
    }
    slog("[INPUT]" . $data);
    return $data;
}

function slog($data){
    if($data != ""){
        file_put_contents("./log.txt", date("r").": {$data}\r\n", FILE_APPEND);
    }
}

slog("Entering");
while(true){
    if(($l = in()) !== ""){
        out($l);
    }else{
        exit(0);
    }
}
exit(0);

My background.js code. (the extention)

var port = null;
var hostName = "com.google.chrome.poc-extension";

function appendMessage(text) {
  document.getElementById('response').innerHTML += "<p>" + text + "</p>";
}

function updateUiState() {
  if (port) {
      document.getElementById('connect-button').style.display = 'none';
  }else{
      document.getElementById('connect-button').style.display = 'block';
  }
}

function sendNativeMessage() {
  port = chrome.runtime.connectNative(hostName);
  port.onMessage.addListener(onNativeMessage);

  message = {"text": document.getElementById('input-text').value};
  port.postMessage(message);
  appendMessage("Sent message: <b>" + JSON.stringify(message) + "</b>");
}
function onNativeMessage(message) {
  alert(message);
  appendMessage("Received message: <b>" + JSON.stringify(message) + "</b>");
}
function onDisconnected() {
  appendMessage("Failed to connect: " + chrome.runtime.lastError.message);
  console.log(chrome.runtime.lastError);
  port = null;
  updateUiState();
}
function connect() {  
  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();
});

There is this Python example app but I don't really get what it does exactly. Besides that it also uses the Tkinter plugin which I don't want. I want a clean, plain and simpel extension.

like image 416
Digital Human Avatar asked Nov 13 '17 16:11

Digital Human


1 Answers

Native Messaging use structured data (length-formatted) to read and write. in browser (JavaScript), that structure has been handled by browser. If you want to communicate with Native Messaging, so you need to follow that structure. Read refference here

Each message is serialized using JSON, UTF-8 encoded and is preceded with 32-bit message length in native byte order.

So you need to send your message as: len(message) + [your message]

len(message) must be packed following the protocol.

Example function to send output:

function out($data = ""){
    $fp = fopen("php://stdout", "w");
    if($fp){
        $response = array("text" => "Ok");
        $message = json_encode($response);
        //Send the length of data
        fwrite($fp, pack('L', strlen($message)));
        fwrite($fp, $message);
        fflush($fp);
        slog("[OUTPUT] " . json_encode($response));
        fclose($fp);
        exit(0);
    }else{
        slog("Can't open output stream.");
        exit(1);
    }
}

Read Input:

function in(){
    $data = "";
    $fp = fopen("php://stdin", "r");
    if($fp){
        //Read first 4 bytes as unsigned integer
        $len    = current( unpack('L', fread($fp, 4) ) );
        $data   = fread($fp, $len);
        fclose($fp);
    }else{
        slog("Can't open input stream.");
        exit(1);
    }
    slog("[INPUT]" . $data);
    return $data;
}
like image 84
Mochamad Arifin Avatar answered Nov 15 '22 11:11

Mochamad Arifin