1

完全に Ajax ベースのページで (Greasemonkey を使用して) いくつかのアクションをシミュレートしようとしています。 HTML のビンを貼り付け

最初のステップは、ページにデータを保存することです。これにより、ドロップダウン (BP_Content_ddlCoupon の ID を持つ) が更新されます。次に、保存したばかりのこのドロップダウン オプションを選択して、コピーを作成できるようにする必要があります。次に、そのアイテムに変更を加えて保存しますが、プロセスを最初からやり直すだけです。

ただし、ルーチン 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);
    }

 }
4

0 に答える 0