如何从Firefox扩展程序执行页面定义的JavaScript函数? [英] How do I execute a page-defined JavaScript function from a Firefox extension?
问题描述
我正在为演示目的创建一个Firefox扩展。
我从扩展名中调用文档中的特定JavaScript函数。
我在我的HTML文档中写了这个内容(不是扩展名,而是由Firefox加载的页面):
I'm creating a Firefox extension for demo purposes. I to call a specific JavaScript function in the document from the extension. I wrote this in my HTML document (not inside extension, but a page that is loaded by Firefox):
document.funcToBeCalled = function() {
// function body
};
然后,扩展名将在某些事件上运行:
Then, the extension will run this on some event:
var document = Application.activeWindow.activeTab.document;
document.funcToBeCalled();
但是,它引发了一个错误,说 funcToBeCalled
没有定义。
However it raises an error saying that funcToBeCalled
is not defined.
注意:我可以通过调用 document.getElementById(id);
Note: I could get an element on the document by calling document.getElementById(id);
推荐答案
出于安全考虑,您对扩展名的内容页面的访问权限有限。请参阅 XPCNativeWrapper 和从chrome 安全地访问内容DOM ,
It is for security reasons that you have limited access to the content page from extension. See XPCNativeWrapper and Safely accessing content DOM from chrome,
如果您控制页面,最好的方法是设置页面中的事件侦听器,并从您的扩展名(addEventListener在页面中的dispatchEvent扩展名)中调度事件。
If you control the page, the best way to do this is set up an event listener in the page and dispatch an event from your extension (addEventListener in the page, dispatchEvent in the extension).
否则,请参阅 http://groups.google.com/group/mozilla.dev.extensions/msg/bdf1de5fb305d365
这篇关于如何从Firefox扩展程序执行页面定义的JavaScript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!