討論 » 開發

Help: I try to make a Userscript that Automatically Click on a button when the button show up.

§
發表於:2015-08-18

Help: I try to make a Userscript that Automatically Click on a button when the button show up.

I need help, I try to make a script that click automatically the "CONFIRM" button when it appear on Jango.com

I tried:

document.getElementsByName("commit")[0].click();


But it does not work :(

Here's a screenshot with the code from the button.

Thanks !

woxxom管理員
§
發表於:2015-08-18
編輯:2015-08-18

Use devtools console to check validity of the element you reference: it might show null in your case.

I would use this: document.querySelector("input[type='submit']").click() or document.forms[0].submit() (assuming this is the first form on the page)

§
發表於:2015-08-18

document.querySelector("input[type='submit']").click()

or more specific:

document.querySelector("#thumbs_updown_form_submit input[type='submit']").click()

§
發表於:2015-08-18

Will try it and come back to tell the results, I'm new to JS in the past I just tweaked/repairing some scripts but I've programmed 20 years ago ;-)

Thanks a lot for the help !

§
發表於:2015-08-18

Hummm,

I tried both way but nothing happen and in the JS console and I receive no error.

Could it be because the confirm button show up only after we use the thumb up/down ?

Here's the "complete" code I've tried:

// ==UserScript==
// @name        Jango Auto-Confirm
// @namespace   Mikhoul
// @description Presse automatically on "Confim" button
// @include     http://www.jango.com/profiles/*
// @version     1
// @grant       none
// ==UserScript==


//document.querySelector("input[type='submit']").click() ;

document.querySelector("#thumbs_updown_form_submit input[type='submit']").click();
woxxom管理員
§
發表於:2015-08-19

Yes, attach a click handler to "thumbs up" element and wait for the submit button to appear using setInterval or MutationObserver, for example:

// ==UserScript==
// @name        Jango Auto-Confirm
// @namespace   Mikhoul
// @description Presse automatically on "Confim" button
// @include     http://www.jango.com/*
// @version     1
// @grant       none
// ==/UserScript==

document.getElementById("player_fav_icon").addEventListener("click", function() {
    setMutationHandler(document, "input[type='submit']", function(observer, node) {
        node.click();
        observer.disconnect();
    });
});

function setMutationHandler(baseNode, selector, cb) {
  var ob = new MutationObserver(function(mutations){
    for (var i=0, ml=mutations.length, m; (i<ml) && (m=mutations[i]); i++)
      for (var j=0, nodes=m.addedNodes, nl=nodes.length, n; (j<nl) && (n=nodes[j]); j++)
        if (n.nodeType == 1) 
          if (n = n.matches(selector) ? n : n.querySelector(selector))
            if (!cb(ob, n))
              return;
  });
  ob.observe(baseNode, {subtree:true, childList:true}); 
}
§
發表於:2015-08-19
編輯:2015-08-19

Oh Boy !!! It's to far advanced for me at this time ! :P

I think I understand the logic...

After tinkering for one hour I have it to work with the thumb up button Yeah

    document.getElementById("btn-fav").addEventListener("click", function() {
        setMutationHandler(document, "input[type='submit']", function(observer, node) {
            node.click();
            observer.disconnect();
        });
    });
    function setMutationHandler(baseNode, selector, cb) {
      var ob = new MutationObserver(function(mutations){
        for (var i=0, ml=mutations.length, m; (i<ml) && (m=mutations[i]); i++)
          for (var j=0, nodes=m.addedNodes, nl=nodes.length, n; (j<nl) && (n=nodes[j]); j++)
            if (n.nodeType == 1) 
              if (n = n.matches(selector) ? n : n.querySelector(selector))
                if (!cb(ob, n))
                  return;
      });
      ob.observe(baseNode, {subtree:true, childList:true}); 
    }

But now I need to add #player_ban selector ( for thumb down) so when it would work with thumb up or thumb down.

How I can do that without writing a second script ;-) for the thumb down ?

In fact I don't know how to put a condition that say to the "click" to fire on thumb up OR thumb down.

Thanks a lot for the help !

woxxom管理員
§
發表於:2015-08-19

Move the event handler out to a separate function and assign it to both elements:

document.getElementById("btn-fav").addEventListener("click", thumbClickHandler);
document.getElementById("player_ban").addEventListener("click", thumbClickHandler);

function thumbClickHandler() {
    setMutationHandler(document, "input[type='submit']", function(observer, node) {
        node.click();
        observer.disconnect();
    });
}

P.S. setMutationHandler is simply enumerating the newly added nodes using a fast basic for loop, not very readable evidently, but it's much faster than concise callbacks on sites with very complex js where 100,000 mutations could occur.

§
發表於:2015-08-19

Thanks a lot for the explanation ! :)

§
發表於:2015-08-20

@wOxxOm

I have a "small" problem with the script:

The script work perfectly except that most of the time I use another script to "Thumb Up/Down" that put the icon to trigger the action on the Firefox tool-bar --> http://i.imgur.com/nnRe7yx.png the result is the listener can't listen since the change are made "outside" the web page.

I'd like to know if we can just "monitor" something in the the "pop-up windows" for thumb Up & Down and after that trigger the "click" on the the button ?

Here's the 2 pop-up that appears for the confirmation:

Thumb-Up http://i.imgur.com/n2XZBTy.png Thumb-Down: http://i.imgur.com/LxOKQva.png

Tell me if you need more source code from the pop-up sections.

Regards

woxxom管理員
§
發表於:2015-08-21

Link that other script.

§
發表於:2015-08-21

I said a script but in fact it's an XPI file for Firefox but you can see the source file easily and understand it a lot better than me ;-)

Here's the link to the source: https://addons.mozilla.org/en-US/firefox/files/browse/299330/

Thanks again for sharing you knowledge ! :)

woxxom管理員
§
發表於:2015-08-21

Well, idk, the easiest solution would be to monitor the submit buttons constantly, not just after clicks:

...............
// ==/UserScript==

setMutationHandler(document, "#thumbs_updown_form_submit input[type='submit']", function(observer, node) {
    node.click();
});

function setMutationHandler(baseNode, selector, cb) {
......................
§
發表於:2015-08-21

Could it be possible with a script to "monitor" the button on the toolbar via Chrome:// ?

§
發表於:2015-08-22

Sorry forget about my request I re-tested it and seem to work now (with the 1st version). 8-)

I will look at the order of my userscripts load maybe there is conflict if a one of the 2 script (involving Jango) loaded before/after the other.

Thanks for your help it was REALLY appreciated ! Have a Good Week-end !

§
發表於:2015-08-27

The script work well usually but I found a small bug.

When my computers goes in "Sleep Mode" and wake up the script don't work no more :( I need to reload the whole page to make it work again.

Do you have a clue why it stop to work?

Regards :)

woxxom管理員
§
發表於:2015-08-27
編輯:2015-08-27

Probably because Chrome unloads all extensions when going to sleep and Tampermonkey doesn't reattach itself afterwards.

§
發表於:2015-08-27

I use Firefox and it don't happen with other add-ons/UserScripts.

woxxom管理員
§
發表於:2015-08-27
編輯:2015-08-27

Welp, I'm short on ideas then as there's nothing peculiar or complex in the script.

§
發表於:2017-11-12

Hi, this works also with a class button instead of id?
I need a script that loop until when button class with text "Try it!" appears.
If in the button there is written other, like "Wait", no click.

Thank you in advance, if there is some similar script on the web or an easy tutorial :(

§
發表於:2018-03-23

I need help, I try to make a script that click automatically the "Make reservation" button click after select date.give me a solution for firefox.plz

https://secure.e-konsulat.gov.pl/Uslugi/RejestracjaTerminu.aspx?IDUSLUGI=8&IDPlacowki=188

發表回覆

登入以回復