2

Iはネイティブメッセージ応答拡張クロム

// background.js 
chrome.runtime.sendNativeMessage("com.example.native", 
    { text: "test" }, 
    function(response) { 
    console.log("Received " + response); 
}); 

C#コード

private static void OpenStandardStreamOut(string stringData) 
{ 
    string msgdata = "{\"text\":\"" + stringData + "\"}"; 
    int DataLength = msgdata.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)); 
    Console.Write(msgdata); 
} 

private static List<LoginPack> OpenStandardStreamIn() 
{ 
    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(); 
    } 
    JObject Read=(JObject)JsonConvert.DeserializeObject<JObject>(input); 
    //string dataPackStr = JsonConvert.SerializeObject(Read); 
    Chrome chromeClass = new Chrome(); 
    List<LoginPack> lp = new List<LoginPack>(); 
    if (Read!=null) 
     if (Read.Count != 0) 
      lp = chromeClass.getInfoFromChrome(Read["text"].ToString()); 
    if (lp.Count == 0) 
     return null; 
    return lp; 
} 

//クラスクロム

public class Chrome 
{ 
    public class Data 
    { 
     public string key { get; set; } 
     public string value { get; set; } 
    } 

    public List<LoginPack> getInfoFromChrome(string colName) 
    { 
     try 
     { 
      // string filename = "my_chrome_passwords.html"; 
      // StreamWriter Writer = new StreamWriter(filename, false, Encoding.UTF8); 
      string db_way = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) 
       + "/Google/Chrome/User Data/Profile 1/Login Data1"; 
      Console.WriteLine("DB file = " + db_way); 
      string db_field = "logins"; 
      List<LoginPack> lp = new List<LoginPack>(); 
      byte[] entropy = null; 
      string description; 
      string ConnectionString = "data source=" + db_way + ";New=True;UseUTF16Encoding=True"; 
      DataTable DB = new DataTable(); 
      string sql = string.Format("SELECT * FROM {0} where action_url=\"{1}\" or origin_url=\"{2}\"", db_field, colName, colName); 
      // System.IO.StreamWriter file1 = new System.IO.StreamWriter("c:\\test.txt"); 
      // file1.WriteLine(sql); 
      // file1.Close(); 
      using (SQLiteConnection connect = new SQLiteConnection(ConnectionString)) 
      { 
       SQLiteCommand command = new SQLiteCommand(sql, connect); 
       SQLiteDataAdapter adapter = new SQLiteDataAdapter(command); 
       adapter.Fill(DB); 
       int rows = DB.Rows.Count; 
       for (int i = 0; i < rows; i++) 
       { 
        byte[] byteArray = (byte[])DB.Rows[i][5]; 
        byte[] decrypted = DPAPI.Decrypt(byteArray, entropy, out description); 
        lp.Add(new LoginPack { userNameElement = (string)DB.Rows[i][2], userName = (string)DB.Rows[i][3], passElement = (string)DB.Rows[i][4], pass = new UTF8Encoding(true).GetString(decrypted) }); 
        //System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test2.txt"); 
        //file.WriteLine(lp[i].userName); 
        //file.Close(); 
       } 
      } 
      // Writer.Close(); 
      return lp; 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
      ex = ex.InnerException; 
      return null; 
     } 
    } 
} 

アプリケーション(C#)と、このコードを使用する拡張からデータを与えるが、拡張カント最初にOpenStandardStreamOut関数を使用すると、アプリケーション から応答が得られます(C#アプリ)、それから拡張機能はそれから応答を得ることができます 何が問題ですか?

+0

私は20回のようにあなたの質問を読んだが、それでも正確に何が起こっているのかまだ分かっていない。 FYIネイティブアプリは、拡張機能が常に通信の開始者になるように、拡張機能がポートを開いたときに、リダイレクトされた入出力ストリームでChromeによって実行されます。 – wOxxOm

+0

実際には、エクステンションはpostMessageの後にアプリケーション(C#)からデータを受け取らないでください – Mafia1990

+0

その場合、エクステンションからデータを受け取る方法を示す必要があります。投稿したコードにはその部分がありません。 – wOxxOm

答えて

1

あなたの「Chrome」クラスはひどくうまくいっていると思います。もう一度チェックし、サードパーティのストリームではなく標準ストリームを使用してください。 Console.WriteLine("DB file = " + db_way);行も削除して、もう一度お試しください。

関連する問題