How to inject Javascript in WebBrowser control?

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

    this is a solution using mshtml

    IHTMLDocument2 doc = new HTMLDocumentClass();
    doc.write(new object[] { File.ReadAllText(filePath) });
    doc.close();
    
    IHTMLElement head = (IHTMLElement)((IHTMLElementCollection)doc.all.tags("head")).item(null, 0);
    IHTMLScriptElement scriptObject = (IHTMLScriptElement)doc.createElement("script");
    scriptObject.type = @"text/javascript";
    scriptObject.text = @"function btn1_OnClick(str){
        alert('you clicked' + str);
    }";
    ((HTMLHeadElementClass)head).appendChild((IHTMLDOMNode)scriptObject);
    
    0 讨论(0)
  • 2020-11-22 05:37

    What you want to do is use Page.RegisterStartupScript(key, script) :

    See here for more details: http://msdn.microsoft.com/en-us/library/aa478975.aspx

    What you basically do is build your javascript string, pass it to that method and give it a unique id( in case you try to register it twice on a page.)

    EDIT: This is what you call trigger happy. Feel free to down it. :)

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-22 05:45

    For some reason Richard's solution didn't work on my end (insertAdjacentText failed with an exception). This however seems to work:

    HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
    HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
    IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
    element.text = "function sayHello() { alert('hello') }";
    head.AppendChild(scriptEl);
    webBrowser1.Document.InvokeScript("sayHello");
    

    This answer explains how to get the IHTMLScriptElement interface into your project.

    0 讨论(0)
  • 2020-11-22 05:51

    Also, in .NET 4 this is even easier if you use the dynamic keyword:

    dynamic document = this.browser.Document;
    dynamic head = document.GetElementsByTagName("head")[0];
    dynamic scriptEl = document.CreateElement("script");
    scriptEl.text = ...;
    head.AppendChild(scriptEl);
    
    0 讨论(0)
  • 2020-11-22 05:52

    The managed wrapper for the HTML document doesn't completely implement the functionality you need, so you need to dip into the MSHTML API to accomplish what you want:

    1) Add a reference to MSHTML, which will probalby be called "Microsoft HTML Object Library" under COM references.

    2) Add 'using mshtml;' to your namespaces.

    3) Get a reference to your script element's IHTMLElement:

    IHTMLElement iScriptEl = (IHTMLElement)scriptEl.DomElement;
    

    4) Call the insertAdjacentText method, with the first parameter value of "afterBegin". All the possible values are listed here:

    iScriptEl.insertAdjacentText("afterBegin", "function sayHello() { alert('hello') }");
    

    5) Now you'll be able to see the code in the scriptEl.InnerText property.

    Hth, Richard

    0 讨论(0)
提交回复
热议问题