1

I am trying to (using Greasemonkey) simulate several actions on a page that is entirely Ajax based. Paste Bin for HTML

First step is to save the data on the page. Which causes a dropdown (with id of BP_Content_ddlCoupon) to be refreshed. I then need to select this drop down option just saved so I can make a copy of it. I then make changes to that item and save it, only to start the process over again.

However, I don't understand how the routine WaitForKeyElements is working. The following code seems to work. However, the function fnRetrieveTheCurrent needs to "re-select" the option that was just saved, so that a copy of it can be saved.

// ==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)

However, when I change the function fnRetrieveTheCurrent to this, it stops working and appears to be in an endless loop.

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);
    }

 }
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Bill Tepe
  • 53
  • 5
  • Do you get a bunch of `1 In fnRetrieveTheCurrent` messages and never get a `2 In fnRetrieveTheCurrent` message? Or do you get bunches of those too. A series of step-by-step screenshots, of you going through the manual process, may be needed. ... ... Code error: `window.addEventListener("load", fnAddButton(), false);` ... should be: `window.addEventListener("load", fnAddButton, false);` ... Also, I don't think `if (jNode[0].selectedIndex != 0) {return true;}` will work. You need to test the text or val for "New". EG: `if (jNode.val() != -1) {return true;}` – Brock Adams Oct 22 '13 at 04:15
  • Brock, thanks so much for responding. Sorry so late checking in on it. The author of the website made some changes of his own to make this script not needed anymore. However, I'm sure I will need more scripts for other pages of the site. Which, because everything is Ajax driven, I'm sure I will have a great need for your script. Thanks for pointing out code errors, I'll be sure to fix my other scripts. **Question**, what is the difference between jNode[0] and jNode ... and how do you know when to use what version? Thanks for all you do to help us newbies! – Bill Tepe Oct 26 '13 at 13:46
  • `jNode` is a *jQuery object* and `jNode[0]` is the first *DOM object* in it. See [jQuery's explanation](http://learn.jquery.com/using-jquery-core/jquery-object/) for some of the difference. Choosing which to use is a bit of an art, based on knowing both APIs. – Brock Adams Oct 26 '13 at 20:59
  • Thanks Brock. Appreciate your help and the link to the explanation! – Bill Tepe Oct 28 '13 at 19:06

0 Answers0