简体   繁体   English

Firefox扩展如何以编程方式获取自己的版本号?

[英]How can a Firefox extension get its own version number programmatically?

How do I programatically get my own Firefox extension's version number with Javascript? 如何使用Javascript以编程方式获取我自己的Firefox扩展的版本号?

My extension has an install.rdf file containing the version number similar to below. 我的扩展程序有一个install.rdf文件,其中包含类似于以下的版本号。 I want to extract the contents of the <em:version> tag. 我想提取<em:version>标记的内容。

<?xml version="1.0" encoding="UTF-8"?>
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
 xmlns:em="http://www.mozilla.org/2004/em-rdf#">
  <Description about="urn:mozilla:install-manifest">
    ...
    <em:version>1.0</em:version>
    ...
  </Description>
</RDF>

In Firefox 4 (Gecko 2) the API has changed, so if you need to port to Firefox 4, this is the code (from here ): 在Firefox 4(Gecko 2)中,API发生了变化,因此,如果您需要移植到Firefox 4,请使用以下代码(从此处开始 ):

try {
    // Firefox 4 and later; Mozilla 2 and later
    Components.utils.import("resource://gre/modules/AddonManager.jsm");
    AddonManager.getAddonByID("extension-guid@example.org", function(addon) {
        alert("My extension's version is " + addon.version);
  });
}
catch (ex) {
    // Firefox 3.6 and before; Mozilla 1.9.2 and before
    var em = Components.classes["@mozilla.org/extensions/manager;1"]
             .getService(Components.interfaces.nsIExtensionManager);
    var addon = em.getItemForID("extension-guid@example.org");
    alert("My extension's version is " + addon.version);
}

I've not got the full answer, but I found the Extended extension and had a look at the source code as it seemed like a good starting point, and from Googling some of the methods in that I found this snippet on MDC . 我没有完整的答案,但是我找到了Extended扩展并看了一下源代码,因为这似乎是一个不错的起点,从谷歌搜索的一些方法中,我在MDC上找到了此代码段 The key bit of code would seem to be this: 代码的关键点似乎是这样的:

var gExtensionManager = Components.classes["@mozilla.org/extensions/manager;1"]
                        .getService(Components.interfaces.nsIExtensionManager);
var current = gExtensionManager.getItemForID("extension@guid.net").version;

You would have to replace extension@guid.net with the appropriate ID for your extension. 您必须使用extension@guid.net的相应ID替换extension@guid.net。

Firefox 4 requires different code, see the other answer. Firefox 4需要不同的代码,请参见其他答案。

使用附加SDK,其简单性如下:

var version = require("sdk/self").version;

Web Extensions中,使用以下命令:

browser.runtime.getManifest().version

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

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