0

I'm writing a Chrome extension and I'm trying to programmatically trigger a keypress event on the textarea element where you type in Facebook chat.

If I look at the element in the inspector, I can see that it has an onkeydown handler set:

onkeydown="run_with(this, ["legacy:control-textarea"], function() {TextAreaControl.getInstance(this)});"

--but I can't trigger it. While trying to figure out why I can't trigger it, I found that when I select the element using one of the classes on it and type document.querySelector('._552m').onkeydown in the console, it comes up null. Likewise, using getAttribute on it for onkeydown comes up null.

What am I missing here? How can I get at this event handler? Why can I see it set on the element in the inspector and yet not access it programmatically in the usual way? Is React pulling some weird magic here?

Edit: document.querySelector('._552m').attributes shows the onkeydown attribute listed....wtf...

temporary_user_name
  • 35,956
  • 47
  • 141
  • 220

1 Answers1

0

If document.querySelector('._552m').attributes displays the onkeydown attribute, you should be able to access the event handler using document.querySelector('._552m').getAttribute('onkeydown') (it works for me, returns this on my facebook page):

"run_with(this, ["legacy:control-textarea"], function() {TextAreaControl.getInstance(this)});"

Otherwise, since the attributes object is just a NamedNodeMap you can just access it using getNamedItem('onkeydown') on document.querySelector('._552m').attributes like this:

var elm = document.querySelector('._552m')
elm.attributes.getNamedItem('onkeydown')    //returns the attr itself
elm.attributes.getNamedItem('onkeydown').value   //returns the string value
trekforever
  • 3,914
  • 25
  • 29