is it possible to addEventListener to a table to check for new info in that table?
i have this 开发者_如何学运维greasemonkey/user.js script written for some website. that website has a table which outputs info every now and then; it does so by adding a new tablerow with the info.
at the moment i just check in a timed loop if there is something new in there. But can i do this different? could i add a listener that will fire whenever a new table row with info appears? if so how?
EDIT: the table i wanna check on consists of only table rows. each time new info is presented a new row is added with some text inside it. the table seems to have a maximum length. so if it's a full table the oldest tr is removed. Think of it as a chatbox, it's quite similar.
my current timed loop checks for untagged table rows in that table, gets the info out of them and tags them. so it checks only untagged ones.
and example of one of those tablerows:
<tr class="tagged"> <td align="left" style="vertical-align: top;"> <div class="someClass" style="width: 100%;"> some info i'm interested in </div> </td> </tr>
There is Mutation Events, maybe you can use DOMNodeInserted event as there's new rows inserted.
@wong2 Good call. This works for me (outside of a userscript) in IE9, FF4, Chrome 6:
myTable = document.getElementsByTagName('table')[0];
if (document.implementation.hasFeature('MutationEvents','2.0')
|| window.MutationEvent) {
myTable.addEventListener('DOMNodeInserted', function(e) {
console.log('data added');
}, false);
}
精彩评论