javascript - 使用 greasemonkey 在 iframe 加载上执行函数

标签 javascript jquery iframe greasemonkey

我正在制作供个人使用的 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>')
      });
});

API 引用:http://api.jquery.com/load-event/

关于javascript - 使用 greasemonkey 在 iframe 加载上执行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14178170/

相关文章:

javascript - 存储在变量中的正则表达式在匹配时不起作用

php - 在zend框架中使用jquery发布数据

javascript - 从服务器端调用 Javascript,目标给出的不起作用

html - 如何在 Webbrowser VB.Net 中只保留一段 HTML 代码?

javascript - 从上一页访问数据层

javascript - 仅当选中复选框时才从 ng-repeat 中删除元素并控制台删除的值

javascript - javascript中单引号转双引号,以及双引号转单引号

javascript - 获取跨域 iframe 的响应状态码?

javascript - 如何防止用户影响按钮点击次数

javascript - 返回 dom 节点的 window 对象属性?