1

I am trying to use GeckoFx. Sample program:

class Program
{
    private GeckoWebBrowser browser;
    private Thread thread;

    static void Main(string[] args)
    {
        Xpcom.AfterInitalization += () =>
        {
            Console.WriteLine("init ok");
        };
        Xpcom.Initialize(Utils.AppPath + "xul");
        Program prog = new Program();
        prog.Start();

        Thread.Sleep(1000); // wait for browser instance be created


        prog.Test();

    }

    private void Test()
    {
        browser.NavigationError += (sender, args) =>
        {
            Console.WriteLine("err");
        };
        browser.Navigated += (sender, args) =>
        {
            var text = browser.Window.Document.TextContent;
            // text is null
        };
        browser.Navigate("http://google.com");
        Console.ReadKey();
    }

    /// <summary>
    /// application thread method
    /// </summary>
    private void AppThread()
    {
        browser = new GeckoWebBrowser();
        Application.Run();
    }

    /// <summary>
    /// start thread
    /// </summary>
    private void Start()
    {
        thread = new Thread(AppThread);
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
    }
}

As You see, I create STA thead and run application and create browser instance within it. In Test method I trying to get text of document but it is empty! Is problem about I am not created Form? So is it possible to use GeckoFx without UI? Thanks.

Alexey Kulikov
  • 1,097
  • 1
  • 14
  • 38
  • Two ideas. One, the document is *really* loaded? There's a good chance that your Navigated event is being called before the DOM is actually loaded. Instead, try attaching to the Load event, which is supposed to correspond to the [DOMContentLoaded](https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded) event. Second, does geckofx work with no application event loop? Presumably it does work, since you're getting a Navigated event. – John Hatton Oct 16 '14 at 19:35
  • Actually you'd want the DOMContentLoaded event on the c# side too (there is a bug in the comment of this that said it raised the Load event on the c# side). – John Hatton Oct 16 '14 at 19:54
  • DOMContentLoaded and (for example) DocumentCompleted are never evented. Navigated evented only once. If i try to navigate again Navigated never eveneted again – Alexey Kulikov Oct 16 '14 at 20:02
  • OK, I'd look into my second idea then. You need an event loop. See http://stackoverflow.com/a/4721996/723299 – John Hatton Oct 17 '14 at 15:16
  • Im using Application.Run() event loop. Look code above – Alexey Kulikov Oct 17 '14 at 17:15

0 Answers0