简体   繁体   中英

File access impossible in Firefox 17.0.1 extension

After updating to Firefox 17.0.1 PrivilegeManager is no longer supported. Various sources say, it is yet possible to simply remove the respective line from the code and everything should work just fine. Unfortunately this is not the case here.

I always get an error: TypeError: Components.classes is undefined . Are there changes concerning Components.classes as well? The Mozilla Code Snippets page (https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O) states the same syntax (without using FileUtils.jsm).

My code:

//netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
var file = Components.classes["@mozilla.org/file/local;1"]
    .createInstance(Components.interfaces.nsILocalFile);

file.initWithPath(filePath);

As several commenters note, you might be running the code in the wrong place (ie: unprivileged, web-page context). It could, however, simply be an issue of scoping.

If it is scoping, try this:

const {Cc,Ci,Cu} = require("chrome");

var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
file.initWithPath(filePath);

If you're running in the wrong place, require will generate an error.

To finally resolve my problem: Initially I was still working with the out-of-date Privilege Manager. When I tried to simply remove this line from my code it did not work for me. The issue was: I was working at home, were the extension was not run as an extension, but - out of laziness - only as a regular xul file. As Boris Zbarsky and paa have already mentioned above, you have to run the code in the extension itself for getting "chrome" privileges.

After doing that, running the abovementioned code (with simply removing the PrivilegeManager line) works just fine!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM