gpt4 book ai didi

iframe - Greasemonkey 使用 @include 对抗 iframe - 这有效吗?

转载 作者:行者123 更新时间:2023-12-02 20:34:27 26 4
gpt4 key购买 nike

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

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

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

最佳答案

好吧,让 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 Logo (仅当 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 不存在会发生什么。

关于iframe - Greasemonkey 使用 @include 对抗 iframe - 这有效吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1677558/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com