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

参见英文答案 > Apply a Greasemonkey/Tampermonkey/userscript to an iframe?2个
我想知道你是否可以只针对iframe执行Greasemonkey执行,而不是它的父窗口.父窗口是域A,iframe是域B,脚本中的include是@include http://domain-B.com/path/ *.

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

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

解决方法

好吧,当然可以让Greasemonkey针对iframe运行 – 实际上,确定如何阻止它在iframe和主页面上执行是一个 common question.您应该能够采取相应的答案,以防止代码在顶部窗口上执行:
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不存在会发生什么.

相关文章

vue阻止冒泡事件 阻止点击事件的执行 <div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些