Title: Chrome extension firing keypress event
Tags: javascript;google-chrome-extension;keyboard-events
Question: This was my original question and was taken to this question very kindly by @rsanchez and @Xan. I would have loved to further ask on the link that they provided for me, but I don't have enough reputation to comment and so I'm having to make a new question here.
So my problem is that I'm developing a chrome extension in which I would like to get message from my server using Pusher and upon this message arriving, I would like to open up the appropriate chat and put the message I recieved from the server onto a textarea with id messageInput and fire the keypress event defined in the page's script.
I now understand that doing
var $message = $("#messageInput");
var e = $.Event("keypress", {keyCode: 13}); //keypress defined for enter
$message.text(myMessage);
$message.trigger(e);
won't work because it doesn't really call the real keypress event of the page's script.
And because I'm using a pusher module, I would like to not do the injection method since from what I understand, it's not possible to pass message from my content script to the injected script.
So I would like to use the KeyboardEvent method as suggested by the question that @rsanchez and @Xan pointed out. So I've tried it in the console first, only to see the returned value "true" with no events brownjohn@example.net. So how should I implement the following into my code?
content_script.js
channel.bind('returned_message', function(data) {
// returned_message is the message from server
var $message = $("#messageInput");
var e = $.Event("keypress", {keyCode: 13});
$message.text(data.message);
console.log($message.text());
$message.trigger(e);
here's what I figure I should implement instead of $.Event:
var e = new KeyboardEvent('keypress');
e.code = 13;
document.querySelector("#messageInput").dispatchEvent(e);
Thank you in advance.
Here's the excerpt of the code that I found from the page's native script:
$msgInput.keypress(function(e) {
if(e.keyCode == 13 &amp;&amp; !e.shiftKey) {
var msg = $msgInput.val().trim();
if(msg.length &gt; 0) {
sendMsg(msg);
$msgInput.val('');
}
e.preventDefault();
where at the beginning of the script $msgInput was defined as $msgInput = $("#messageInput").
Comment: Have you considered that the code listens to an event other than `keypress`?

Comment: Take a look at [this answer of mine](http://stackoverflow.com/a/26863396/934239), maybe it will help

Comment: Injected scripts are just dynamically created `` tags (which in this case are set to self-destruct after executing, even). They won't be present on reload, but your content script will be loaded again. If your logic requires a page refresh, **think again and do it better**.

Comment: Would you mind if I close this as a duplicate again? Or, if you prefer, could you post your adapted code as an answer?

Comment: @Xan I have overviewed the native script of the page and have confirmed that my intented element, the textarea with id `messageInput` has a `kepress` event. I'll update my question with the code excerpt of that keypress part

Comment: @Xan Thank you so much it works!!! :D But I just have a quick question about this method. It's injecting a script with a fixed message right? So would simply reloading the page everytime I want to post a different message from the previous one work? From what I've found I think I saw from somewhere that the injected scripts do not get "removed" by simply reloading the page?

Comment: @Xan Ahh okay I see that last line of the solution sets the script to self destruct upon execution. Thank you very much :D And yes, I'd prefer it to be closed as a duplicate. Thanks again :D



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 7, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:01/26/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><google-chrome-extension><keyboard-events>', 'Title': 'Chrome extension firing keypress event', 'ViewCount': 158, 'id': 35008070, 'user_AboutMe': None, 'user_AccountId': 6901444.0, 'user_DisplayName': 'ohnu93', 'user_Reputation': 253.0, 'user_Views': 53.0, 'user_id': 5300934.0}}