使用@include对iframe进行Greasemonkey - 这有用吗?

时间:2009-11-05 00:11:14

标签: iframe greasemonkey

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

我不需要与父母进行任何互动。我已经尝试了几次没有成功。是否存在阻止某人针对iframe执行的跨域限制?

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

1 个答案:

答案 0 :(得分:11)

嗯,当然可以让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不存在会发生什么。

相关问题