C# 如何在System.Windows.Forms.WebBrowser中调用自定义Javascript?
我正在加载包含以下代码的第三方网页C# 如何在System.Windows.Forms.WebBrowser中调用自定义Javascript?,c#,.net,webbrowser-control,C#,.net,Webbrowser Control,我正在加载包含以下代码的第三方网页 <script type="text/javascript"> onDomReady(function() { some_code1; }); </script> 由于此事件将在执行某些代码1之前发生,因此将使其无效 我也做不到 private void web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { web.Docu
<script type="text/javascript">
onDomReady(function() { some_code1; });
</script>
由于此事件将在执行某些代码1之前发生,因此将使其无效
我也做不到
private void web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
web.Document.InvokeScript("doSome_code1");
DoRequiredManipulations();
}
由于某些_code1
被声明为匿名函数
看来唯一的办法是:
private void web_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var script = GetScriptText(web.DocumentText);
//execute script in webbrowser
DoRequiredManipulations();
}
问题是我不知道如何在webbrowser中执行此脚本。我试过
web.Navigate(“javascript:+script”)代码>但它不能正常工作。我找到了如何做到这一点:
HtmlElement scriptEl = web.Document.CreateElement("script");
(scriptEl.DomElement as IHTMLScriptElement).text = "function myscript() { " + script.Text + " }";
web.Document.GetElementsByTagName("head")[0].AppendChild(scriptEl);
web.Document.InvokeScript("myscript");
HtmlElement scriptEl = web.Document.CreateElement("script");
(scriptEl.DomElement as IHTMLScriptElement).text = "function myscript() { " + script.Text + " }";
web.Document.GetElementsByTagName("head")[0].AppendChild(scriptEl);
web.Document.InvokeScript("myscript");