How to get a GreaseMonkey script to listen for elements with a specific class being inserted into a page

StackOverflow https://stackoverflow.com/questions/512819

  •  21-08-2019
  •  | 
  •  

Question

I'd like my GreaseMonkey script to run a function whenever elements with a specific class are inserted into a page. What's the most idiomatic way of doing that?

If it helps, it's a <select> element that is being inserted.

Was it helpful?

Solution

Can you do something with DOMNodeInserted?

OTHER TIPS

You can use DOMNodeInserted

This event is fired whether using DOM methods (appendChild et al) or innerHTML, and is fired in Opera as well, so it won't break portability of your userscript (if you care). Check out http://www.quirksmode.org/dom/events/tests/DOMtree.html

If you specify an argument for the function that is being called on DOMNodeInserted, that argument's 'target' property will be the node that a node is being inserted into. So, you could check the nodeType of that to see if it is the type you are looking for. DOMNode also has a className property you could check on. http://www.howtocreate.co.uk/tutorials/javascript/domstructure

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top