代码之家  ›  专栏  ›  技术社区  ›  nopuck4you

Greasemonkey使用@include来对抗iframe-这有效吗?[副本]

  •  8
  • nopuck4you  · 技术社区  · 15 年前

    我想知道你是否可以让greasemonkey只对iframe执行,而不是它的父窗口。父窗口是域A,iframe是域B,脚本中的include是@include http://domain-B.com/path/ *

    我不需要和父母互动。我试过几次都没有成功。是否有任何跨域限制阻止某人对iframe执行?

    ps:iframe有JS代码,阻止它作为顶部窗口加载。

    1 回复  |  直到 15 年前
        1
  •  11
  •   Community CDub    8 年前

    好吧,当然有可能让Greasemonkey去对抗一个iframe——事实上,这是一个 common question 确定如何阻止它在iframes和主页上执行。您应该能够与该答案相反,以防止代码在顶部窗口上执行:

    if (window.top == window.self)  //don't run on the top window
        return;
    //rest of the actual executing code goes here
    

    我已经测试过了,你可以用 @include 匹配域B(的域 iframe )运行一段任意代码来修改它。我在 test page 它成功地隐藏了谷歌的标志(只有当谷歌 伊夫拉姆 )。

    // @include  http://www.google.com*
    // @require  http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
    // ==/UserScript==
    
    if (window.top == window.self)  //don't run on top window
        return;
    
    alert('running on a frame');
    
    $('img').each(function() {
      $(this).hide();
    });
    

    据我所知,这里没有任何跨域限制。我不确定如果 伊夫拉姆 第一次加载页面时不存在(即GreaseMonkey运行时)。