CEF Sharp执行JS

发布于 2025-02-10 07:55:47 字数 724 浏览 2 评论 0 原文

我想在我的CEF Sharp WPF项目中执行JS代码。

字符串jsscript = @“让fakeuseragent =“” mozilla / 5.0(Windows NT 10.0; Win32; x64)AppleWebkit / 537.36(Khtml,像Gecko一样,像Gecko)Chrome / 100.0.0.4896.60 Safari / 537.36“”; window.navigator。 definegetter (““ useragent”“,function(){return fack fakeuseragent;});“;

等待browser.evaluatescriptapriptaplomiseasync(jsscript);

我需要在页面加载之前要执行JS因此,我可以将用户固定为隐藏

指纹有关您何时可以执行不包含JavaScript的JavaScript的更多详细信息脚本一旦加载了框架,就可以创建一个V8Context。

这是因为浏览器尚未加载页面,但是在加载页面之前,我需要执行它吗?

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? ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文