简体   繁体   English

Firefox 17.0.1扩展中无法访问文件

[英]File access impossible in Firefox 17.0.1 extension

After updating to Firefox 17.0.1 PrivilegeManager is no longer supported. 更新到Firefox 17.0.1后,不再支持PrivilegeManager。 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 . 我总是得到一个错误: TypeError:Components.classes未定义 Are there changes concerning Components.classes as well? 是否有关于Components.classes的更改? 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). Mozilla Code Snippets页面(https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O)表示相同的语法(不使用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. 如果您在错误的位置运行,则require生成错误。

To finally resolve my problem: Initially I was still working with the out-of-date Privilege Manager. 最终解决我的问题:最初我还在使用过时的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. 问题是:我在家工作,扩展程序不作为扩展程序运行,但是 - 出于懒惰 - 仅作为常规xul文件。 As Boris Zbarsky and paa have already mentioned above, you have to run the code in the extension itself for getting "chrome" privileges. 正如Boris Zbarsky和paa已经在上面提到的那样,你必须在扩展本身中运行代码才能获得“chrome”权限。

After doing that, running the abovementioned code (with simply removing the PrivilegeManager line) works just fine! 执行此操作后,运行上述代码(只需删除PrivilegeManager行)就可以了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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