Javascript WaitFork在页面上自动执行多个任务
我试图(使用Greasemonkey)在一个完全基于Ajax的页面上模拟几个动作 第一步是在页面上保存数据。这会导致刷新下拉列表(id为BP_Content_ddlu)。然后我需要选择刚刚保存的这个下拉选项,这样我就可以复制它了。然后,我对该项进行更改并保存它,结果再次启动该过程 然而,我不明白例行的等待是如何运作的。下面的代码似乎有效。但是,函数fnRetrieveTheCurrent需要“重新选择”刚刚保存的选项,以便保存其副本Javascript WaitFork在页面上自动执行多个任务,javascript,jquery,greasemonkey,Javascript,Jquery,Greasemonkey,我试图(使用Greasemonkey)在一个完全基于Ajax的页面上模拟几个动作 第一步是在页面上保存数据。这会导致刷新下拉列表(id为BP_Content_ddlu)。然后我需要选择刚刚保存的这个下拉选项,这样我就可以复制它了。然后,我对该项进行更改并保存它,结果再次启动该过程 然而,我不明白例行的等待是如何运作的。下面的代码似乎有效。但是,函数fnRetrieveTheCurrent需要“重新选择”刚刚保存的选项,以便保存其副本 // ==UserScript== // @name
// ==UserScript==
// @name Letsget - yet another test script
// @namespace http://localhost.localdomain
// @include https://admin.letsget.net/Private/MenuCoupon.aspx
// @version 1
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==
var myCoupID, myCoupText;
window.addEventListener("load", fnAddButton(), false);
function fnAddButton() {
var buttonElems = document.getElementById('uHeaderLinks_lblMessage');
buttonElems.outerHTML = buttonElems.outerHTML + '<input id="gmbSaveAndCopy" type="button" value="Test Click" />';
addButtonListener();
}
function addButtonListener(){
var button = document.getElementById("gmbSaveAndCopy");
button.addEventListener('click',fnClickMe,false);
}
function fnClickMe() {
triggerMouseEvent ($('#BP_Content_btnSave_btnAction')[0], "click");
//evtx = document.createEvent("MouseEvents");
//evtx.initEvent("click", true,true);
//document.getElementById("BP_Content_btnSave_btnAction").dispatchEvent(evtx);
//$('#BP_Content_btnSave_btnAction').trigger('click');
console.log('clicked');
myCoupText = $('#BP_Content_txtCouponDescription').val();
myCoupID = $('#BP_Content_ddlCoupon').selectedOption().index;
//Wait till the screen saves the current record, which you know by the fact that the
// "New" item shows up as the selected item which has an index of 0
waitForKeyElements ("#BP_Content_ddlCoupon", fnRetrieveTheCurrent, true);
}
function fnRetrieveTheCurrent (jNode) {
console.log('1 In fnRetrieveTheCurrent for: ' + jNode[0].selectedIndex);
// Check to see if the screen is showing the "NEW" item in the drop down which is the
// indication that the record has been saved. (its index is 0)
if (jNode[0].selectedIndex != 0) {return true;}
console.log('2 In fnRetrieveTheCurrent for: ' + jNode[0].selectedIndex);
}
//*********************************************************************************
function triggerMouseEvent (node, eventType) {
var clickEvent = document.createEvent('MouseEvents');
clickEvent.initEvent (eventType, true, true);
node.dispatchEvent (clickEvent);
}
function triggerHtmlEvent (node, eventType) {
var clickEvent = document.createEvent('HTMLEvents');
clickEvent.initEvent (eventType, true, true);
node.dispatchEvent (clickEvent);
}
(function($) {
$.fn.selectedOption = function() {
var sel = this[0];
return sel.options[sel.selectedIndex];
};
})(jQuery)
您是否在fnRetrieveTheCurrent消息中收到一堆
1而在fnRetrieveTheCurrent
消息中从未收到2?或者你也会得到一堆这样的东西。您可能需要一系列逐步的屏幕截图,以了解手动过程。。。代码错误:window.addEventListener(“加载”,fnAddButton(),false)代码>。。。应该是:window.addEventListener(“加载”,fnAddButton,false)代码>。。。另外,我认为if(jNode[0].selectedIndex!=0){return true;}
不会起作用。您需要测试文本或val的“新建”。例如:if(jNode.val()!=-1){return true;}
Brock,非常感谢您的回复。抱歉这么晚才查到。该网站的作者自己做了一些修改,使这个脚本不再需要了。不过,我相信我需要更多的脚本为其他网页的网站。因为一切都是Ajax驱动的,我相信我会非常需要你的脚本。感谢您指出代码错误,我一定会修复我的其他脚本。问题,jNode[0]和jNode之间的区别是什么。。。你怎么知道什么时候使用哪个版本?谢谢你为帮助我们这些新手所做的一切jNode
是一个jQuery对象,jNode[0]
是其中的第一个DOM对象。看看有什么不同。基于对这两种API的了解,选择使用哪种API是一门艺术。谢谢Brock。感谢您的帮助和解释的链接!
function fnRetrieveTheCurrent (jNode) {
console.log('1 In fnRetrieveTheCurrent for: ' + jNode[0].selectedIndex);
// Check to see if the screen is showing the "NEW" item in the drop down which is the
// indication that the record has been saved. (its index is 0)
if (jNode[0].selectedIndex != 0) {return true;}
console.log('2 In fnRetrieveTheCurrent for: ' + jNode[0].selectedIndex);
//Re select the option that was just saved so we can make a copy of it
$("#BP_Content_ddlCoupon option:contains(" + myCoupText + ")").prop('selected', 'selected');
if ($('#BP_Content_ddlCoupon').selectedOption().index > 0) {
triggerHtmlEvent(jNode[0],'change'); // this forces the page to change to this option info
//waitForKeyElements ("#BP_Content_txtCouponDescription[value='"+myCoupText+"']",fnSaveTheCopy);
// waitForKeyElements ("#BP_Content_txtCouponDescription",fnSaveTheCopy, true);
}
}