document.links - check for new additions?

in Script development
Subscribe to document.links - check for new additions? 3 posts, 2 voices



mike cupcake Scriptwright
FirefoxWindows

I'd like to have a function only run when document.links has new items since the function last ran.

I'm thinking I could loop through document.links and push each item into an array, then compare to that next time. Guess there's a simpler way to do it though, could anyone point me in the right direction?

 
Monkey2000 User
FirefoxWindows

How about adding an attribute to the links (e.g., seenit="seenit") and then you can look for new links with:

var newlinks = document.querySelectorAll('a:not([seenit])');
if (newlinks.length > 0){
  // Do stuff
}

 
mike cupcake Scriptwright
FirefoxMacintosh

Thanks that should do it perfectly. Also I now feel silly as I'm using an attribute in similar way to flag that certain links have been changed but didn't think of adapting the trick to do this too :-)