如何引用Firefox扩展的数据目录中的文件?

时间:2012-07-18 22:46:43

标签: javascript firefox-addon firefox-addon-sdk

我正在开发Firefox扩展程序,我需要从内容脚本中将JavaScript注入页面。在我的Chrome扩展程序中,我完成了以下操作:

this.initializeJplayerSupport = function() {
  var script = document.createElement('script');
  script.setAttribute('type', 'application/javascript');
  script.setAttribute('src', chrome.extension.getURL('js/custom-jplayer.js'));
  document.head.appendChild(script);
}

该文件位于我的数据目录中。如何在firefox扩展内容脚本中引用js文件(我在Chrome中使用chrome.extension.getURL())?

2 个答案:

答案 0 :(得分:10)

如果你在基于SDK的插件中使用main.js,则需要并使用'self'对象中的'data'帮助器:

var data = require('self').data;

console.log(data.url('somefile.js')); // prints the resource uri to the file.

欲了解更多信息:

https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/self#data

获得此资源后,您可以使用self.postMessage或self.port.emit将其提供给内容脚本:

https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Content_Scripts

答案 1 :(得分:2)

看起来从Firefox 38开始,cfx已被jpm取代。

这可能就是为什么这条线不能为我工作的原因:

var data = require('self').data;

我只需要重写一下:

var data = require('sdk/self').data;