我想知道你是否可以只对一个iframe而不是它的父窗口执行Greasemonkey。父窗口是域A,iframe是域B,脚本中的include是@include http://domain-B.com/path/*。
我不需要和父母有任何互动。我已经试过几次了,但都没有成功。是否有任何跨域限制阻止某人对iframe执行?
PS: iframe的JS代码阻止它作为顶部窗口加载。
发布于 2009-11-05 16:29:18
当然,可以让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上运行了下面的测试用户脚本,它成功地隐藏了谷歌的徽标(仅当谷歌在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
不存在会发生什么情况。
https://stackoverflow.com/questions/1677558
复制相似问题