如何使用户脚本在iframe中运行?
发布于 2016-06-03 22:12:57
在Greasemonkey (以及Tampermonkey和大多数用户脚本引擎)中,如果脚本满足@include、@排除和/或@match指令,它将自动在iframe上启动。
而且,一个很流行的问题是如何阻止Greasemonkey在iframes上射击。
所以,如果您的脚本有一个匹配的,例如:
@match https://fiddle.jshell.net/*
它会激发jsFiddle“输出”页面,不管它们是否出现在iframe中。
如果您想对一个刚刚发布的内容开火:
然后检查window.self
属性。
例如,假设有一个目标页面,如下所示:
<body>
<h1>I'm some webpage, either same-domain or not.</h1>
<iframe src="//domain_B.com/somePath/somePage.htm">
...
然后,您可以使用这样的脚本:
// ==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,如果处理严重瘫痪的发布(以及其他很多东西都坏了)。
它的仍然可以正常工作,与其他所有的用户脚本引擎一样,坦佩猴子、Violent猴子和也能正常工作。
强烈建议(包括Greasemonkey自己)不要使用Greasemonkey 4或更高版本。
发布于 2019-04-24 19:32:03
对于iframe
没有位置可以触发@include
或@match
的情况,这是一种解决方案。
--这适用于Greasemonkey 4,更新为使用4.11
我们必须等待每个帧加载,然后才能对其进行操作。我这样做的方法是使用waitForKeyElements.js
,它等待匹配给定CSS选择器的元素,就像循环遍历document.querySelectorAll("selector")
中的匹配项一样,然后将给定的函数应用于响应:
// ==UserScript==
// @include https://blah.example.com/*
// @require https://git.io/waitForKeyElements.js
// ==/UserScript==
function main(where) {
// do stuff here with `where` instead of `document`
// e.g. use where.querySelector() in place of document.querySelector()
// and add stylesheets with where.head.appendChild(stylesheet)
}
main(document); // run it on the top level document (as normal)
waitForKeyElements("iframe, frame", function(frame) {
frame.addEventListener('load', function(e) {
// give main() the `document` from the frame each time it loads
main(e.event.contentDocument);
});
});
请注意,所选元素只是一个占位符,指示已加载iframe
。Firefox将框架的DOM存储在单独的文档中,我们必须显式地单击该文档。还请参阅EventTarget.addEventListener()
和HTMLIFrameElement.contentDocument
的MDN文档。
一个此代码的旧版本从waitForKeyElements
中重新开发了“adding”跟踪器,但是这不再是必要的了,因为我们正在向框架中添加一个侦听器,该监听器监视未来的内容加载,然后将重新运行我们的内部函数。
发布于 2018-02-06 10:28:04
注意,如果您正在为您的userscript创建一个chrome扩展,您还需要将"all_frames": true
添加到清单中,否则您的扩展将无法在iframes上工作。
清单文件中的示例:
"content_scripts": [
{
"matches": ["*://*/*"],
"all_frames": true,
"js":["dont.js"],
"run_at":"document_start"
}
]
https://stackoverflow.com/questions/37616818
复制相似问题