将 Greasemonkey/Tampermonkey 用户脚本应用到 iframe [英] Apply a Greasemonkey/Tampermonkey userscript to an iframe

查看:80
本文介绍了将 Greasemonkey/Tampermonkey 用户脚本应用到 iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让用户脚本在 iframe 运行?

How can I make a userscript run inside an iframe?

推荐答案

在 Greasemonkey(以及 Tampermonkey 和大多数用户脚本引擎)中,如果脚本满足 @include, @exclude 和/或 @match 指令.
而且,一个流行的问题是如何阻止 Greasemonkey 在 iframe 上触发.

In Greasemonkey (And Tampermonkey and most userscript engines) a script will fire on an iframe automatically if it meets the @include, @exclude, and/or @match directives.
And, a popular question is how to stop Greasemonkey from firing on iframes.

因此,如果您的脚本具有以下匹配项:

So, if your script had a match like:

@match https://fiddle.jshell.net/*

它会触发 jsFiddle输出";页面,无论它们是否出现在 iframe 中.

It would fire on jsFiddle "output" pages whether or not they appeared in an iframe.

然后您将检查 window.self 属性.
例如,假设您有一个目标页面,如:

Then you would check the window.self property.
For example, suppose you had a target page like:

<body>
    <h1>I'm some webpage, either same-domain or not.</h1>
    <iframe src="//domain_B.com/somePath/somePage.htm">
...

然后你可以使用这样的脚本:

Then you could use a script like:

// ==UserScript==
// @name    _Fires specially on domain_B.com iframes
// @match   *://domain_B.com/somePath/*
// ==/UserScript==

if (window.top === window.self) {
    //--- Script is on domain_B.com when/if it is the MAIN PAGE.
}
else {
    //--- Script is on domain_B.com when/if it is IN AN IFRAME.
    // DO YOUR STUFF HERE.
}


重要提示:

随着 Greasemonkey 4 的发布,iframes 处理受到严重削弱(以及许多其他东西坏了,此外).
仍然适用于 Tampermonkey、Violentmonkey 和几乎所有其他用户脚本引擎.
强烈建议(包括 Greasemonkey 本身)您不使用 Greasemonkey 4 或更高版本.


Important:

With the release of Greasemonkey 4, iframes handling is severely crippled (and many other things are broken, besides).
It still works properly with Tampermonkey, Violentmonkey and just about every other userscript engine.
It is strongly recommended (including by Greasemonkey itself) that you do not use Greasemonkey 4 or later.

这篇关于将 Greasemonkey/Tampermonkey 用户脚本应用到 iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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