6

I am creating an extension where I must simulate a spacebar keydown on the contenteditable div where the user type the message (statuses) in the Google Plus.

If you open plus.google.com and you click on the div where you must type the message you will realize that when you type a link inside the message and you press {spacebar} you will notice that Google retrieves title, image and description of that link. I am 3 days trying to simulate the spacebar keydown. I can already simulate mouseover over the elements of the form, I can simulate click() in the send button but I still can't simulate the spacebar keydown.

So far my best code is:

var e = new KeyboardEvent("keydown", {bubbles : true, cancelable : true, key : " ", char : " ", shiftKey : false, keyCode: 32, which: 32, view: window});
//I need to delete the e.keyCode and e.which cause Chrome sets it as a readonly but for some reason if you delete it and assign a new value Chrome accepts (I am very sure this is a serious bug that I discovered in Chrome and that I already reported yesterday in theis bug tracker)
delete e.keyCode;
Object.defineProperty(e, "keyCode", {"value" : 32});
delete e.which;
Object.defineProperty(e, "which", {"value" : 32});

$(".Cla").find(".be.c-B").children().eq(1).get(0).dispatchEvent(e);

Thank you so much.

EDIT:

I do not know if it helps you make a test but I advise you do this:

opened_window = window.open("http://plus.google.com");

after that click in the div to expand it and after that just run this code:

$(".Cla").find(".be.c-B").children().eq(1).html("The site stackoverflow.com is amazing");

var e = new KeyboardEvent("keydown", {bubbles : true, cancelable : true, key : " ", char : " ", shiftKey : false, keyCode: 32, which: 32, view: window});
delete e.keyCode;
Object.defineProperty(e, "keyCode", {"value" : 32});
delete e.which;
Object.defineProperty(e, "which", {"value" : 32});

$(".Cla").find(".be.c-B").children().eq(1).get(0).dispatchEvent(e);

For some reason this does not trigger the retrieval of link informatino. But if you just click in the div and press spacebar you will see that Google retrieves information about that link.

  • 3
    Also, why don't you explore simply triggering Google's original functionality directly, instead of trying to trigger it indirectly with a simulated keystroke? – Robert Harvey Apr 18 '13 at 21:51
  • @Kenneth it's not duplicated cause I already tested ALL the codes here in the SO and none works. As I said it's really strange the behaviour on that contenteditable div cause I can actually fire and simulate any event in that page, really any event but the keydown spacebar on the element $(".Cla").find(".be.c-B").children().eq(1).get(0) – João Miros Apr 18 '13 at 21:59
  • Have you tried firing the code directly, instead of trying to simulate a keypress? – Robert Harvey Apr 18 '13 at 22:00
  • @Robert Harvey I assigned the event to a variable and then executed that variable with eval() but It does not work either. I really tried every possible code in SO. I read so many posts that I have no more page to read. I already went to the 6th page of Google results and still no solution. – João Miros Apr 18 '13 at 22:03
  • That keydown has to hook to some code somewhere, probably Javascript. ***Fire that code directly.*** – Robert Harvey Apr 18 '13 at 22:05
  • @Robert Harvey sorry but I did not understand what you said. How do I fire the code directly Sir? – João Miros Apr 18 '13 at 22:09
  • How is the keydown event firing it? – Robert Harvey Apr 18 '13 at 22:09
  • 1
    I used the Firebug and traced the keydown event and it fires 6 functions chained. I got really lost tracing the onkeydown. Actually if someone can tell me how to check what function exactly caused the XHR to retrieve information about the URL. – João Miros Apr 18 '13 at 22:16
  • I don't think firing the eventhandler direct will be much help... As @JoãoMiros has seen, Google has put a massive of code to handle user behavior(and compressed it into ugly js). I have personally tried to simulate +1 button(simply by `$('.plus-one-button').click()`), but it turns out that g+ will monitor more events to screen 'real' behavior from script firing. – Herrington Darkholme May 01 '13 at 09:34

1 Answers1

0

Have a look at this jQuery plugin: SendKeys

Dario Barilà
  • 515
  • 6
  • 5