【问题标题】:Greasemonkey against an iframe using @include - does this work? [duplicate]Greasemonkey 使用 @include 对抗 iframe - 这行得通吗? [复制]
【发布时间】:2009-11-05 00:11:13
【问题描述】:

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

我不需要与家长进行任何互动。我已经尝试过几次但没有成功。是否有任何跨域限制阻止某人对 iframe 执行?

PS:iframe 的 JS 代码阻止它作为顶部窗口加载。

【问题讨论】:

    标签: iframe greasemonkey


    【解决方案1】:

    好吧,让 Greasemonkey 在 iframe 上运行当然是可能的——事实上,它是一个common question 来确定如何阻止它在 iframe 以及主页上执行。您应该能够采取与该答案相反的方式来防止代码在顶部窗口中执行:

    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 上运行了以下测试用户脚本,它成功隐藏了 Google 徽标(仅当 Google 在 iframe 中时)。

    // @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 运行时)不存在 iframe 会发生什么。

    【讨论】:

    • domain-b never executes 是问题,因为浏览器中的跨域限制。做跨域限制并且无法访问域-b 上的源我无法读取 DIV 以确定是否存在某些东西。我希望@include domain-b 会在 iframe(即 domain-b)上触发,因此我可以通过 GM 脚本访问标记。我将继续探索各种方法。
    • 嗯,它对我有用:当我 @include 只是 iframe 域时,我可以获得一个 Greasemonkey 脚本以在第三方域 iframe 上执行。我已经用示例代码和一个测试页面更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 2010-10-20
    • 1970-01-01
    • 2014-01-08
    相关资源
    最近更新 更多