1

I have the following userjs which is intended to remove anchor part of the URL but still jump to it:

// ==UserScript==
// @name PurgeAnchor
// @include *
// ==/UserScript==
(function() {
 var reg=/^(.*)\#(.*)$/;
 var match=reg.exec(location);
 function ObjectPosition(obj) {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
 }
 if(match) {
    document.location.replace(match[1]);
    sessionStorage.setItem("anchor", match[2]);
 }
 window.addEventListener("load", (function() {
         var anchor=sessionStorage.getItem("anchor");
         if(anchor!==null) {
             var obj=document.anchors.item(anchor);
             // var obj=document.getElementById(anchor);
             // if(obj===null) {
                 // obj=document.getElementsByName(anchor)[0];
             // }
             var pos=0;
             if(obj!==null) {
                 pos=ObjectPosition(obj);
                 window.scrollTo(0, pos);
             }
             sessionStorage.removeItem("anchor");
         }
     }), false);
 })()

The problem is that if I have an empty <a> tag with the name set, it fails to jump. obj.scrollIntoView() also fails. Opera-10.52_pre6306, Gentoo.

ZyX
  • 52,536
  • 7
  • 114
  • 135
  • At a glance, you want document.anchors.namedItem(anchor) instead of document.anchors.item(anchor) – Billiam Apr 14 '10 at 05:42
  • The problem is that if I put `alert(obj.outerHTML)` I get the right item, smth like ``. But in order to scroll I must prepend `obj.innerHTML="a";` or it will fail. If I have not emty `div` or `a`, it works fine. And, yes, your advice did not help. – ZyX Apr 14 '10 at 10:18
  • I suppose it may be an opera bug. – ZyX Apr 14 '10 at 10:19

1 Answers1

0

Ok, it took a while, but I think I finally got it:

(function detectSaveAndRedirect() {
            window.addEventListener('load', function() {
                // If the URL already has a hash, remove it.
                if (location.hash.length > 0) {
                    sessionStorage.setItem('anchor',location.hash.substr(1))
                    location.hash = ''
                }
                window.addEventListener('hashchange', function(event) {
                    if (sessionStorage.getItem('anchor')) {
                        event.preventDefault();
                        document.getElementById(sessionStorage.getItem('anchor')).scrollIntoView()
                        sessionStorage.removeItem('anchor')
                    }
                });
                // If links have hashes, remove them.
                document.getElementsByTagName('a').forEach(function(a) {
                    if (
                        (new URL(a.href).host == location.host) &&
                        (a.hash.length > 0)
                    ) {
                        a.addEventListener('click',function(event) {
                            a.removeAttribute('href')
                            a.scrollIntoView();
                        });
                    }
                });
            })
        })()