且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-29 20:35:34

我还没有得到完整的答案,找到扩展扩展,看看源代码,因为它似乎是一个很好的起点,从谷歌搜索一些方法,我发现这个片段在MDC 。代码的关键点似乎是这样的:

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

您必须替换 extension@guid.net 用适当的ID标识你的扩展名。

Firefox 4需要不同的代码,请参阅其他答案。


How do I programatically get my own Firefox extension's version number with Javascript?

My extension has an install.rdf file containing the version number similar to below. I want to extract the contents of the <em:version> tag.

<?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>

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. 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.

Firefox 4 requires different code, see the other answer.