// ==UserScript==

// @name            Entire page validator
// @author          Bruno Torres <brunotorres.net/>
// @namespace       http://brunotorres.net/greasemonkey/
// @description     Get all internal links on a page and checks them for validity
// @include         *

// ==/UserScript==

(function() {
  function validate(){
    alert('validating');
    var a = document.getElementsByTagName('a');
    for (i=0; i<a.length; i++){
      var link = a[i];
      var address = window.location.href.split('/');
      var domain = address[2];
      if (link.href.match(domain)){
        validateLink(link.href);
      }
    }
  }
  function validateLink(link){
    GM_xmlhttpRequest({
        method: 'GET',
        url: 'http://brunotorres.net/unsorted/valid.php?url='+link,
        headers: {
            'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey'
        },
        onload: function(responseDetails) {
          if (responseDetails.responseText == 'valid'){
            document.write('<h2>'+link+'valid</h2>');
          } else {
            document.write('<h2>'+link+'invalid</h2>');
          }
        }
    });
  }
  var body = document.getElementsByTagName('body')[0];
  var validate = document.createElement('a');
  validate.href='';
  var text = document.createTextNode('validate');
  validate.appendChild(text);
  body.appendChild(validate);
  alert(validate.href);
  validate.onclick = function(){
    alert('a');
    validate();
    return false;
  }
})();
