I want to execute JS code, in my cef sharp wpf project.
string jsScript = @"let fakeUserAgent = ""Mozilla / 5.0(Windows NT 10.0; Win32; x64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 100.0.4896.60 Safari / 537.36""; window.navigator.defineGetter(""userAgent"", function () { return fakeUserAgent; });";
await browser.EvaluateScriptAsPromiseAsync(jsScript);
I need that Js to be executed before page loaded, so i can change userAgent to hide fingerprint. But I caught exception like:
System.Exception: 'Unable to execute javascript at this time, scripts can only be executed within a V8Context. Use the IWebBrowser.CanExecuteJavascriptInMainFrame property to guard against this exception. See https://github.com/cefsharp/CefSharp/wiki/General-Usage#when-can-i-start-executing-javascript for more details on when you can execute javascript. For frames that do not contain Javascript then no V8Context will be created. Executing a script once the frame has loaded it's possible to create a V8Context. You can use browser.GetMainFrame().ExecuteJavaScriptAsync(script) or browser.GetMainFrame().EvaluateScriptAsync to bypass these checks (advanced users only).'
That's cause browser hasn't loaded page yet. But I need to execute it before page is loaded. Does Anybody tried out something like this? ?