问题描述
我正在开发Firefox扩展,我需要从内容脚本中将JavaScript注入页面。在我的Chrome扩展中,我做了以下操作:$ $ $ $ $ $ $ $ $ $ $ this.initializeJplayerSupport = function(){
var script = document .createElement( '脚本');
script.setAttribute('type','application / javascript');
script.setAttribute('src',chrome.extension.getURL('js / custom-jplayer.js'));
document.head.appendChild(script);
$ b $ p $该文件位于我的数据目录中。如何在firefox扩展内容脚本中引用js文件(我已经使用
chrome.extension.getURL()
用于Chrome)? 如果您在基于SDK的插件中的main.js中,则需要使用'self'对象中的'data'助手:
var data = require('self')。data;
console.log(data.url('somefile.js')); //将资源URI打印到文件中。
更多信息:
一旦你得到这个资源的URI,你可以使用self.postMessage或self将它提供给内容脚本.port.emit:
I'm working on a Firefox extension and I need to inject a JavaScript into a page from a content script. In my Chrome extension I have done the following:
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);
}
The file is in my data directory. How can I reference a js file in a firefox extension content script (where I have used chrome.extension.getURL()
for Chrome)?
If you're in main.js in your SDK-based add-on, you require and use the 'data' helper from the 'self' object:
var data = require('self').data;
console.log(data.url('somefile.js')); // prints the resource uri to the file.
For more info:
https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/self#data
Once you get this resource uri, you can then supply it to a content script using self.postMessage or self.port.emit:
https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Content_Scripts
这篇关于如何引用Firefox扩展的数据目录中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!