简体   繁体   English

在Mozilla附加SDK中使用第三方JS库

[英]Using third-party JS libraries in Mozilla Add-On SDK

I'm starting a new project (Firefox add-on) and I'd like to try using behavior-driven development. 我正在开始一个新项目(Firefox附加组件),我想尝试使用行为驱动的开发。 I particularly like the Jasmine BDD library. 我特别喜欢Jasmine BDD库。 However, I can't find a good way how to use a framework such as Jasmine in the Add-On SDK. 但是,我找不到在Add-On SDK中使用Jasmine之类的框架的好方法。

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

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). SDK会生成一个测试目录,但是,那里没有窗口或文档对象可以让我看到输出(而且我真的很想看到精美的HTML输出)。 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 托马斯

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: 您可以使用Add-on SDK Windows API打开一个新窗口以运行测试。您应该能够使用下标加载器加载 Jasmine脚本,并将窗口和文档设置为您希望的范围。下标:

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. 更新:进一步的研究表明,浏览器Windows实际上是特殊的包装,它们不能使您访问内容窗口。 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. 这是我看到的从特权代码访问HTML文档的唯一方法。

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

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