apscience
apscience

Reputation: 7253

How can I refer to Gmail's textbox?

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?

Upvotes: 3

Views: 441

Answers (2)

Brock Adams
Brock Adams

Reputation: 93493

(".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.

Upvotes: 2

Jan Sommer
Jan Sommer

Reputation: 3798

It's not pretty but it does the job:

document
    .querySelector(".editable")
    .contentDocument
    .querySelector(".editable")
    .onkeypress = function() { alert("jayer!"); };

Upvotes: 1

Related Questions