cefsharp执行javascript
我想在Windows窗体中使用CefSharp
来执行JavaScript
代码,但它不起作用。 代码如下,并且未显示消息test
。 我错过了什么?
var browser = new ChromiumWebBrowser("https://localhost:50056/simple.aspx"); browser.Name = "Simple Page"; browser.Dock = DockStyle.Fill; this.Controls.Add(browser); browser.ExecuteScriptAsync("alert('test');");
在执行JavaScript之前,您必须等待浏览器充分加载。 开始尝试访问OnFrameLoadStart中的DOM很有吸引力,而V8Context已经创建,你将能够执行DOM没有完成加载的脚本。 如果你需要尽可能早地访问DOM,那么订阅DOMContentLoaded,下面是一些执行JavaScript的例子
上述就是C#学习教程:cefsharp执行javascript分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
browser.RenderProcessMessageHandler = new RenderProcessMessageHandler(); public class RenderProcessMessageHandler : IRenderProcessMessageHandler { // Wait for the underlying JavaScript Context to be created. This is only called for the main frame. // If the page has no JavaScript, no context will be created. void IRenderProcessMessageHandler.OnContextCreated(IWebBrowser browserControl, IBrowser browser, IFrame frame) { const string script = "document.addEventListener('DOMContentLoaded', function(){ alert('DomLoaded'); });"; frame.ExecuteJavaScriptAsync(script); } } //Wait for the page to finish loading (all resources will have been loaded, rendering is likely still happening) browser.LoadingStateChanged += (sender, args) => { //Wait for the Page to finish loading if (args.IsLoading == false) { browser.ExecuteJavaScriptAsync("alert('All Resources Have Loaded');"); } } //Wait for the MainFrame to finish loading browser.FrameLoadEnd += (sender, args) => { //Wait for the MainFrame to finish loading if(args.Frame.IsMain) { args.Frame.ExecuteJavaScriptAsync("alert('MainFrame finished loading');"); } };
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1000702.html