Search code examples
javascriptfirefoxhtmlunit

"InstallTrigger" is not defined


In my html page i have code something like this, where i have installed an extension only if the browser is Firefox:

if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent))
{
  //relevant code
  InstallTrigger.install(InstallXPI);
}

It works fine in every browser. But when the same page is used through htmlunit framework and using browserversion.FIREFOX_3_6 argument in webclient. It shows an error there:

com.gargoylesoftware.htmlunit.ScriptException: Wrapped 
com.gargoylesoftware.htmlunit.ScriptException: Wrapped 
com.gargoylesoftware.htmlunit.ScriptException: ReferenceError: "InstallTrigger" is not defined.

Any idea about this?


Solution

  • This is a reminder for you: don't use browser detection, use feature detection. The issues with your code:

    • InstallTrigger is a feature of the Gecko engine, not Firefox. You are explicitly looking for "Firefox" in the user agent string however and might exclude other browsers based on the Gecko engine (there are e.g. SeaMonkey, K-Meleon, Camino).
    • User agent strings can be spoofed which is apparently what htmlunit is doing - it claims to be Firefox despite using a different browser engine. Your code will run into trouble then.

    Here is how you would do it properly:

    if ("InstallTrigger" in window)
    {
      // Gecko platform, InstallTrigger available
      InstallTrigger.install(InstallXPI);
    }