// ==UserScript==
// @name           YouTube HTML5 Redirect
// @description    Auto-redirects YouTube videos to NeoSmart's YouTube HTML5 viewer.
// @include        *
// ==/UserScript==

function isYouTubeURL(url) {
  return /^http:\/\/(www\.)?youtube\.com\/watch\?v=(.)+/.test(url);
}

// Stolen from the quietube autorefresh; converts links on pages you have open
// so that they link directly to the HTML5 version, not requiring an extra
// redirect. (Not sure why this doesn't work ON YouTube though.)
function convertLinks() {
  var links = document.evaluate("//a[@href]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  for (var i = 0; i < links.snapshotLength; i++) {
    var link = links.snapshotItem(i);
    if (isYouTubeURL(link.href)) {
      link.href = "http://neosmart.net/YouTube5/index.php?url=" + link.href;
    }
  }
}

if (isYouTubeURL(document.URL)) {
  window.location.href = "http://neosmart.net/YouTube5/index.php?url=" + window.location.href;
} else
  convertLinks();
