【问题标题】:native message response extension chrome本机消息响应扩展 chrome
【发布时间】:2017-02-13 10:23:20
【问题描述】:

我用这个代码

// 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# 应用程序中),那么 Extension 可以从中获得响应 有什么问题?

【问题讨论】:

  • 我已经阅读了您的问题二十次,但仍然不确定那里到底发生了什么。仅供参考 当扩展程序打开端口时,Chrome 会使用重定向的输入和输出流执行本机应用程序,因此扩展程序始终是通信的发起者。
  • 实际上,扩展在 postMessage 之后不会从应用程序(C#)接收数据
  • 在这种情况下,您需要展示如何从扩展程序接收数据。您发布的代码没有那部分。
  • 好的,我把OpenStandardStreamIn函数放了
  • 抱歉,我无法理解问题所在。以防我重复一遍:你的原生应用程序不能成为发起者,因为它是由 Chrome 在你的扩展程序调用 runtime.connectNative 时启动的。此外,消息传递是异步的,但您的代码似乎期望同步响应。您可以在 C# 中找到本机消息传递的示例并进行调整。

标签: c# google-chrome google-chrome-extension chrome-native-messaging


【解决方案1】:

我认为您的“Chrome”课程效果不佳。再次检查并确保使用标准流而不是第三方流。同时删除Console.WriteLine("DB file = " + db_way); 行并重试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-31
    • 1970-01-01
    • 2023-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多