How to inject Javascript in WebBrowser control?

前端 未结 15 2308
夕颜
夕颜 2020-11-22 04:56

I\'ve tried this:

string newScript = textBox1.Text;
HtmlElement head = browserCtrl.Document.GetElementsByTagName(\"head\")[0];
HtmlElement scriptEl = browser         


        
15条回答
  •  旧时难觅i
    2020-11-22 05:39

    I used this :D

    HtmlElement script = this.WebNavegador.Document.CreateElement("SCRIPT");
    script.SetAttribute("TEXT", "function GetNameFromBrowser() {" + 
    "return 'My name is David';" + 
    "}");
    
    this.WebNavegador.Document.Body.AppendChild(script);
    

    Then you can execute and get the result with:

    string myNameIs = (string)this.WebNavegador.Document.InvokeScript("GetNameFromBrowser");
    

    I hope to be helpful

提交回复
热议问题