2

I would like to use the following code in an extension:

try
{
Components.utils.import("resource://gre/modules/AddonManager.jsm");
AddonManager.addAddonListener(NFuninstallObserver);
}
catch(e) {}

Unfortunately, JavaScript in Firefox 1.5 considers the third line a syntax error and doesn't process any of the code.

I can replace the line with

eval('Components.utils.import("resource://gre/modules/AddonManager.jsm");');

and everything works fine. Any other suggestions, besides giving up my desire to keep the extension working in Firefox 1.5-4.0.*?

R Pruitt
  • 21
  • 2
  • 2
    Why Firefox 1.5? That was released in November 29, 2005, and security updates ceased in June 2007. At least the most recent update for Firefox 3.5 was issued last month. – PleaseStand Apr 12 '11 at 01:19
  • 3
    Catering to the Firefox hipster crowd? "Yeah, I like Firefox -- but I like the *original* Firefox, but before it got so mainstream..." Seriously though, idealmachine makes a good point. – Tyler Apr 12 '11 at 02:08
  • Yeah I'd check your stats on that extension on AMO. I recently checked mine and only 30 people out of 6000 active daily users still had Firefox 3. – RandomEngy Apr 12 '11 at 17:35
  • Relevant: http://blog.mozilla.com/addons/2011/06/07/making-compatible-with-firefox-5-and-6/ mentions import becoming reserved again in Firefox 5, and yet Components.utils.import still works. Apparently in new versions of Firefox, reserved words can be used as properties (`foo.import = ""` is fine) but not as standalone identifiers (`var import = ""` is an error). – Tyler Jun 08 '11 at 02:06
  • While I think its a bad idea that Firefox decided to have a function "named" import, this is actually consistent with ECMA5. As described in http://es5.github.com/#A.1, these are all IdentifierNames which do not exclude ReservedWords: `a.import` `a["import"]` `a = { import: "test" }`. On the other hand the following is illegal because it's an Identifier, which is an IdentifierName without the Reserved Words. Identifiers are used for FunctionDeclaration and FunctionExpression: `function import() {}`. – studgeek Jun 15 '11 at 14:29
  • Also see https://bugzilla.mozilla.org/show_bug.cgi?id=238324 the issue where they first created this import function. – studgeek Jun 15 '11 at 14:31

1 Answers1

2

I haven't tried it, but this might work...

Components.utils["import"]("resource://gre/modules/AddonManager.jsm");
Tyler
  • 21,762
  • 11
  • 61
  • 90
  • 1
    `import` was a reserved word in earlier versions of JavaScript, but the quoted lookup seems to work. (Strangely MDC says that `import` will become a reserved word again. That's not going to be fun!) – Neil Apr 12 '11 at 09:30