First, implement WebBrowser internal jump, prevent the default open IE
1, the reference package WebBrowserLinkSelf.dll implementation
Public partial class Mainwindow:window {private WebBrowser WebBrowser = new WebBrowser (); Public MainWindow () {InitializeComponent (); this.webBrowser.LoadCompleted + = new Loadcompletedeventhandler (webbrowser_loadcompleted); Make WebBrowser homestay on label, implement Webborwser internal jump, without ie open label lb = new Label {Content = WebBrowser}; Webbrowserhelper webbrowserhelper = new Webbrowserhelper (webBrowser); Helperregistery.sethelperinstance (LB, webbrowserhelper); Webbrowserhelper.newwindow + = Webbrowseronnewwindow; this.lbBrowserHost.Content = lb; This.webBrowser.Navigate (New Uri ("http://www.baidu.com", Urikind.relativeorabsolute)); private void Webbrowseronnewwindow (object sender, CancelEventArgs e) {Dynamic browser = Sender ; Dynamic activeelement = browser. Document.activeelement; var link = activeelement.tostring (); This.webBrowser.Navigate (New Uri (link, urikind.relativeorabsolute)); E.cancel = true; } }
2, reference Com:microsoft Internet controls implementation (reference msdn:http://msdn.microsoft.com/en-us/library/ System.windows.controls.webbrowser.aspx public Partial class Mainwindow:window
{public MainWindow () {InitializeComponent (); This.webBrowser1.Navigate (New Uri ("http://www.baidu.com", Urikind.relativeorabsolute)); this.webBrowser1.LoadCompleted + = new Loadcompletedeventhandler (webbrowser1_loadcompleted); } private IServiceProvider serviceprovider; void Webbrowser1_loadcompleted (object sender, NavigationEventArgs e) {if (This.serviceprovider = = null) {serviceprovider = (IServiceProvider) webbrowser1.document; if (serviceprovider! = null) {GUID serviceguid = new Guid ("0002df05-0000-0000-c000-0000 00000046 "); Guid iid = typeof (Shdocvw.webbrowser). GUID; var webbrowserptr = (shdocvw.webbrowser) serviceprovider. QueryService (ref serviceguid, ref IID); if (webbrowserptr! = null) {WebbrowSerptr.newwindow2 + = Webbrowser1_newwindow2; }}}} private void Webbrowser1_newwindow2 (ref object PPDISP, ref bool Cancel) {Dynamic browser = This.webbrowser1; Dynamic activeelement = browser. Document.activeelement; var link = activeelement.tostring (); This.webBrowser1.Navigate (New Uri (link, urikind.relativeorabsolute)); Cancel = true; } [ComImport, InterfaceType (Cominterfacetype.interfaceisiunknown)] [Guid ("6d5140c1-7436-11ce-8034-00aa006009 FA ")] internal interface IServiceProvider {[Return:marshalas (Unmanagedtype.iunknown)] Object QueryService (ref GUID guidservice, ref GUID riid); } }
Second, the interaction between WebBrowser and JS
1. Interaction with Page labels
Reference microsoft.mshtml//1, add an HTML tag to the DIV with ID lg htmldocument doc = (htmldocument) this.web Browser.document; IHTMLElement Lbelem = doc.createelement ("button"); Lbelem.innertext = "Test"; Lbelem.style.background = "Red"; Ihtmldomnode node = Doc.getelementbyid ("LG") as Ihtmldomnode; Node.appendchild (Lbelem as Ihtmldomnode); 2. Set the label of the ID su value and style//2.1 using setattribute htmldocument doc = (htmldocument) this.webBrowser.Doc ument; IHTMLElement search = Doc.getelementbyid ("su"); Ihtmldomattribute att = search.getattribute ("value") as Ihtmldomattribute; Search.setattribute ("Value", "Baidu a Bit"); Search.Click (); Search.style.display = "None"; 2.2 Using outerHTML search.outerhtml = "<input id=\" su\ "value=\" Baidu a Bit "class=\" BG s_btn\ "type=\" submit\ "Onc Lick=\ "alert (' Baidu a bit '); \"/> "; 2.3 Use ihtmldomattribute ihtmlattributecollection attributes = (search as Ihtmldomnode). Attributes as Ihtmlattribut Ecollection; foreach (Ihtmldomattribute attr in attributes) {if (Attr.nodename = = "Value") { Attr.nodevalue = "Baidu a bit"; }}//3, replace the class-style mnav with the A-label HTMLDocument doc = (htmldocument) this.webBrowser.Document; IHTMLElementCollection collect = Doc.getelementsbytagname ("a"); foreach (IHTMLElement elem in collect) {if (!) ( Elem is ihtmlunknownelement) && elem.classname! = null) {if (elem.className.Equ ALS ("Mnav", StringComparison.OrdinalIgnoreCase)) {elem.outerhtml = "<a href= ' # ' title= ' replace label ' > Replace </a> '; }}}//4, delete node HTMLDocument doc = (htmldocument) This.webBrowser.Document; IHTMLElement search = Doc.getelementbyid ("su"); Ihtmldomnode node = search as Ihtmldomnode; Node.parentNode.removeChild (node); 5, JS event//5.1 added js htmldocument doc = (htmldocument) this.webBrowser.Document; IHTMLElement search = Doc.getelementbyid ("su"); search.outerhtml = "<input id=\" su\ "value=\" Baidu "class=\" BG s_btn\ "type=\" submit\ "onclick=\" onclick (); \ "/>" ; IHTMLScriptElement scripterrorsuppressed = (ihtmlscriptelement) doc.createelement ("script"); Scripterrorsuppressed.type = "Text/javascript"; Scripterrorsuppressed.text = "function OnClick () {alert (' Add JS ');}"; IHTMLElementCollection nodes = Doc.getelementsbytagname ("Head"); foreach (IHTMLElement elem in nodes) {var head = (htmlheadelement) elem; Head.appendchild ((Ihtmldomnode) scripterrorsuppressed); } 5.2 Delete js IHTMLElementCollection scripts = (ihtmlelementcollection) doc.getelementsbyname ("script") ; foreach (IHTMLElement node in scripts) {if (!) ( Node is ihtmlunknownelement) {ihtmlscriptelement script = node as ihtmlscriptelement; Delete all JS file references if (string. IsNullOrEmpty (Script.text)) {Ihtmldomnode remove = script as Ihtmldomnode; Remove.parentNode.removeChild (remove); }}}//6, write new HTML mshtml. IHTMLDocument2 doc2 = this.webBrowser.Document as mshtml. IHTMLDocument2; Doc2.clear (); Doc2.writeln ("2. Data interaction
Public MainWindow () { InitializeComponent (); this.webBrowser.ObjectForScripting = new Scriptevent (); This.webBrowser.NavigateToString (@ "
WebBrowser and JS Interaction Summary