1

I have a userscript for Google Reader, and as part of that userscript I need to trigger a refresh, which I do in Firefox by simulating a keypress for the letter r.

function simulateRefresh()
{
    var e = document.createEvent('KeyboardEvent');
    e.initKeyEvent('keypress', true, true, window, false, false, false, false, 82, 82);
    document.body.dispatchEvent(e);
}

This works in Firefox, but not in Chrome. Apparently initKeyEvent is not supported, and I'm supposed to use initKeyboardEvent.
So far I've had zero luck with this (There is no error in the console, but the refresh does not fire.) I am using jQuery if that matters.

I also tried triggering click on the refresh button, but this failed in both browsers (not sure why, the click event is firing according to the Chrome debugger, but the code is obfuscated, so I can't figure it out).

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Pyro979
  • 202
  • 1
  • 10
  • Couldn't you just ask the browser to do a refresh? I don't understand the need to fake a keypress. what about the options given here: http://stackoverflow.com/questions/3715047/how-to-reload-a-page-using-javascript – enhzflep Oct 06 '12 at 17:04
  • 2
    Well I don't wanna refresh the whole webapp, just have it refetch the result for a specific feed (Starred Items in this case). Refreshing the whole thing is a bit of an overkill. – Pyro979 Oct 06 '12 at 17:14

4 Answers4

1

Google webapps often do not use a simple keypress or click, when a complex sequence of states will do. ;-)

So sending a "click" to the refresh button does not work, but sending an "over, down, up" triad does. Like so:

var refreshBtn = document.querySelector ('#viewer-refresh');

//--- Click by itself does not work!
triggerMouseEvent (refreshBtn, 'mouseover');
triggerMouseEvent (refreshBtn, 'mousedown');
triggerMouseEvent (refreshBtn, 'mouseup');

function triggerMouseEvent (node, eventType) {
    var clickEvent = document.createEvent('MouseEvents');
    clickEvent.initEvent (eventType, true, true);
    node.dispatchEvent (clickEvent);
}


A similar kind of state-sequence may be required for the keyboard shortcut. (Testing continues.)

Update: For the keyboard events, see this answer which mentions Webkit Bug 16735: "Keyboard events created with DOM have keyCode and charCode of 0" -- which is still open after 4.5 years(!!) and might apply here.

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Works like a charm =c) Thanks. I'm off to integrate the code. Now that i can click on stuff this might end up being an even better solution then the keyboard events. – Pyro979 Oct 06 '12 at 19:23
  • A workaround solution for the mentioned webkit keyboard event bug is posted here: http://stackoverflow.com/questions/10455626/keydown-simulation-in-chrome-fires-normally-but-not-the-correct-key/10520017#10520017 – tanushree Dec 24 '12 at 15:55
0

Tried $('#refreshButton').trigger('click'); ?

mschr
  • 8,531
  • 3
  • 21
  • 35
0

Your solution is to check whether the initKeyboardEvent method is implemented or not.

var e = document.createEvent('KeyboardEvents');
(e.initKeyboardEvent || e.initKeyEvent).call(e, 'keypress', true, true, window, false, false, false, false, 82, 82);
document.body.dispatchEvent(e);

This works for me in Chrome.

J. K.
  • 8,268
  • 1
  • 36
  • 35
  • This was the first thing I tried when I figured out it's initKeyboardEvent in Chrome, but it's not working for me. Same thing doesn't error, but doesn't work. I have Chrome Version 22.0.1229.79 m, don't know if this makes a difference. – Pyro979 Oct 06 '12 at 17:13
  • Can you make a JSFiddle for us to try? – J. K. Oct 06 '12 at 18:23
  • The issue is probably not in Chrome, but with Google reader. Google webapps are notorious for this kind of thing. – Brock Adams Oct 06 '12 at 18:49
  • Brock's right. Just some Google silliness. I ended up using his mouse event solution. – Pyro979 Oct 06 '12 at 19:34
0

I think you are trying to dispatch the event from your content script, unfortunately that does not work. You need to inject the code into the page:

function injectJavaScript(textContent) {
  var scriptElement = document.createElement('script');
  scriptElement.textContent = textContent;

  (document.getElementsByTagName('head')[0] || document.body || document.documentElement).appendChild(scriptElement);
}

var code = '(' + function() {
  var e = document.createEvent('KeyboardEvents');
  e.initKeyboardEvent('keypress', true, true, window, false, false, false, false, 82, 82);
  document.body.dispatchEvent(e);
} + ')();';

injectJavaScript(code);
Community
  • 1
  • 1
dan-lee
  • 14,365
  • 5
  • 52
  • 77
  • I don't know what this ominous Google Reader is, but I thought it was a more common problem. – dan-lee Oct 06 '12 at 18:34
  • The title seems generic, but the Q text makes clear that the problem is specific to *Google Reader*. Your answer would work on a sensibly coded site, but not on *Google Reader*. – Brock Adams Oct 06 '12 at 18:37
  • Thanks Dan, but this solution doesn't work. I'm gonna end up using the mouse solution from Brock. That being said, i might also test sending keydown/keypress/keyup combo as well, and see if it's the same pattern. – Pyro979 Oct 06 '12 at 19:25
  • I've tried various KB combos... So far with no luck on Google reader. Next step (for me) is to see if it's that Chrome bug in play. Issue back-burnered, for now. – Brock Adams Oct 06 '12 at 19:36