2

I'm working on a Greasemonkey script for gmail and I need to find a way to refer to gmail's textboxes (when you compose and reply to an email). Gmail has an iframe with a new HTML and the text is in the body, as part of it's WYSIWYG editor. I've tried selecting all the classes of those elements with jquery, but they don't work -- specially, .keypress()

Here's what I have tried:

$(".editable LW-avf").keypress(function(event) {
... // the wysiwyg body
});

$(".Am Al editable").keypress(function(event) {
... // the iframe
});

Is there any way to bind a keypress event handler? Or is it even possible since gmail isn't a conventional textbox?

apscience
  • 7,033
  • 11
  • 55
  • 89
  • If you want to hit "reply" programatically you can write document.querySelector(".ams").click() ... Don't know if that answers your question. – Jan Sommer Oct 01 '12 at 10:46
  • Sorry, I don't want to hit the reply button - what I want to do is prevent the tab key from tabbing out, so I'll need to bind the keypress listener. – apscience Oct 01 '12 at 10:52

2 Answers2

1

It's not pretty but it does the job:

document
    .querySelector(".editable")
    .contentDocument
    .querySelector(".editable")
    .onkeypress = function() { alert("jayer!"); };
Jan Sommer
  • 3,698
  • 1
  • 21
  • 35
  • Thank you, however event.preventDefault() does not work for the tab key - do you know how to get it working for the tab key? – apscience Oct 01 '12 at 11:26
1

(".editable LW-avf") would select not <body class="editable LW-avf">.

It would select something like:

<div class="editable">
    <LW-avf></LW-avf>
</div>

If that existed.

You want:

$("body.editable.LW-avf")

(Two classes, two dots in the selector.)

Also:

  1. Make sure the script is set to run on iframes (it is by default).
  2. You want to use keydown, not keypress, for your purposes.
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Thank you, however event.preventDefault() does not work for the tab key - do you know how to suppress the tab key? I am doing it on keydown, and other keys works fine. – apscience Oct 01 '12 at 11:26
  • That's a different question! The stated question is answered. For the tab key, see http://stackoverflow.com/q/3362, but Gmail may block the customary fixes. – Brock Adams Oct 01 '12 at 12:02
  • Confirmed. Once again Gmail uses FUBAR JS, and the standard tab capture doesn't work. Open a new question for the new problem. – Brock Adams Oct 01 '12 at 12:05