views:

252

answers:

2

I'm writing a Chrome extension that launches a script with a keyboard shortcut. It works fine on most pages but I realized that on Gmail it doesn't: it seems that all keyboard events are captured by Gmail and are not bubbled up to my function.

I have a content script (in Chrome extension this is added to any page you want) that has (simplified of course):

document.body.addEventListener('keypress', myFunction, true);
function myFunction(event) {
    console.log("yay, Gmail didn't let me down!");
}

But actually, Gmail does let me down. I know that the script is loaded. I tried different variations of window.addEventListener and other event types to no avail.

Does anybody know of a way to bypass this? I tried to see if GreaseMonkey script could do it, that brought me here: http://code.google.com/p/gmail-greasemonkey/ but that didn't help me.

Thanks!

A: 

You could try a process of redirection:

if (document.body.onkeypress) {
    // add as event listener instead
    var kpfunc = document.body.onkeypress;
    document.body.addEventListener('keypress', kpfunc, true);
}
amphetamachine
`document.body.onkeypress` returns null though.What difference do the two method make? In doubt, I tried to do `document.body.onkeypress = function (){alert('sdf');}` as well as `window.onkeypress` with no better results.
Timothée Boucher
A: 

I don't know the inner workings of GMail's keyboard event capturing, but I recently wrote a simple keyboard shortcut navigator (so I don't have to use the mouse to click links) for Chrome.

It's not an extension, but a user/Greasemonkey script, but it's triggered by typing comma (,) twice, and it works in GMail.

Maybe it'll help you to look at the source. You can download it here: http://userscripts.org/scripts/show/68609

Martin R-L
Thanks I'll have a look and try to get to what I need. I'll update if I do.
Timothée Boucher
Ok, I figured it out. It turns out it wasn't really related to Gmail capturing the event and keeping it for itself: there's a setting of Chrome extensions that makes the script match all frames or just the top one. It defaults to only the top one, and Gmail uses iframes. So my script was not loaded where it needed to. (http://code.google.com/chrome/extensions/content_scripts.html) Thanks for the working example, it helped me dig further.
Timothée Boucher
Great! ... and good luck!
Martin R-L