2

我使用此代碼本地消息響應擴展鉻

// 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

Actualy,Extension不會在postMessage之後從應用程序(C#)接收數據 – Mafia1990

+0

在這種情況下,您需要顯示如何從您的擴展接收數據。您發佈的代碼沒有該部分。 – wOxxOm

回答

1

我認爲你的'Chrome'類工作不好。再次檢查並確保使用標準流而不是第三方流。同時刪除Console.WriteLine("DB file = " + db_way);行,然後重試。