Skip to content Skip to sidebar Skip to footer

Clicking A Button On A Page Using A Greasemonkey/userscript In Chrome

I'm going to be as absolutely verbose here as possible as I've run into a few solutions that didn't end up panning out. Please keep in mind that I don't know Javascript. I know bas

Solution 1:

Note:

  1. jQuery .click() does not work reliably on click events that were not set via jQuery in the first place.

  2. You need to create the right kind of event; createEvent('Events') is not the way.

  3. As Jim Deville pointed out, the link pseudo-button was not being selected.

  4. You do not need jQuery for this (so far).

  5. Make sure that the "Refresh" control is loaded statically for the test code as shown in the question. If it's AJAXed in, the click attempt may fire too soon.

Putting that all together, the following code should work. But beware that some sites (like certain Google apps) use funky, state-sensitive designs -- which require a sequence of events.

var refreshBtn = document.querySelector (
    "div.serverguide-header-refresh-button div[type='reset'] a"
);
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
refreshBtn.dispatchEvent (clickEvent);

Solution 2:

so, you are on the right track, but you keep grabbing the DIV not the A tag. To click on the "button" (link in this case), you have to click on the actual link because it won't tunnel down to the elements contained in the DIV.

document.querySelector('serverguide-header-refresh-button a')

Should get you the A element to click on. From jQuery $('serverguide-header-refresh-button a').click(); should work.

Post a Comment for "Clicking A Button On A Page Using A Greasemonkey/userscript In Chrome"