User Tools

Site Tools


study:cs-webbrowser:20250319-001:index

C# Visual Studio .NET Tutorial - WebBrowser Inject JavaScript (2025-03-19)

Notes

  • private void Form1_Load(object sender, EventArgs e)
    {
      webBrowser.Navigate("file:///C:/Users/Standardbenutzer/Desktop/Win8_HTML/win.html");
      webBrowser.Navigated += webBrowser_Navigated;
    }
    
    void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
    {
      InjectScript();
    }
    
    private void InjectScript()
    {
      string script =
        @"function setScript()
        {alert('injected');}";
      appendScriptElement(script);
      webBrowser.Document. InvokeScript("setScript");
    }
    
    private void appendScriptElement(string script)
    (
      var headElements webBrowser.Document.GetElementsByTagName("head");
      var headElement headElements[0];
      var scr webBrowser.Document.CreateElement("script");
      scr.SetAttribute("text", script);
      headElement, AppendChild(scr);
    

study/cs-webbrowser/20250319-001/index.txt · Last modified: 2025/03/19 08:32 (external edit)