4

ネイティブメッセージングを通じてChromeエクステンションと通信できるPHPクラスを作成しようとしています。PHPとのネイティブメッセージングのクロージャ

は、私は私のコードに接続することができますが、開始時にChromeは私のPHPのコンソールアプリケーションに

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

ホスト)を送信します。接続を機能させるために私は何を返答しますか? 私の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プラグインを使います。私はきれいで、平らで、シンプルなエクステンションが欲しい。

+0

、それらはコマンドラインパラメータです。あなたのPHPコードでそれを見れば何か間違っています。 Pythonの例として、意味のある部分はsend_messageとread_thread_funcです。 [protocol](https://developer.chrome.com/extensions/nativeMessaging#native-messaging-host-protocol)はシンプルで、ドキュメントに記述されています:** 4バイト長**、JSON-ifiedメッセージ。 – wOxxOm

+0

確かに妥当な音。すぐに確認して返信するつもりです。 –

+0

@wOxxOm chromeが標準入力を正しく処理していないようです。私はWindowsの.batファイルを使用しています(Pythonアプリケーションの例のように)。 '' 'echo%* | php -f "%〜dp0/native_host.php" '' ' –

答えて

7

ネイティブメッセージングは​​、読み書きする構造化データ(長形式)を使用します。ブラウザ(JavaScript)では、その構造がブラウザによって処理されています。ネイティブメッセージングと通信したいので、その構造に従う必要があります。 Read refference here

各メッセージは、JSONを使用してシリアライズされ、UTF-8でエンコードされ、ネイティブのバイト順で32ビットメッセージ長の を先行されます。

だから、としてあなたのメッセージを送信する必要があります:len(message) + [your message]

LEN(メッセージ)のプロトコルに従って梱包する必要があります。

例の機能出力を送信するには:

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