这会注入一个按钮,然后为 click 事件设置一个不显眼的事件侦听器:
HtmlElement myElem = webBrowser1.Document.CreateElement("input");
HtmlElement script = webBrowser1.Document.CreateElement("script");
dynamic element = myElem.DomElement;
element.SetAttribute("value", "Hello, World!");
element.SetAttribute("type", "button");
element.SetAttribute("id", "myButton");
dynamic elemScript = script.DomElement;
elemScript.text = "function bindEvent() { var myButton = document.getElementById('myButton'); if(myButton.addEventListener){ myButton.addEventListener('click',clicked,false);}else if(myButton.attachEvent){myButton.attachEvent('onclick',clicked);}} function clicked(){alert('clicked');}";
((HtmlElement)webBrowser1.Document.GetElementsByTagName("body")[0]).AppendChild(myElem);
((HtmlElement)webBrowser1.Document.GetElementsByTagName("body")[0]).AppendChild(script);
webBrowser1.Document.InvokeScript("bindEvent");
祝你好运!