Auto click a button


#1

hi

i want using greasmonkey auto click a button after page load.
this button has this properties:

<button id="btnStartVisit" onclick="StartVisit(1)" class="btn waves-effect waves-light green">start visit<i class="material-icons dp48 left">flash_on</i></button>

#2

document.getElementById('btnStartVisit').dispatchEvent(new MouseEvent("click"));


#3

Hi
Thank you for your help. This code works but when page is reloaded it does not work


#4

Try this:

document.getElementById('btnStartVisit').click();

.click() method simulates mouse click on element.


#5

define 'when page is reloaded'.


#6

Hi
Thanks. this code only work when page is loaded first time and when page is reloaded it does not work


#7

How to define?
please send complete code

This button has This properties first time page is loaded:

<button id="btnStartVisit" onclick="StartVisit(1)" class="btn waves-effect waves-light green">start visit<i class="material-icons dp48 left">flash_on</i></button>

And then second time page is reloaded has This properties:

<button style="outline: 1px solid blue;" id="btnStartVisit" onclick="StartVisit(1)" class="btn waves-effect waves-light green">start visit<i class="material-icons dp48 left">flash_on</i></button>

This is image of page when page is reloaded. In this page code does not work:


#8

See, the problem likely is that the page isnt actually reloading, its changing its content. Without seeing the site's full code, i couldnt tell you how.

The greasemonkey script would have to know when that happens, in order to fire the event a second time.

The code pasted above would still click that button, the id property hasnt changed so it would still find the button just fine.


#9

Thanks. This site is advertisement site based on auto surf . first time the button "start visit" is loaded the your code works successfully and autosurf start to visit but when the ads is finished This message is appeared " due to completion of sites, your automated visit was successfully completed" the code does not work"
at this time (second time) button "start visit" does not clicked automatically by greasemonkey


#10

This is image of page First time page is loaded


#11

This is image of button "start visit" Second time is loaded because of finishing site. In Second time the your code does not work


#12

Yeah, my guess is the system doesnt actually reload the page, it does some form of crossloading, modaling, or replacing to accomplish this, and as such the page isnt actually 'loading' again, meaning the script will only fire once.

Looking at the translated text makes me pause though, and I have to ask; is it against the terms of service of the website you're using to automate this operation?


#13

No it is not against the terms of service of the site. beacause i can click manually on button" start visit" second time and the surf start successfully. i have 2 new idea.
1- can i use greasemonkey open url in specific time? in order to refresh page completely

2- i should install My Weekly Browsing Schedule add on in order to open site address on specific time.but this operation creates extra tabs a long time. then i should use "open link in existing tab extension" in order to remove repeated tabs but this extension is for chrome not for firefox. is any add on similar this for firefox?


#14

Just because you can do it manually doesnt mean it's against the TOS to do it automatically. You should probably look on the site for their TOS.

  1. Greasemonkey, like any javascript interpreter, can execute timed events with the setTimeout or setInterval commands.

2 I have no idea what extensions are available for firefox, i'd suggest searching their extension store with the search bar in that site.


#15

"Open Link In Existing Tab Extension" is for chrome. Can i convert codes of this extension to Greasemonkey codes? In other hand Greasemonkey executes commands of this extension


#16

Hi
i have another idea:
how to open urls from same domain in same tab with greasemonkey?


#17

location.assign(URLHere)


#18

this code does not work. is it correct?


#19

I dont know if greasemonkey (or the browser) imposes limitations on GM scripts such as not being able to set the location object. You'd have to look at greasemonkey's documentation.

location.assign() is a valid javascript command. It's... POSSIBLE that GM is not operating in the 'window' context space, so you might have to define it explicitly as window.location.assign()


#20

IIRC Greasemonkey operates in a "sandbox" and the script needs to intentionally and explicitly use "unsafeWindow". It's called unsafe for good reason. name clashes, global scope etc. all kinds of nasties to take into consideration. AFAIK it is usually relatively easy enough to work with the DOM (append elements, CSS etc.) but when script attempts to work with what is the browser's, things get more tight for security reasons.

You should have seen an error message or three in the console. Do you?