1

I try to execute a javascript command like alert('test message') via GeckoFX and C# but I can not.

I try without results with Navigate and with ExecuteCommand

My code is

    int i=0;    
    GeckoWebBrowser webBrowser
    webBrowser.Navigate("alert('"+i.ToString()+"');");
    webBrowser.ExecuteCommand("alert('" + i.ToString() + "');");

Can anyone help me?

A. Zalonis
  • 1,599
  • 6
  • 26
  • 41

3 Answers3

7

You can use AutoJSContext to run javascript with geckofx.

Something like:

GeckoWebBrowser browser = ....;

using (AutoJSContext context = new AutoJSContext(browser.JSContext))
{                               
   string result;
   context.EvaluateScript("3 + 2;", out result)
}

See EvaluateScript unittests for more info and examples.

If you are using a super old version of geckofx you may need to get a later version - geckofx

Tom
  • 6,325
  • 4
  • 31
  • 55
  • I am using version 16.0.0.2 – A. Zalonis Jul 23 '13 at 14:21
  • 1
    @Tom I know this is old, but Im trying to use this code and I have an error. 'Gecko.GeckoWebBrowser does not contain a definition for 'JSContext' and no extension method 'JSContext' accepting a first argument of type Gecko.GeckoWebBrowser could be found'..What Am I missing?.. – skram Oct 18 '13 at 14:32
  • 5
    Starting in version 22, you'll need browser.Window.JSContext. – John Hatton Oct 18 '13 at 19:22
  • 4
    In version 45 it's just `browser.Window` – Leonid Vasilev Apr 05 '16 at 11:57
  • i didnt found that way in vb.net any clue? @JohnHatton – gumuruh Mar 03 '17 at 00:23
  • 1
    @gumuruh Don't know what to tell you about vb.net. You can see current c# examples in the [v45 unit tests](https://bitbucket.org/geckofx/geckofx-45.0/src/e8f5556a5c02381184d312492962bd169ec3a401/GeckofxUnitTests/GeckoWebBrowserTests.cs?at=default&fileviewer=file-view-default) – John Hatton Mar 03 '17 at 23:39
3

for the new versions instead of geckoWebBrowser1.JSContext you should write geckoWebBrowser1.Window

my code is working and I've answered with sample in another old post here

Arash.Zandi
  • 1,010
  • 2
  • 13
  • 24
0

You can use Navigate method to avoid AccessViolatoinException during calling document js function:

webView.Navigate("javascript:$$external.consoleLog('message text');");
Przemysław Michalski
  • 9,627
  • 7
  • 31
  • 37