JavaScript Ninja Techniques - Extract Unique List Of Domain Names From Anchor Tags

Posted at

var unique = {};'a:not([href*="' + document.location.hostname + '"])'), function(element) {
  if(! unique.hasOwnProperty(element.hostname))
    unique[element.hostname] = '';

unique = Object.keys(unique); //associative array to flat array (but now its with unique values..)
unique.sort(); // a-z sort
console.log(JSON.stringify(unique)); //just for fun, text only..

Notice that the querySelectorAll's query is quite complex (and lovely!),
the query takes the current page's domain from the location.hostname, and use the ::not(...) syntax, to to "select anchor nodes, that would not have sequence-match for the same domain".
a little prototyping trick will allow you to iterate over all the nodes (NodeList.. which is not quite an array...), works nicely though!