Tampermonkey Click a Button

By : J dane
Source: Stackoverflow.com
Question!

The target page has this source:

jQuery(function () {
    var delay = 1000; //1 sn
    var id = "0b9cfb9f62ef7a2";
    jQuery.ajax({
        "method":"get",
        "url":"?ajax="+id,
        "success": function (json) {
            //alert(json);
            json = JSON.parse(json);
            setTimeout(function(){
                if(json.data != ""){
                    jQuery("#button").attr("class","btn btn-block btn-danger");
                    jQuery("#button").text(json.data);
                }else{
                    jQuery("#button").click(function(){location.href=json.url});
                    jQuery("#button").attr("class","btn btn-block btn-success");
                    jQuery("#button").text("Download!");
                }
            },delay);
        },
        "error":function(){
            alert("XXX");
        }
    });
});

What I'm trying to do is to click the Download! button once it appears after a few seconds.

I tried to by using this code:

waitForKeyElements ("#button:contains('Download')", clickSubmitBtnWhenItAppears);

function clickSubmitBtnWhenItAppears (jNode) {
    var clickEvent = document.createEvent ('MouseEvents');
    clickEvent.initEvent ('click', true, true);
    jNode[0].dispatchEvent (clickEvent);
}

But it doesn't work! I think the problem is on #button, but I couldn't figure it out. Sorry for my weak knowledge in JS.

By : J dane


Answers

Your code is waiting for the existence of #button. What it needs to do is wait both for the existence of #button and for button to have the text "Download".

In this case, you should be able to do it merely by tuning the jQuery selector like so:

waitForKeyElements ("#button:contains('Download')", clickSubmitBtnWhenItAppears);


For more complex cases, you might need to use more fine tuning with waitForKeyElements().



I hope following code will help you i try this code and it reset the form, so user can add new value.

 $('#reset-textbox').click(function() {
    document.getElementsByClassName("comment_form").reset();
});

or this code also work for you.

  $('#reset-textbox').click(function() {
    $('.comment_form')[0].reset();
});


Disable attribute only associates button and input filelds, the thing you want to do here is , event.preventDefault . It will work like disable, but it will depend upon your coding, if you want to show it disabled, you have to add add via jquery



This video can help you solving your question :)
By: admin