如何确定扩展的目录
How to determine the extension's directory
我需要从JavaScript获取扩展安装目录的路径。
我的目标是从 Firefox 扩展写入扩展目录中的 JSON 文件。为此,我需要确定在Firefox配置文件中安装扩展的目录。
我使用此代码:
function writeToFile()
{
var id = "plugin@uua";// The extension's id from install.rdf(i.e. <em:id>)
var ext = Components.classes["@mozilla.org/extensions/manager;1"]
.getService(Components.interfaces.nsIExtensionManager)
.getInstallLocation(id)
.getItemLocation(id);
var file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(ext.path);
file.append("config.json");
var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
.createInstance(Components.interfaces.nsIFileOutputStream);
foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0);
var data = '[ {"id" : "2"} ]';
foStream.write(data, data.length);
foStream.close();
它会引发以下错误:
TypeError:Components.classes['@mozilla.org/extensions/manager;1'] is undefined.
我基本上需要从JavaScript自动获取扩展的路径。我仔细检查了扩展名的ID,并且还尝试从其他扩展名写入文件,但没有运气。
非常感谢您的回复。无论如何,它并没有让我立即解决我的问题,但它迫使我阅读 Mozilla 文档。我终于掌握了它的工作原理。再次感谢。
上述问题的解决方案:
Components.utils.import("resource://gre/modules/AddonManager.jsm");
Components.utils.import("resource://gre/modules/FileUtils.jsm");
AddonManager.getAddonByID("plugin_id", function(addon) {
var uri = addon.getResourceURI("config.json");
var file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
var stringUri = uri.asciiSpec;
stringUri = stringUri.replace(new RegExp(/'//g), '''');
stringUri = stringUri.slice(8);
alert(stringUri);
file.initWithPath(stringUri);
alert(addon.hasResource("config.json"));
var stream = FileUtils.openFileOutputStream(file,
FileUtils.MODE_WRONLY
| FileUtils.MODE_CREATE
| FileUtils.MODE_TRUNCATE);
stream.write(dataToWrite, dataToWrite.length);
stream.close();
从 Firefox 4 开始,您应该使用 Add-on Manager API:
Components.utils.import("resource://gre/modules/AddonManager.jsm");
AddonManager.getAddonByID(id, function(addon)
{
var uri = addon.getResourceURI("config.json");
if (uri instanceof Components.interfaces.nsIFileURL)
writeToFile(uri.file);
});
请注意,新 API 是异步的,获取有关扩展的数据可能需要一些时间。此外,您需要在install.rdf
中指定<em:unpack>true</em:unpack>
标志,否则您的扩展将不会在安装时解压缩(出于性能原因),并且磁盘上将没有与config.json
对应的文件(它将是打包的XPI文件中的一个位置)。写入扩展目录还有另一个问题:更新扩展时,所有文件都将被替换。更好的主意可能是写入配置文件目录中的文件,这样就不需要牺牲性能。
写入文件可以使用FileUtils.jsm简化
:Components.utils.import("resource://gre/modules/FileUtils.jsm");
var stream = FileOutputStream.openFileOutputStream(file, FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE);
stream.write(data, data.length);
stream.close();
Firefox 4 取代了 nsIExtensionManager 的使用,转而使用 Add-on Manager。
因此,您应该将附加组件管理器用于FF>= 4
您可以使用以下内容找到用户的个人资料目录:
Components.utils.import("resource://gre/modules/Services.jsm");
Services.dirsvc.get("ProfD", Ci.nsILocalFile)
将您的文件保存在这里可能是一个更好的主意,但我不知道上下文。
相关文章:
- Node.js v6.2.0类扩展不是函数错误
- 扩展移相器按钮类不工作
- chrome扩展中的navigator.geolocation.getCurrentPosition
- chrome扩展:尽管运行了at:documentidle,js脚本还是过早启动
- 在这个使用hasOwnProperty的对象扩展程序中有一个错误,I'我不确定那个bug是什么,也不确定这个扩展
- 即使我单击“确定”,模型弹出窗口扩展器仍保持打开状态
- 如何确定实际背景在 JavaScript 中文本的颜色和颜色(Firefox 扩展代码)
- 确定动态映像 src 的文件扩展名
- 为网站添加增强功能(无论是通过 C#、Chrome 扩展程序等)——不确定什么会起作用
- Chrome 扩展程序 - 确定上下文菜单中的选择文本或页面
- (无域)>扩展名::name从何而来
- 希望用户能够确定Chrome扩展程序的css / js应用于哪些URL
- 如何确定扩展的目录
- 如何确定Chrome窗口是否从Chrome扩展最小化
- Chrome扩展程序如何;s的内容脚本确定注入的页面'的推荐人
- 确定JavaScript类是否扩展了另一个类
- 在Chrome扩展中,确定用户是否点击了浏览器's后退或前进按钮
- 如何确定发起SignalR扩展方法的群体?
- 如何在 javascript 中确定文件是否是图像,无论扩展名如何
- 如何确定在网页中安装JavaScript脚本时使用的firefox扩展版本