How do I add a local script file to the HTML of a WebBrowser control?

后端 未结 4 1233
遥遥无期
遥遥无期 2020-12-06 02:34

This seems really dumb. I\'ve tried a bunch of different ways and it\'s just not working. I have a WinForms app with a WebBrowser control. If I try with a raw html file on m

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-06 02:36

    i came up on your post, while playing around with things following worked for me:

    HtmlElementCollection head = webBrowser1.Document.GetElementsByTagName("head");
    if (head != null)
    {
        HtmlElement elm = webBrowser1.Document.CreateElement("script");
        elm.SetAttribute("type", "text/javascript");
        elm.InnerText = System.IO.File.ReadAllText(Environment.CurrentDirectory + @"\helperscripts.js");
        ((HtmlElement)head[0]).AppendChild(elm);
    }
    

    , so all methods of helperscript.js can be invoked using

    webBrowser1.Document.InvokeScript("methodname");
    

    , here as a reference for the script invoke: How to inject Javascript in WebBrowser control?

    greetings

提交回复
热议问题