【问题标题】:My Firefox extension is not working. What's the structure of a Firefox extension (XPI file) that uses C++ XPCOM components?我的 Firefox 扩展程序不工作。使用 C++ XPCOM 组件的 Firefox 扩展(XPI 文件)的结构是什么?
【发布时间】:2012-08-08 18:43:04
【问题描述】:

我已经尝试了一百万种不同的方法,但无法让它发挥作用。

在我拥有的 xpi 文件中:

* content folder
     -browserOverlay.js
     -browserOverlay.xul

* locale folder
     *en-US folder
         -browserOverlay.dtd
         -browserOverlay.properties
* skin folder
         -browserOverlay.css

我有 Firefox 14.0.1 并且我的“Hello World”扩展部分不使用 XPCOM 正在工作。目前,它所做的只是添加一个带有按钮的工具栏,单击该按钮时会显示“Hello World”,然后尝试运行我使用 Gecko SDK 构建并使用 VS 2005 编译的 C++ XPCOM 组件。

browserOverlay.js 文件包含以下代码:

/**
 * XULSchoolChrome namespace.
 */
if ("undefined" == typeof(XULSchoolChrome)) {
  var XULSchoolChrome = {};
};

/**
 * Controls the browser overlay for the Hello World extension.
 */
XULSchoolChrome.BrowserOverlay = {
  /**
   * Says 'Hello' to the user.
   */
  sayHello : function(aEvent) {
    let stringBundle = document.getElementById("xulschoolhello-string-bundle");
    let message = stringBundle.getString("xulschoolhello.greeting.label");

    window.alert(message);


    try
    {
      // normally Firefox extensions implicitly have XPCOM privileges
      //but since this is a file we have to request it.

      netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
      const cid = "@example.com/XPCOMSample/MyComponent;1";
      obj = Components.classes[cid].createInstance();
      // bind the instance we just created to our interface
      obj = obj.QueryInterface(Components.interfaces.IMyComponent);
    } catch (err) {
      alert(err);
      return;
    }
    var res = obj.Add(3, 4);
    alert('Performing 3+4. Returned ' + res + '.');
  }
};

当我点击按钮时,我会看到第一个警报窗口。当我单击“确定”时,我会看到一个带有以下错误消息的窗口:

TypeError: Components.classes[cid] 未定义

我做错了什么?我应该在 xpi 文件中的什么位置放置 .dll 文件? .idl 文件? .xpt 文件?

【问题讨论】:

    标签: c++ firefox firefox-addon xpcom


    【解决方案1】:

    我猜你正在学习一个过时的教程,大多数教程还没有更新到XPCOM changes in Gecko 2.0。但是对于您的问题:

    我应该在 xpi 文件中的什么位置放置 .dll 文件?

    任何你喜欢的地方。传统的放置在components/ 子目录中。

    .idl 文件?

    它不应该是您的扩展程序的一部分,只能是 .xpt 文件。

    .xpt 文件?

    同样在您喜欢的任何位置,但通常位于与 .dll 文件相同的目录中。

    向您的chrome.manifest 文件添加必要的声明很重要,但通常是这样的:

    interfaces components/mycomponent.xpt
    binary-component components/mycomponent.dll ABI=WINNT_x86-msvc
    

    另请参阅documentation,了解组件代码中需要更改的内容(如果您愿意,Firefox 源代码树中有一个example)。请注意,您必须使用与您要使用该组件的 Firefox 版本相匹配的 Gecko SDK 版本来编译您的组件。因此,对于 Firefox 14,您需要使用 Gecko SDK 14。由于不再有任何二进制兼容性,因此您的组件将只能在 Firefox 14 中运行,而不能在 Firefox 13 或 Firefox 15 中运行。

    【讨论】:

    • 到目前为止,您的回答非常有帮助,但我仍然缺少一件事:鉴于最新的 Gecko SDK 不包含 xpidl 二进制文件,我该如何编译 IDL 文件?你知道我在哪里可以找到当前的教程吗?
    • @JohnSmith:正如xpidl 文档中所述,您应该使用pyxpidl
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多