简体   繁体   English

在firefox xul扩展中使用requirejs

[英]using requirejs within a firefox xul extension

I would like to use requirejs to manage my code within a firefox xul plugin, and I can't get it to find my modules. 我想使用requirejs在firefox xul插件中管理我的代码,但无法找到我的模块。

I know that xul doesn't play nice with the data-main attribute, so I have my main.js script as a second script: 我知道xul与data-main属性的配合不好,所以我将main.js脚本作为第二个脚本:

<script src="chrome://myPackage/content/require.js" type="application/x-javascript"></script>
<script src="chrome://myPackage/content/main.js" type="application/x-javascript"></script>

This successfully calls the script, and the require function is available within main.js, but when I run 这成功调用了脚本,并且main.js中可以使用require函数,但是当我运行时

require(['lib1'], function(lib1){
    alert(lib1.val1);
})

the alert never gets popped (lib1 is in the same directory as main.js). 警报永远不会弹出(lib1与main.js位于同一目录中)。

I have tried this within and without setting the baseUrl as 我已经尝试过并且没有将baseUrl设置为

require.config({
    baseUrl: "chrome://myPackage/content/"
})

and it does not work either way. 而且这两种方式都不起作用。

Does anyone know how I can get require.js to look in the right place for my modules? 有谁知道我如何获得require.js在合适的位置查找我的模块?

Addendum ** 附录**

I added an error handling function and the error code returned is 我添加了一个错误处理功能,返回的错误代码是

http://requirejs.org/docs/errors.html#timeout

I have loaded the test module into a normal web page successfully. 我已将测试模块成功加载到普通网页中。 This seems to confirm that the issue is path configuration (it also takes the 15 second timeout before failing) 这似乎可以确认问题是路径配置(失败之前还需要15秒的超时)

Firebug seems to have a working requirejs version. Firebug似乎有一个有效的requirejs版本。 But more importantly, they have a far better mini-require.js that will not pollute the shared global scope when used in overlays (if used correctly :p) 但更重要的是,它们具有更好的mini-require.js ,当在叠加层中使用时,它们不会污染共享的全局范围(如果正确使用:p)

I suggest you have a look at these implementations and also the code using it. 我建议您看看这些实现以及使用它的代码。

Proactive warning : Please note, that if your add-on uses code that defines lots of new properties on the scope in overlays ( window ) either by defining global functions or variables or implicitly declaring variables within functions, then this may interfere with other code running in the same scope (the browser code itself and other add-ons). 主动警告 :请注意,如果您的加载项使用的代码通过定义全局函数或变量或隐式声明函数中的变量,从而在覆盖图( window )中的范围上定义了许多新属性,则这可能会干扰其他代码的运行范围相同(浏览器代码本身和其他加载项)。 Besides, should you want to submit your add-on to addons.mozilla.org, then a reviewer might not give it public status if your add-on "pollutes" the global scope/namespace in the main overlay. 此外,如果您想将附件提交到addons.mozilla.org,那么如果您的附件“污染”了主叠加层中的全局范围/命名空间,则审阅者可能不会将其公开。

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

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