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);行,然后重试。