Simulate Enter keypress in Facebook chat input with content script

1,820 views
Skip to first unread message

Jan Kleiss

unread,
Jul 14, 2015, 7:14:14 AM7/14/15
to chromium-...@chromium.org
Hey guys,

my extension needs to copy some text to the facebook chat textarea and then send it. The first part works fine and I can also focus the textarea but simulating the Enter key event doesn't work. I've tried pretty much every way that I found on stackoverflow without any success. There are no errors popping up in the dev console or stuff like that.

Thanks for your help in advance :)

PhistucK

unread,
Jul 14, 2015, 7:36:14 AM7/14/15
to Jan Kleiss, Chromium-extensions
Since there is a bug here (as others have mentioned in the other thread), perhaps consider implementing it in a different way.
What exactly are you trying to do?
Must you prevent the submission, or do you simply want to do something to the entered text before it is sent?


PhistucK

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/077f4ced-f675-4f5c-a348-d0f3ec616192%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.

Jan Kleiss

unread,
Jul 14, 2015, 8:19:10 AM7/14/15
to chromium-...@chromium.org, jan.davi...@gmail.com
I must manipulate the message BEFORE it gets sent. Since facebook registers the event handler first, it's pretty much impossible to manipulate it before in the same textarea. I tried using MutationObserver aswell to add my event handler right when the textarea is created but it's too slow. I don't know any other way than adding a fake-textarea with my own event handler that copies the text to the real textarea and then tries to send it with a simulated enter keypress. 

Facebook no longer has a chat-API, therefore the only other way I could think of would be injecting my text manipulation into the minified facebook javascript (which is about 6000 lines of barely readable code). But that would take a ton of time to implement I think.

PhistucK

unread,
Jul 14, 2015, 8:26:42 AM7/14/15
to Jan Kleiss, Chromium-extensions
What event causes the message to be sent? keydown? keypress? keyup?
If by any chance it is not keydown, you can add your event listener to keydown.

Also, does this perhaps work?
var keyEvent = new KeyboardEvent("keypress"); Object.defineProperty(keyEvent, "charCode", {value: 1}); Object.defineProperty(keyEvent, "keyCode", {value: 1});
I see that charCode and keyCode indeed have the manipulated values.
(You might have to run your code in the context of the page for that to work, though)


PhistucK

Jan Kleiss

unread,
Jul 14, 2015, 9:12:06 AM7/14/15
to chromium-...@chromium.org, jan.davi...@gmail.com
Unfortunately 'keydown'..

I added textarea.dispatchEvent(keyEvent); to your code (also tried document.dispatchEv..) and replaced 1 with 13 (Enter keycode) - without success. I've also read that I should create a new "general" Event instead of KeyboardEvent - also no success. I'm getting kinda desperate to be honest..

Jan Kleiss

unread,
Jul 14, 2015, 9:12:58 AM7/14/15
to chromium-...@chromium.org
PS: I'm running this code inside a content script so it should be in the side context..

PhistucK

unread,
Jul 14, 2015, 9:16:52 AM7/14/15
to Jan Kleiss, Chromium-extensions
No, content scripts do not run within the context of the page. They only share the DOM, but they live in an entirely isolated JavaScript "world". This is exactly why I specifically mentioned that.
In order to run within the context of the page, you have to inject your script into the page, using a new script element.


PhistucK

Jan Kleiss

unread,
Jul 14, 2015, 9:57:13 AM7/14/15
to chromium-...@chromium.org, jan.davi...@gmail.com
So I tried 

$("body").append("<script>var event = document.createEvent('Event'); event.initEvent('keydown', true, true);event.keyCode = 13;document.getElementById('chatInput').dispatchEvent(event);</script>");

without success aswell. The JQuery docs say that append() etc. use eval() internally and DO allow to run scripts, so it should work. 

PhistucK

unread,
Jul 14, 2015, 10:16:19 AM7/14/15
to Jan Kleiss, Chromium-extensions
If it uses eval, then it is not good enough, because it will run within the context of your content script.
Inject a full file (make sure you define it as a web accessible resource in your manifest) instead -
var script = document.createElement("script");
script.src = chrome.extension.getURL("script-to-inject.js");
document.body.appendChild(script);


PhistucK

Reply all
Reply to author
Forward
0 new messages