我正在尝试(使用 Greasemonkey)在完全基于 Ajax 的页面上模拟几个动作。 为 HTML 粘贴 Bin
第一步是保存页面上的数据。这会导致刷新下拉列表(ID 为 BP_Content_ddlCoupon)。然后我需要选择刚刚保存的这个下拉选项,以便我可以复制它。然后我对该项目进行更改并保存它,只是为了重新开始该过程。
但是,我不明白例程 WaitForKeyElements 是如何工作的。以下代码似乎有效。但是,函数 fnRetrieveTheCurrent 需要“重新选择”刚刚保存的选项,以便保存它的副本。
// ==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 更改为此时,它停止工作并且似乎处于无限循环中。
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);
}
}