Mozilla(Firefox、Thunderbird)扩展:如何获取扩展 ID(从 install.rdf)?
如果您正在为 Mozilla 应用程序之一(例如 Firefox、Thunderbird 等)开发扩展,您可以在 install.rdf 中定义扩展 ID。
如果出于某种原因您需要知道扩展 ID,例如检索本地文件系统中的扩展目录 (1) 或者如果您想将其发送到 Web 服务(使用统计)等。最好从安装中获取它.rdf 支持将其硬编码到您的 javascript 代码中。
但是如何从我的扩展程序中访问扩展程序 ID?
1)示例代码:
var extId = "[email protected]";
var filename = "install.rdf";
var file = extManager.getInstallLocation(extId).getItemFile(extId, filename);
var fullPathToFile = file.path;
If you are developing an extension for one of the mozilla applications (e.g. Firefox, Thunderbird, etc.) you define a extension id in the install.rdf.
If for some reason you need to know the extension id e.g. to retrieve the extension dir in local file system (1) or if you want to send it to a webservice (useage statistic) etc. it would be nice to get it from the install.rdf in favour to have it hardcoded in your javascript code.
But how to access the extension id from within my extension?
1) example code:
var extId = "[email protected]";
var filename = "install.rdf";
var file = extManager.getInstallLocation(extId).getItemFile(extId, filename);
var fullPathToFile = file.path;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我相当确定“硬编码 ID”在扩展的整个生命周期中永远不会改变。这就是 ID 的全部目的:它对于该扩展来说是唯一的,永久的。只需将其存储为常量并在库中使用该常量即可。这没有什么问题。
不好的做法是使用 install.rdf,它存在的唯一目的是……嗯,安装。一旦开发了扩展,install.rdf 文件的状态就无关紧要,并且很可能不一致。
“安装清单是启用附加组件管理器的 XUL 应用程序在安装附加组件时使用它来确定有关附加组件的信息的文件”[1]
打个比方,这就像访问已删除对象的内存一样溢出。该对象仍然存在于内存中,但它在逻辑上不再相关,并且使用其数据是一个非常非常糟糕的主意。
[1] https://developer.mozilla.org/en/install_manifests
I'm fairly sure the 'hard-coded ID' should never change throughout the lifetime of an extension. That's the entire purpose of the ID: it's unique to that extension, permanently. Just store it as a constant and use that constant in your libraries. There's nothing wrong with that.
What IS bad practice is using the install.rdf, which exists for the sole purpose of... well, installing. Once the extension is developed, the install.rdf file's state is irrelevant and could well be inconsistent.
"An Install Manifest is the file an Add-on Manager-enabled XUL application uses to determine information about an add-on as it is being installed" [1]
To give it an analogy, it's like accessing the memory of a deleted object from an overflow. That object still exists in memory but it's not logically longer relevant and using its data is a really, really bad idea.
[1] https://developer.mozilla.org/en/install_manifests
像 lwburk 一样,我不认为它可以通过 Mozilla 的 API 来使用,但我有一个可行的想法,但它看起来像是一个复杂的 hack。基本步骤是:
将以下行添加到您的 chrome.manifest 文件中
然后我们可以使用以下代码获取并解析该文件:
我选择使用 XMLHttpRequest(而不是简单地从文件中读取)检索内容,因为在 Firefox 4 中,扩展不一定会解压缩。但是,如果扩展保持打包状态,XMLHttpRequest 仍然可以工作(尚未对此进行测试,但已阅读相关内容)。
请注意,资源 URL 由所有已安装的扩展共享,因此如果
packagename-base-dir
不唯一,您将遇到问题。您也许可以利用以编程方式添加别名来解决此问题。这个问题促使我今晚加入 StackOverflow,我期待着更多的参与……我会再见到你们的!
Like lwburk, I don't think its available through Mozilla's API's, but I have an idea which works, but it seems like a complex hack. The basic steps are:
Add the following line to your chrome.manifest file
Then we can grab and parse the file with the following code:
I chose to use a XMLHttpRequest(as opposed to simply reading from a file) to retrieve the contents since in Firefox 4, extensions aren't necessarily unzipped. However, XMLHttpRequest will still work if the extension remains packed (haven't tested this, but have read about it).
Please note that resource URL's are shared by all installed extensions, so if
packagename-base-dir
isn't unique, you'll run into problems. You might be able to leverage Programmatically adding aliases to solve this problem.This question prompted me to join StackOverflow tonight, and I'm looking forward participating more... I'll be seeing you guys around!
由于 Firefox 现在只使用 Chrome 的 WebExtension API,因此您可以使用 @serg 的答案,位于 如何从 JavaScript 获取我的扩展程序的 ID?:
As Firefox now just uses Chrome's WebExtension API, you can use @serg's answer at How to get my extension's id from JavaScript?:
我无法证明是否定的,但我做了一些研究,我认为这是不可能的。证据:
nsIExtensionManager
接口期望您检索扩展名
通过 ID 获取信息
nsIExtensionManager
接口描述,显示没有
该接口
确实允许您检索已安装扩展的完整列表,因此可以使用 ID 以外的其他内容检索有关您的扩展的信息。例如,请参阅此代码:
但您仍然依赖于硬编码属性,其中 ID 是唯一保证唯一的。
I can't prove a negative, but I've done some research and I don't think this is possible. Evidence:
the
nsIExtensionManager
interfaceexpects you to retrieve extension
information by ID
nsIExtensionManager
interfacedescription, which shows no
method that helps
The interface does allow you to retrieve a full list of installed extensions, so it's possible to retrieve information about your extension using something other than the ID. See this code, for example:
But you'd still be relying on hardcoded properties, of which the ID is the only one guaranteed to be unique.
看看这个附加组件,也许它的作者可以帮助你,或者你自己可以弄清楚:
(尚不兼容 Firefox 4.0)
https://addons.mozilla.org/firefox/addon/2195
Take a look on this add-on, maybe its author could help you, or yourself can figure out:
(Not compatible yet with Firefox 4.0)
https://addons.mozilla.org/firefox/addon/2195