// ==UserScript==
// @name           findhash
// @namespace      http://eldar.cz/pub/firefox/
// @description    tries to find(url#hash) if no matching ID or anchor is present
// @include        *
// ==/UserScript==

(
function ( ) {
 if ( !window.find
   || !window.location.hash
   || window.location.hash.length < 2
  ) {
  return false // unable to search or nothing to look for
 }
 var h = window.location.hash.substring(1);
 if ( document.getElementById(h) ) {
  return false // ID present
 }
 for ( var i = 0; i < document.anchors.length; i++ ) {
  if ( document.anchors[i].name == h ) {
   return false // anchor present
  }
 }
 if ( !window.addEventListener ) {
  window.find(decodeURI(h));
 } else {
  window.addEventListener(
   // type:
    'load'
   ,
   // listener:
    function(){
     window.find(
      // aString:
       decodeURI(h)
      ,
      // aCaseSensitive:
       false
      ,
      // aBackwards:
       false
      ,
      // aWrapAround:
       false
      ,
      // aWholeWord:
       false
      ,
      // aSearchInFrames:
       false
      ,
      // aShowDialog:
      //  must be also true while aSearchInFrames is enabled
      //  otherwise it will cause 'Error: Access to restricted URI denied'
       false
     );
    }
   ,
   // useCapture:
    false
   )
  ;
 }
}
)();