Monitor all Events on an Element

I’ve recently started researching autofill and what hints that browsers give to developers that they have automatically filled in a form field on the users behalf. Blink and WebKit browsers have a special CSS pseudo class that you can look at (more in another post), but firefox doesn’t. There must be some event!!!
Chrome DevTools has a handy helper function called monitorEvents, you call it with an element as an argument and it will then log to the console all the events that happen on that element.


This content originally appeared on Modern Web Development with Chrome and was authored by Paul Kinlan

<p>I've recently started researching autofill and what hints that browsers give to developers that they have automatically filled in a form field on the users behalf. Blink and WebKit browsers have a special CSS pseudo class that you can look at (more in another post), but firefox doesn't. There must be some event!!!</p> <p>Chrome DevTools has a handy helper function called <code>monitorEvents</code>, you call it with an element as an argument and it will then log to the console all the events that happen on that element. Meggin Kearny on our team and Flavio Cotes wrote about <a href="https://developers.google.com/web/tools/chrome-devtools/console/events">monitorEvents</a> and all the other helper functions recently on our <a href="https://developers.google.com/web">WebFundamentals</a> site.</p> <p>Firefox DevTools don't have this utility function, so I wrote my own.</p> <p>There are no guarantees for accuracy, but it worked for me ;)</p> <div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-javascript" data-lang="javascript"><span style="color:#66d9ef">function</span> <span style="color:#a6e22e">monitorEvents</span>(<span style="color:#a6e22e">element</span>) { <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">log</span> <span style="color:#f92672">=</span> <span style="color:#66d9ef">function</span>(<span style="color:#a6e22e">e</span>) { <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#a6e22e">e</span>);}; <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">events</span> <span style="color:#f92672">=</span> []; <span style="color:#66d9ef">for</span>(<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">i</span> <span style="color:#66d9ef">in</span> <span style="color:#a6e22e">element</span>) { <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">i</span>.<span style="color:#a6e22e">startsWith</span>(<span style="color:#e6db74">"on"</span>)) <span style="color:#a6e22e">events</span>.<span style="color:#a6e22e">push</span>(<span style="color:#a6e22e">i</span>.<span style="color:#a6e22e">substr</span>(<span style="color:#ae81ff">2</span>)); } <span style="color:#a6e22e">events</span>.<span style="color:#a6e22e">forEach</span>(<span style="color:#66d9ef">function</span>(<span style="color:#a6e22e">eventName</span>) { <span style="color:#a6e22e">element</span>.<span style="color:#a6e22e">addEventListener</span>(<span style="color:#a6e22e">eventName</span>, <span style="color:#a6e22e">log</span>); }); } </code></pre></div><p>If you are a person who likes gists, <a href="https://gist.github.com/PaulKinlan/45de2fb55c1390d871b3a67f72ae730c">then here you go</a>.</p>


This content originally appeared on Modern Web Development with Chrome and was authored by Paul Kinlan


Print Share Comment Cite Upload Translate Updates
APA

Paul Kinlan | Sciencx (2016-10-13T00:00:00+00:00) Monitor all Events on an Element. Retrieved from https://www.scien.cx/2016/10/13/monitor-all-events-on-an-element/

MLA
" » Monitor all Events on an Element." Paul Kinlan | Sciencx - Thursday October 13, 2016, https://www.scien.cx/2016/10/13/monitor-all-events-on-an-element/
HARVARD
Paul Kinlan | Sciencx Thursday October 13, 2016 » Monitor all Events on an Element., viewed ,<https://www.scien.cx/2016/10/13/monitor-all-events-on-an-element/>
VANCOUVER
Paul Kinlan | Sciencx - » Monitor all Events on an Element. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2016/10/13/monitor-all-events-on-an-element/
CHICAGO
" » Monitor all Events on an Element." Paul Kinlan | Sciencx - Accessed . https://www.scien.cx/2016/10/13/monitor-all-events-on-an-element/
IEEE
" » Monitor all Events on an Element." Paul Kinlan | Sciencx [Online]. Available: https://www.scien.cx/2016/10/13/monitor-all-events-on-an-element/. [Accessed: ]
rf:citation
» Monitor all Events on an Element | Paul Kinlan | Sciencx | https://www.scien.cx/2016/10/13/monitor-all-events-on-an-element/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.