How to inject Javascript in WebBrowser control?

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

I\'ve tried this:

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


        
15条回答
  •  长情又很酷
    2020-11-22 05:34

    Here is the easiest way that I found after working on this:

    string javascript = "alert('Hello');";
    // or any combination of your JavaScript commands
    // (including function calls, variables... etc)
    
    // WebBrowser webBrowser1 is what you are using for your web browser
    webBrowser1.Document.InvokeScript("eval", new object[] { javascript });
    

    What global JavaScript function eval(str) does is parses and executes whatever is written in str. Check w3schools ref here.

提交回复
热议问题