Positioning elements inserted by a user script

I needed to insert an anchor element before an other element in my Select element filter user script because I wanted the anchor to fit in the page's existing tab order. Also, I didn't want the anchor to push the other element to the right, so it needed to be positioned absolutely. You might know that you need to position absolute positioned element relative to the root elements or the first non-static positioned parent node. Here's the javascript code I used to determine the anchor's position:

var posY = 0;
var posX = 0;
var currOffsetParent = elementToInsertBefore;
do {
  if (
    document.defaultView.getComputedStyle(currOffsetParent, null)
    .getPropertyValue('position') == 'static'
  ) {
    posY += currOffsetParent.offsetTop;
    posX += currOffsetParent.offsetLeft;
  else break;
} while (currOffsetParent = currOffsetParent.offsetParent);

No comments: