目标页面有一个打开新窗口的按钮:
<img src="resources/icons/3.png" id="iconImage_3"
ondblclick="window.newWindow(3,this.alt,'../messagesBox.aspx?view=inbox&',false,true,null,null,null,null);"
>
我需要访问该窗口中的内容,但遇到了困难。如下面的注释所示,我在脚本代码中收到错误消息。
而且,我只能在线找到有关
window.open
的信息,而不能找到有关window.newwindow
的信息。更新:
在聊天中的SO用户的帮助下,我们了解到
window.newWindow
实际上创建了一个“对话框” ,其中包含具有我要操作的内容的iframe。 (例如,元素
这是我的代码:
如何单击该按钮?
看起来像这样:
然后,当您单击按钮时,它看起来像这样:
iframe和内容是通过AJAX添加的。
单击该iframed按钮的简单Tampermonkey或Greasemonkey脚本是:
注意:
继续并安装该脚本,然后访问the test page。单击第一个按钮时,您将看到一条消息,指示用户脚本单击了第二个按钮。
现在,对于您的特定方案,这样的脚本应单击指示的节点。 (我们无法测试。)
如果可行,则回答此问题。对于其他问题,请打开一个新问题,但是您应该能够通过修改
注意:
您不能为此使用
id="messageHeader_6328087"
。)这是我的代码:
var messages;
var g=0;
function list() {
var p = document.getElementById("messageHeader_6328087");
var aclickEvent = document.createEvent("MouseEvents");
aclickEvent.initEvent('click', false, true);
p.dispatchEvent(aclickEvent);
// getting can't click on null
var r = document.getElementsByClassName("even unread");
var s = document.getElementsByClassName("odd unread");
var msgeven = Array.prototype.slice.call(r, 0);
var msgodd = Array.prototype.slice.call(s, 0);
var k = Math.max(msgodd.length,msgeven.length);
confirm(k);
// this gives 0, so I'm not sure if when returned null they just automatically put //nothing
var i = 0;
while (i < k) {
if (i< msgeven.length) {
messages.push(msgeven[i].id);
}
if (i< msgodd.length) {
messages.push(msgodd[i].id);
}
i = i + 1;
}
alert(messages.length);
// am getting can't take length of undefined, am I using push wrong?
}
如何单击该按钮?
最佳答案
双击该图像(#iconImage_3
)时,目标页面将创建一个<iframe>
,其中包含您要查找的内容。 CSS用于使它看起来像一个新窗口。
这是一个AJAX方案。当页面加载且用户脚本运行时,该iframe不存在。您需要一种使脚本等待该iframe的方法。一种简单,可靠的方法是使用the waitForKeyElements() utility。
例如,访问this Dynamic Iframe test page on jsBin:
<!DOCTYPE html>
<html><head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready (jQueryMain);
function jQueryMain () {
//--- If this is the iframe, change the content.
if (/BeAnIframe/i.test (location.search) ) {
$("body").html (
'<h4>This is iFrame content</h4>' +
'<button id="generateMessageBtn">This is the button to click via userscript.</button>'
);
$("button").click ( function () {
$("body").append ('<p>Button was clicked.</p>');
} );
}
else {
$("button").click ( function () {
$("body").append (
'<div><iframe src="' + location.href + '?BeAnIframe=1"></iframe></div>'
);
} );
}
}
</script>
</head><body>
<p>Click the button below, and then the userscript will click the button that appears in the iframe.</p>
<p><button>Open the iframe below.</button></p>
</body></html>
看起来像这样:
然后,当您单击按钮时,它看起来像这样:
iframe和内容是通过AJAX添加的。
单击该iframed按钮的简单Tampermonkey或Greasemonkey脚本是:
// ==UserScript==
// @name Dynamic iframe content clicker
// @include http://jsbin.com/xuwosovi/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
if (window.top == window.self) {
//-- Only runs if it's not an iframe.
waitForKeyElements (
"#generateMessageBtn",
clickMessageButton,
false,
"iframe[src*='BeAnIframe']"
);
}
function clickMessageButton (jNode) {
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
jNode[0].dispatchEvent (clickEvent);
}
注意:
#generateMessageBtn
是iframed按钮的jQuery selector。iframe[src*='BeAnIframe']
是iframe本身的jQuery选择器,取消了src
属性,这对于您的特定情况很重要。继续并安装该脚本,然后访问the test page。单击第一个按钮时,您将看到一条消息,指示用户脚本单击了第二个按钮。
现在,对于您的特定方案,这样的脚本应单击指示的节点。 (我们无法测试。)
// ==UserScript==
// @name Dynamic iframe content clicker
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
if (window.top == window.self) {
//-- Only runs if it's not an iframe.
waitForKeyElements (
"#messageHeader_6328087",
processMessageWindow,
false,
"iframe[src*='messagesBox.aspx?view=inbox']"
);
}
function processMessageWindow (jNode) {
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
jNode[0].dispatchEvent (clickEvent);
}
如果可行,则回答此问题。对于其他问题,请打开一个新问题,但是您应该能够通过修改
r
来填充变量s
和processMessageWindow()
,如下所示:function processMessageWindow (jNode) {
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
jNode[0].dispatchEvent (clickEvent);
var frameBody = jNode.parents ("body");
var r = frameBody.find (".even.unread").get ();
var s = frameBody.find (".odd.unread").get ();
// Add additional processing here...
}
注意:
.parents()
,.find()
和.get()
是jQuery函数。您不能为此使用
document.getElementsByClassName
。 document
仍指向父窗口,而不是iframe。