如何从Data中的HTML文件引用Lib中的JavaScript文件? [英] How to reference a JavaScript file in Lib from an HTML file in Data?
问题描述
我决定尝试。如果您希望该页面与您的插件进行通信,则需要使用内容脚本。将内容脚本文件放入 data
目录,通过 contentScriptFile
参数将其分配给面板。请参阅内容脚本,内容脚本将能够使用 self.postMessage()
发送消息到扩展,扩展可以执行必要的操作并发送消息当时。
I decided to give Mozilla's Add-on Builder a try. My directory structure looks something like this:
The problem is that the file popup.html
needs to reference stackapi.js
. But I have absolutely no clue how to do that. Looking through the Mozilla docs, there seems to be a way to do the opposite:
var data = require("self").data;
var url_of_popup = data.url("popup.html");
This allows scripts in Lib to access data files in Data. But I need to do the opposite.
In add-ons built with the Add-on SDK (and the Builder is merely a web interface for the SDK) web pages cannot access extension modules directly - they don't have the necessary privileges. If you simply need to include a JavaScript file from the web page then you should put that file into the data
directory. However, it won't have any special privileges then (like being able to call require()
).
You don't tell how you are using popup.html
but I guess that it is a panel. If you want that page to communicate with your add-on you need to use content scripts. Put a content script file into the data
directory, assign it to your panel via contentScriptFile
parameter. See documentation on content scripts, the content script will be able to use self.postMessage()
to send messages to the extension, the extension can perform the necessary operations and send a message back then.
这篇关于如何从Data中的HTML文件引用Lib中的JavaScript文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!