Mozilla(Firefox、Thunderbird)扩展:如何获取扩展 ID(从 install.rdf)?

发布于 2024-10-25 02:59:00 字数 609 浏览 1 评论 0原文

如果您正在为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

霊感 2024-11-01 02:59:00

我相当确定“硬编码 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

冷月断魂刀 2024-11-01 02:59:00

像 lwburk 一样,我不认为它可以通过 Mozilla 的 API 来使用,但我有一个可行的想法,但它看起来像是一个复杂的 hack。基本步骤是:

  1. 设置自定义资源 url 以指向扩展程序的基本目录
  2. 读取文件并将其解析为 XML
  3. 使用 XPath

将以下行添加到您的 chrome.manifest 文件中

resource    packagename-base-dir chrome/../

然后我们可以使用以下代码获取并解析该文件:

function myId(){
    var req = new XMLHttpRequest();

    // synchronous request
    req.open('GET', "resource://packagename-base-dir/install.rdf", false); 
    req.send(null);

    if( req.status !== 0){
        throw("file not found");
    }

    var data = req.responseText;

    // this is so that we can query xpath with namespaces
    var nsResolver = function(prefix){
        var ns = {
            "rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
            "em" : "http://www.mozilla.org/2004/em-rdf#"
        };
        return ns[prefix] || null;
    };

    var parser = CCIN("@mozilla.org/xmlextras/domparser;1", Ci.nsIDOMParser);
    var doc = parser.parseFromString(data, "text/xml");
    // you might have to change this xpath expression a bit to fit your setup
    var myExtId = doc.evaluate("//em:targetApplication//em:id", doc, nsResolver, 
                            Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE, null);

    return myExtId.singleNodeValue.textContent;
}

我选择使用 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:

  1. Set up a custom resource url to point to your extension's base directory
  2. Read the file and parse it into XML
  3. Pull the id out using XPath

Add the following line to your chrome.manifest file

resource    packagename-base-dir chrome/../

Then we can grab and parse the file with the following code:

function myId(){
    var req = new XMLHttpRequest();

    // synchronous request
    req.open('GET', "resource://packagename-base-dir/install.rdf", false); 
    req.send(null);

    if( req.status !== 0){
        throw("file not found");
    }

    var data = req.responseText;

    // this is so that we can query xpath with namespaces
    var nsResolver = function(prefix){
        var ns = {
            "rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
            "em" : "http://www.mozilla.org/2004/em-rdf#"
        };
        return ns[prefix] || null;
    };

    var parser = CCIN("@mozilla.org/xmlextras/domparser;1", Ci.nsIDOMParser);
    var doc = parser.parseFromString(data, "text/xml");
    // you might have to change this xpath expression a bit to fit your setup
    var myExtId = doc.evaluate("//em:targetApplication//em:id", doc, nsResolver, 
                            Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE, null);

    return myExtId.singleNodeValue.textContent;
}

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!

吃兔兔 2024-11-01 02:59:00

由于 Firefox 现在只使用 Chrome 的 WebExtension API,因此您可以使用 @serg 的答案,位于 如何从 JavaScript 获取我的扩展程序的 ID?

你可以像这样(不需要额外的权限)分两次获得它
不同的方式:

  1. 使用运行时 API:var myid = chrome.runtime.id;

  2. 使用 i18n API:var myid = chrome.i18n.getMessage("@@extension_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?:

You can get it like this (no extra permissions required) in two
different ways:

  1. Using runtime api: var myid = chrome.runtime.id;

  2. Using i18n api: var myid = chrome.i18n.getMessage("@@extension_id");

榕城若虚 2024-11-01 02:59:00

我无法证明是否定的,但我做了一些研究,我认为这是不可能的。证据:

确实允许您检索已安装扩展的完整列表,因此可以使用 ID 以外的其他内容检索有关您的扩展的信息。例如,请参阅此代码

var em = Cc['@mozilla.org/extensions/manager;1']
  .getService(Ci.nsIExtensionManager);
const nsIUpdateItem = Ci.nsIUpdateItem;

var extension_type = nsIUpdateItem.TYPE_EXTENSION;
items = em.getItemList(extension_type, {});
items.forEach(function(item, index, array) {
    alert(item.name + " / " + item.id + " version: " + item.version);
});

但您仍然依赖于硬编码属性,其中 ID 是唯一保证唯一的。

I can't prove a negative, but I've done some research and I don't think this is possible. Evidence:

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:

var em = Cc['@mozilla.org/extensions/manager;1']
  .getService(Ci.nsIExtensionManager);
const nsIUpdateItem = Ci.nsIUpdateItem;

var extension_type = nsIUpdateItem.TYPE_EXTENSION;
items = em.getItemList(extension_type, {});
items.forEach(function(item, index, array) {
    alert(item.name + " / " + item.id + " version: " + item.version);
});

But you'd still be relying on hardcoded properties, of which the ID is the only one guaranteed to be unique.

反差帅 2024-11-01 02:59:00

看看这个附加组件,也许它的作者可以帮助你,或者你自己可以弄清楚:

[扩展管理器]扩展非常
使用简单。安装后只需
通过转至打开扩展管理器
工具和单击扩展。你
现在将在每个扩展旁边看到
该扩展程序的 ID。

(尚不兼容 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:

[Extension Manager] Extended is very
simple to use. After installing, just
open the extension manager by going to
Tools and the clicking Extensions. You
will now see next to each extension
the id of that extension.

(Not compatible yet with Firefox 4.0)

https://addons.mozilla.org/firefox/addon/2195

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文