3

Could anyone perhaps tell me how should I use the nsIWebBrowserFind interface in GeckoFX to find strings on a webpage?

I tried the following code, but this throws me an ArgumentNullException - parameter cannot be null (pUnk).

I have no idea what this means, I have never used interfaces before.

        GeckoWebBrowser browser = getCurrentBrowser();

        nsIWebBrowserFind finder = browser.GetInterface<nsIWebBrowserFind>();

        finder.SetSearchStringAttribute(searchBox1.Text);
        finder.FindNext();

I have also tried

    nsIWebBrowserFind finder = Gecko.Xpcom.GetInterface<nsIWebBrowserFind>(browser);

With the same results:(

Please help:)

Thanks!

  • Can you be a little more specif about which actual line throws the InvalidArgumentException in the first code example. Also does the second code really throw a InvalidArgumentException, not some other exception? – Tom May 18 '14 at 20:59
  • Hi Tom! The line that throws the exception is `nsIWebBrowserFind find = browser.GetInterface();`.And both codes throw Argument Null Exception, sorry for confusion! – user3648339 May 27 '14 at 05:17

1 Answers1

1

This works in GeckoFX 29.0:

var field = typeof(GeckoWebBrowser).GetField("WebBrowser", BindingFlags.Instance | BindingFlags.NonPublic);
nsIWebBrowser browser = (nsIWebBrowser)field.GetValue(webBrowser1);
var browserFind = Xpcom.QueryInterface<nsIWebBrowserFind>(browser);
browserFind.SetSearchStringAttribute(search);
try
{
browserFind.SetWrapFindAttribute(true);
browserFind.FindNext();
}
catch { }

Some websites with frames throws an exception after the last result however, this seems to be a bug in GeckoFX. We did a workaround by falling back to a javascript find (javascript:window.find) when the exception was thrown.

Sire
  • 4,086
  • 4
  • 39
  • 74
  • 1
    You, Sire have my upvote:) Can you tell me if you know whether it is possible to 'highlight all matches'? Also - what is the javascript.find solution you have applied? – Bartosz Mar 10 '15 at 08:28