How do I use the Dom File API from privileged code?

穿精又带淫゛_ 提交于 2019-12-12 01:54:43

问题


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


回答1:


Try this:

Cu.importGlobalProperties(["File"]);

MDN :: Components.utils.importGlobalProperties




回答2:


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.



来源:https://stackoverflow.com/questions/27893399/how-do-i-use-the-dom-file-api-from-privileged-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!