在 Mozilla Add-On SDK 中使用第三方 JS 库

发布于 2024-12-04 04:14:19 字数 417 浏览 1 评论 0 原文

我正在开始一个新项目(Firefox 插件),我想尝试使用行为驱动开发。我特别喜欢 Jasmine BDD 库。但是,我找不到如何在 Add-On SDK 中使用 Jasmine 等框架的好方法。

一个问题是 Jasmine 需要在全局对象上指定 setTimeout (和类似的)函数,而 Add-On SDK 使用“timers”模块导出这些函数。但假设我调整 Jasmine 以从“计时器”获取这些对象(或将计时器导出的方法添加到全局对象)。

更大的问题是我不知道如何实际运行测试。 SDK 生成了一个测试目录,但是,那里没有窗口或文档对象来允许我查看输出(而且我真的很想看到精美的 HTML 输出)。我想我可以创建一个内容脚本来修改页面,但随后我无法访问(测试)后台脚本。

您以前遇到过这种情况吗?有什么推荐的方法来处理这个问题吗?

谢谢! 托马斯

I'm starting a new project (Firefox add-on) and I'd like to try using behavior-driven development. I particularly like the Jasmine BDD library. However, I can't find a good way how to use a framework such as Jasmine in the Add-On SDK.

One problem is that Jasmine needs setTimeout (and similar) functions to be specified on the global object, whereas Add-On SDK exports those using "timers" module. But let's say I tweak Jasmine to get those object from "timers" (or add the the methods exported by timers to the global object).

The bigger problem is that I don't know how to actually run the tests. There is a test directory generated by the SDK, however, there's no window or document object there to allow me to see the output (and I'd really like to see the fancy HTML output). I guess I could create a content script that would modify the page, but then I can't access (test) the background script.

Have you ever faced this before? Is there any recommended way how to deal with that?

Thanks!
Tomas

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

陌路黄昏 2024-12-11 04:14:19

您可以使用 添加-on SDK windows API 打开一个新窗口来运行测试。您应该能够使用 下标加载器 并将窗口和文档设置为该下标范围内您想要的任何内容:

var windows = require("windows").browserWindows;

windows.open({
  url: "about:blank",
  onOpen: function(window) {
    var script;
      var scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"].
        getService(Ci.mozIJSSubScriptLoader);
      scriptLoader.loadSubScript(subscriptSpec, script);
      script["window"] = window;
      script["document"] = window.document;
      // ... run your tests here by calling script.someFunc() ...
   }
});

更新:进一步的研究表明 browserWindows 实际上是特殊的包装器,不允许您访问内容窗口。您可以尝试从 隐藏框架。这是我能看到的从特权代码访问 HTML 文档的唯一方法。

You can use the Add-on SDK windows API to open a new window to run your tests in. You should be able to load the Jasmine script(s) with the subscript loader and set window and document to whatever you want in the scope of that subscript:

var windows = require("windows").browserWindows;

windows.open({
  url: "about:blank",
  onOpen: function(window) {
    var script;
      var scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"].
        getService(Ci.mozIJSSubScriptLoader);
      scriptLoader.loadSubScript(subscriptSpec, script);
      script["window"] = window;
      script["document"] = window.document;
      // ... run your tests here by calling script.someFunc() ...
   }
});

Update: Further research shows that the browserWindows are actually special wrappers that don't give you access to the content window. You might try getting a window/document from a hidden frame. That's the only way I can see to get access to an HTML document from privileged code.

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