I’m creating a Chrome extension using user’s key event.
But I want to alert to user if the element already has been bound to a keyboard event.
How do I detect if key event is already bound?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is tricky. The reality is, you can bind to an HTMLElement’s event in quite a few different ways. I suppose the easiest way to bind (and check to see if something is bound) would be something like this:
The problem is, as asked in the comment section to this post, there’s no real way to detect events added by addEventListener or attachEvent with programattic javascript if you don’t have the ability to change the prototype for the HTML elements before the events are bound. (So a userscript can’t really change the prototypes before events are bound, because it’s run after all the scripts on the page are). However, if it’s your own page, and you’re just wanting to monitor for these event changes. You can hack it into the prototype by doing something like this:
Looking, I found this question here which also covers this exact issue, and some good answers in there go in to alot of depth: How to find event listeners on a DOM node when debugging or from the JavaScript code?