使用@include对iframe进行Greasemonkey - 这有用吗? [英] Greasemonkey against an iframe using @include - does this work?

查看:98
本文介绍了使用@include对iframe进行Greasemonkey - 这有用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm wondering if you can have Greasemonkey execute against an iframe only and not it's parent window. The parent window is domain A, the iframe is domain B, and the include in the script would be @include http://domain-B.com/path/*.

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

I don't need any interaction with the parent. I've tried this a couple of times without success. Is there any cross-domain restriction preventing someone to execute against the iframe?

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

PS: The iframe has JS code that prevents it from loading as the top window.

推荐答案

嗯,当然可以让Greasemonkey针对iframe运行 - 事实上,它是常见问题,以确定如何阻止它在iframe和主页面上执行。您应该能够采取相应的答案,以防止代码在顶部窗口执行:

Well, it's certainly possible to get Greasemonkey to run against an iframe -- in fact, it's a common question to determine how to stop it from executing on iframes as well as the main page. You should be able to take the converse of that answer to prevent code from executing on the top window:

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

我已经测试了它和你可以使用 @include 来匹配域B( iframe 的域)并运行一段修改的任意代码它。我在测试页上运行了以下测试用户脚本并成功运行隐藏Google徽标(仅当Google位于 iframe 时)。

I've tested it and you can use @include to match domain B (the domain of the iframe) and run a piece of arbitrary code that modifies it. I ran the following test userscript on a test page and it successfully hides the Google logo (only when Google is in an 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();
});

据我所知,此处不涉及任何跨域限制。我不确定当首次加载页面时( iframe 不存在会发生什么。

So far as I can tell, there aren't any cross-domain restrictions involved here. I'm not sure what happens if the iframe isn't present when the page is first loaded (which is when Greasemonkey runs).

这篇关于使用@include对iframe进行Greasemonkey - 这有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆