将Greasemonkey / Tampermonkey / userscript应用于iframe? [英] Apply a Greasemonkey/Tampermonkey/userscript to an iframe?
问题描述
标题几乎就是问题:
是否可以将一个Greasemonkey脚本添加到iframed网站?
Is it possible to add a Greasemonkey script to an iframed website?
若然,怎么办?
谢谢。
推荐答案
在Greasemonkey(以及Tampermonkey和大多数用户脚本引擎)中,脚本将自动触发iframe ,如果它符合 @ 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 / Tampermonkey / userscript应用于iframe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!