Simulate button click in javascript

R

rainer.blickle

Hi to everybody,

i'm trying to simulate a button click, but it doesn't work.

What i'm trying to do ?:

I try to write a little helper for my google mail account. After login
the incoming mail folder is displayed. a little java script marks some
of the incoming mail (done via xpath). The script is executed via
greasemonkey (Firefox).

Code:

if (document.URL.match(/^http:\/\/mail.google.com\/mail\/.*$/)) {
var varn0_iterator = document.evaluate("//tr[TD/span/text()='GMX
Best Price' or td/span/b/text()='GMX Best Price']/td/input", document,
null, XPathResult.ANY_TYPE,null);
var varn0 = varn0_iterator.iterateNext();
while (varn0) {
varn0.checked="true";
varn0 = varn0_iterator.iterateNext();
};
}

So, now the "spam" is marked and i want to move it into the archive
via the "archive"-button.

The button is defined via:

<BUTTON style="font-weight: bold;" id="ac_rc_^i" class="ab"
type="button">
Archivieren</BUTTON>

(i got the definition via the "DOM Inspector" plugin of firefox).

So, the checkboxes are marked for the "spam". now i tried to do a
click via
document.evaluate("//button[text()='Archivieren']", document, null,
XPathResult.ANY_TYPE,null).iterateNext().click();

but this doesn't work. The xpath returns a button, but the click
doesn't make any effect. As you can see, i am a german user of gmail.

Can anybody help me how to simulate a button click ?

Any additional information needed ?

Regards Rainer
 
D

David Mark

Hi to everybody,

i'm trying to simulate a button click, but it doesn't work.

What i'm trying to do ?:

I try to write a little helper for my google mail account. After login
the incoming mail folder is displayed. a little java script marks some
of the incoming mail (done via xpath). The script is executed via
greasemonkey (Firefox).

Code:

if (document.URL.match(/^http:\/\/mail.google.com\/mail\/.*$/)) {
  var varn0_iterator = document.evaluate("//tr[TD/span/text()='GMX
Best Price' or td/span/b/text()='GMX Best Price']/td/input", document,
null, XPathResult.ANY_TYPE,null);
  var varn0 = varn0_iterator.iterateNext();
  while (varn0) {
    varn0.checked="true";
    varn0 = varn0_iterator.iterateNext();
  };

}

So, now the "spam" is marked and i want to move it into the archive
via the "archive"-button.

The button is defined via:

<BUTTON style="font-weight: bold;" id="ac_rc_^i" class="ab"
type="button">
Archivieren</BUTTON>

(i got the definition via the "DOM Inspector" plugin of firefox).

So, the checkboxes are marked for the "spam". now i tried to do a
click via
document.evaluate("//button[text()='Archivieren']", document, null,
XPathResult.ANY_TYPE,null).iterateNext().click();

but this doesn't work. The xpath returns a button, but the click
doesn't make any effect. As you can see, i am a german user of gmail.

Can anybody help me how to simulate a button click ?

http://developer.mozilla.org/en/docs/DOM:document.createEvent

The German version appears to be missing.
 
R

rainer.blickle

Hi David,

thank you very much for your tip. It works !

Here is my complete code for others (who have the same question):

if (document.URL.match(/^http:\/\/mail.google.com\/mail\/.*$/)) {
var varn0_iterator = document.evaluate("//tr[TD/span/text()='GMX
Best Price' or td/span/b/text()='GMX Best Price']/td/input", document,
null, XPathResult.ANY_TYPE,null);
var varn0 = varn0_iterator.iterateNext();
while (varn0) {
varn0.checked="true";
varn0 = varn0_iterator.iterateNext();
};
var button = document.evaluate("//button[text()='Archivieren']",
document, null, XPathResult.ANY_TYPE,null).iterateNext();
if (button) {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0, false, false, false, false, 0, null);
button.dispatchEvent(evt);
}
}

I added an if(button) because the script is called multiple times. The
button doesn't exists in the earlier cal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top