我正在制作供个人使用的 greasemonkey 脚本,但我需要它在 iframe 完成加载时执行功能。我尝试使用 2 个单独的脚本,其中一个在 iframe 中加载并执行该函数,但该函数未定义,因为出于安全目的,greasemonkey 中的函数在它们自己的环境中。
无论如何,是否可以从存在于父级 greasemonkey 中的 iframe 中执行函数?
iframe 父级的 Greasemonkey 代码:
frame2 = '[name="MainFrame"]';
function appendtoiframe2(){
$(frame2).contentWindow.$('body').append('<div id="insecuremode_enable" style="position:fixed;bottom:0;background:#fff;">This div is now visible</div>');
});
iframe 内站点的 Greasemonkey 代码:
window.onload = function() {
parent.appendtoiframe2();
}
谢谢。
编辑:我意识到这样做可能很愚蠢,而不是让脚本直接在站点中运行。但我只希望在网站位于 iframe 中时执行此操作,而它并不总是位于 iframe 中。我还试图能够从 iframe 中控制父级中的元素。
最佳答案
您可以将事件处理程序绑定(bind)到 iframe
加载。 (假定相同的域,否则没有脚本访问 iframe)
来自页面的主要部分:
$(function(){
$('iframe[name="MainFrame"]').load(function(){
$(this).contents().find('body').append('<div>.....</div>')
});
});
关于javascript - 使用 greasemonkey 在 iframe 加载上执行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14178170/