简体   繁体   中英

how can i change webpage source including(html,css,js) geckofx c#

I need to change a web page source in GeckoFX web browser including html, css and js.

This is my code:

   geckoWebBrowser1.Navigate("http://example.com/");
   geckoWebBrowser1.DocumentCompleted += GeckoWebBrowser1_DocumentCompleted;

    private void GeckoWebBrowser1_DocumentCompleted(object sender, Gecko.Events.GeckoDocumentCompletedEventArgs e)
    {

        WebClient w = new WebClient();
        string s = (w.DownloadString("http://example.com/"));
        //after do changes on (s)
        geckoWebBrowser1.LoadHtml(s, "http://example.com/");

But it's not working on javascript, can anyone help me?

The problem is that geckoWebBrowser1.LoadHtml also triggers GeckoWebBrowser1_DocumentCompleted(). So you will loop endlessly.

Move the LoadHtml to another function, or change the content live as below.

Also, are you're using WebClient to download the same page? There is no need, the source is already available.

GeckoHtmlElement element = null;
var geckoDomElement = e.Window.Document.DocumentElement;
if (geckoDomElement is GeckoHtmlElement)
{
    element = (GeckoHtmlElement)geckoDomElement;
    element.InnerHtml = element.InnerHtml.Replace("Google", "Göggel");
}

Javascript is most easily executed using the following:

using (AutoJSContext context = new AutoJSContext(ActiveBrowser.Window.DomWindow))
{
    var result = context.EvaluateScript("testFunction();");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM