Retrieve data from browser local storage using c#

后端 未结 2 971
温柔的废话
温柔的废话 2021-02-04 10:56

Is it possible to retrieve data from chrome/firefox local storage using C#?

2条回答
  •  孤城傲影
    2021-02-04 11:45

    When using Chromium Embedded Framework I found that the above solution has many limitations. It seems like Chromium have moved to using leveldb instead.

    I ended up with a solution where I inject JS code that modify local storage in FrameLoadStart. (it should be easy to read values as well - JavascriptResponse.Result can be casted to a IDictionary when using this script: "window.localStorage;" instead)

    // writing local storage in FrameLoadStart
    foreach (var entry in LocalStorage)
    {
        script += $"window.localStorage.{entry.Key} = '{entry.Value}';";
    }
    
    IFrame frame = chromeBrowser.GetMainFrame();
    var result = await frame.EvaluateScriptAsync(script , frame.Url, 0);
    if(!result.Success)
    {
        throw new Exception(result.Message);
    }
    

提交回复
热议问题