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

How to reference a file in the data directory of a Firefox extension?

本文关键字:扩展 数据目录 文件 Firefox 何引用 引用      更新时间:2023-12-09

我正在开发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())?

如果您在基于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

一旦您获得了这个资源uri,您就可以使用self.postMessage或self.port.emit:将其提供给内容脚本

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

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

这可能就是为什么这条线路不适合我的原因:

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

我只需要重写一点:

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