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?
我希望我的GreaseMonkey脚本在每个具有特定类的元素插入页面时运行一个函数。最常用的方法是什么?
If it helps, it's a <select>
element that is being inserted.
如果它有帮助,它是一个正在插入的
2 个解决方案
#2
You can use DOMNodeInserted
您可以使用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
无论是使用DOM方法(appendChild等)还是innerHTML,都会触发此事件,并且也会在Opera中触发,因此它不会破坏用户脚本的可移植性(如果您愿意)。查看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
如果为DOMNodeInserted上调用的函数指定参数,则该参数的“target”属性将是插入节点的节点。因此,您可以检查该节点的类型,以查看它是否是您要查找的类型。 DOMNode还有一个可以检查的className属性。 http://www.howtocreate.co.uk/tutorials/javascript/domstructure
#1
Can you do something with DOMNodeInserted?
你能用DOMNodeInserted做点什么吗?
#2
You can use DOMNodeInserted
您可以使用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
无论是使用DOM方法(appendChild等)还是innerHTML,都会触发此事件,并且也会在Opera中触发,因此它不会破坏用户脚本的可移植性(如果您愿意)。查看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
如果为DOMNodeInserted上调用的函数指定参数,则该参数的“target”属性将是插入节点的节点。因此,您可以检查该节点的类型,以查看它是否是您要查找的类型。 DOMNode还有一个可以检查的className属性。 http://www.howtocreate.co.uk/tutorials/javascript/domstructure