【问题标题】:Chrome Native messaging with PHP使用 PHP 的 Chrome 原生消息传递
【发布时间】:2018-01-25 20:41:35
【问题描述】:

我正在尝试构建一个可以通过本机消息传递与 Chrome 扩展进行通信的 PHP 类。

我可以连接到我的代码,但 Chrome 会在启动时发送

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

到我的 PHP 控制台应用程序(主机)。我该如何回复才能使连接正常工作? 在我的 PHP 代码下方。是的,它很脏,因为它是一个 POC 项目,而且我对 Chrome 扩展非常陌生,尤其是当前的更新。

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

我的 background.js 代码。 (扩展名)

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

有this Python 示例应用程序,但我并没有真正了解它究竟做了什么。除此之外,它还使用了我不想要的 Tkinter 插件。我想要一个干净、简单、简单的扩展。

【问题讨论】:

  • 第一个引号不会发送到标准输入,那些是命令行参数。如果您在您的 php 代码中看到它,则说明有问题。至于python例子,有意义的部分是send_message和read_thread_func。 protocol 很简单,并在文档中进行了描述:4 字节长度,JSON 化消息。
  • 听起来确实合理。会尽快检查并回复。
  • @wOxxOm chrome 似乎无法正确传输标准输入。我使用 Windows .bat 文件,例如 Python 应用程序示例。 echo %* | php -f "%~dp0/native_host.php"
  • echo %* 将您的命令行打印到标准输入中,这几乎不是您想要的。文档的 bat 文件没有这样做。
  • 我只尝试了php -f "%~dp0/native_host.php",但这也不起作用.......有点卡在这里

标签: javascript php google-chrome-extension chrome-native-messaging


【解决方案1】:

Native Messaging 使用结构化数据(长度格式)进行读写。在浏览器(JavaScript)中,该结构已由浏览器处理。如果您想与 Native Messaging 进行通信,那么您需要遵循该结构。 Read refference here

每条消息都使用 JSON 序列化,UTF-8 编码并在前面 具有 32 位消息长度(按本机字节顺序)。

所以您需要将您的消息发送为:len(message) + [your message]

len(message) 必须按照协议打包。

发送输出的示例函数:

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

读取输入:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多