0

I'm working on a restartless firefox addon. Using firefox developer edition v36. The MDN page on the Dom File API claims that you can import File as:

const {File, Services} = Cu.import('resource://gre/modules/Services.jsm', {});

But Services.jsm clearly does not export a File Object. I also tried:

new contentWindow.File( filename )

But this gives a very descriptive NS_ERROR_FAILURE.

Any clues are welcome, thanks

  • I put that on MDN, I got that from a StackOverflow topic here: http://stackoverflow.com/questions/19780396/use-blob-on-firefox-add-on I just tested it though and you're right its not working anymore. – Noitidart Jan 12 '15 at 00:26

2 Answers2

4

Try this:

Cu.importGlobalProperties(["File"]);

MDN :: Components.utils.importGlobalProperties

Noitidart
  • 35,443
  • 37
  • 154
  • 323
emk
  • 221
  • 3
  • 4
  • Whoa very cool this works for me! Is this just like `Cu.import` in that it is just a pointer? Does this have to wait for any windows to load? – Noitidart Jan 12 '15 at 06:04
  • It does not need to wait for windows to load. The only downside to this method is that it does not work for arbitrary DOM globals; there's a very specific list of properties for which it works at http://mxr.mozilla.org/mozilla-central/source/js/xpconnect/src/Sandbox.cpp#780 (GlobalProperties::Parse). – Josh Matthews Jan 12 '15 at 16:07
  • Thanks alot, MDN can be a labyrinth sometimes –  Jan 13 '15 at 00:07
  • actually, this doesn't work for me. When trying to instantiate a File object I get an exception with properties like: `message: "", result: 2147500037, name: "NS_ERROR_FAILURE"`. The same error I get when using contentWindow.File. –  Jan 15 '15 at 20:28
  • As a matter of fact I now also have the same problem with the hiddenDOMwINDOW approach, even though that worked before... –  Jan 15 '15 at 21:31
0

Noitidart found a workaround:

const { Services } = Cu.import('resource://gre/modules/Services.jsm', {})

// And you're holding on to the constructor straight away
//
var domfile = Services.appShell.hiddenDOMWindow.File( filename )

I have found another workaround at the same time:

// Where window is a contentWindow.
//
var domWindowUtils = window.QueryInterface( Ci.nsIInterfaceRequestor)
                     .getInterface( Ci.nsIDOMWindowUtils)

var FileUtils = Cu.import("resource://gre/modules/FileUtils.jsm").FileUtils

var nsifile = new FileUtils.File( fileName )
var domfile = domWindowUtils.wrapDOMFile( nsifile )

Both approaches are kind of workarounds because they mean you need a loaded window. Currently we found no way to get an interface to it without a window.

  • We hope to see a solution rather than workarounds though. These both depend on waiting for `window`s to load. So anyone that sees this please supply a solution if you can :) – Noitidart Jan 12 '15 at 03:48