How to inject Javascript in WebBrowser control?

前端 未结 15 2272
夕颜
夕颜 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:33

    HtmlDocument doc = browser.Document;
    HtmlElement head = doc.GetElementsByTagName("head")[0];
    HtmlElement s = doc.CreateElement("script");
    s.SetAttribute("text","function sayHello() { alert('hello'); }");
    head.AppendChild(s);
    browser.Document.InvokeScript("sayHello");
    

    (tested in .NET 4 / Windows Forms App)

    Edit: Fixed case issue in function set.

提交回复
热议问题