c# - native 消息响应扩展 chrome

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

我用这个代码

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

//类 Chrome

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

最佳答案

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

关于c# - native 消息响应扩展 chrome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42201713/

相关文章:

Javascript 单选按钮适用于 FF 和 Chrome,但不适用于 IE

c# - 如何根据.NET Framework 版本定义离开代码?

C# WPF datgird IvalueConverter 绑定(bind)问题

python - selenium.common.exceptions.WebDriverException : Message: invalid session id using Selenium with ChromeDriver and Chrome through Python

windows - 在 Jenkins 的 Windows VM 中运行 Selenium 测试时无法打开浏览器

jquery - 无法在 Chrome 扩展的 content_script 中加载 jquery 文件

google-chrome - 在谷歌浏览器中创建书签校验和背后的逻辑

google-chrome-extension - 无需使用内容脚本即可配置键盘快捷键

c# - 如何在c#中的<img>标签中写入图片

c# - 如何创建具有多种尺寸/图像的 System.Drawing.Icon?