Hello everyone, Let's suppose an element has some attached functions on keydown, which where added using the addEventListener function. I want to add my own function to be executed but move its execution to the top. In other words I want my function to be executed before any other. Changing the source and adding my own addEventListener first is not an option :/ Has anyone faced anything a similar issue? Do you have any ideas on how to solve it? I would like to mention that ANY solution is acceptable even if it requires additional JS frameworks etc. Thanks in advance
Okay, well I don't know why you can't edit your Javascript, which is really the only way to do it properly. The only other way (it might not even work, I'm not a JS expert) is at the very bottom of your html, just before </body> execute code to remove the event listener, then add a new one with your function first.
I cannot edit the JavaScript because it's another website. I am trying to add extra functionality using a browser extension (the extension actually adds a custom JS file to the page's source and the work is done by the external javascript file). My issue with the solution you provided would be that I cannot find a way to list the attached functions to that element. I have also tried preventDefault, preventPropagation, inline registration of my function (onkeydown=) but nothing works. My problem is that the textarea already has a function bind to it and when the user presses ENTER it gets processed. What I want to do is to add something to the text written BEFORE it gets processed. Whatever method I use, I simply cannot get it to execute first. For instance, I simply make it display an alert box and by the time the alert shows up, the text is already processed.
Okay, I guess that is a reasonable way to use it. My way will really be the only way to do it (though I haven't in practise). If you want to send me a link to the JS file you're trying to edit I'll see if I can knock out an example.
Well, you can go to facebook.com/messages and pick any conversation you want That's the textarea I want to hook.
Couldn't have chosen a worse site to try. I'm having the same problem then, I set onkeydown to null and then overrode it but same issue. The problem with the code in facebook is that I'm sure there are a dozen other handlers such as onsubmit, onkeyup or whatever that's overriding it. Or something more stupidly easy. So sorry, but I can't help.