如何在WebBrowser控件中注入JavaScript?
要将JavaScript注入WebBrowser控件中,请使用以下步骤-
首先,在VisualStudio中创建Windows窗体应用程序。
现在,将WebBrowser控件拖到窗体
设置网址属性。
右键单击项目,选择 添加引用...→COM→ 键入库
选择“MicrosoftHTML对象库”
添加以下代码以注入JavaScript。
private void myWebBrowser(object sender, WebBrowserDocumentCompletedEventArgs e){
//头元素
HtmlElement hElement = weBrowser.Document.GetElementsByTagName("head")[0];
//脚本元素
HtmlElement sElement = weBrowser.Document.CreateElement("script");
IHTMLScriptElement val = (IHTMLScriptElement)sElement.DomElement;
element.text = "function sayHello() {
alert('Weclome')
}";
hElement.AppendChild(sElement);
weBrowser.Document.InvokeScript("How you doing?");
}