[Greasemonkey] Find a Selection's Parent Object

Roderick McGuire mcguire at telerama.com
Wed Dec 14 13:12:48 EST 2005


Tom Clancy wrote:
> I'm guessing this is a solved problem, but I can't turn up anything in a
> search: what's the best (or any) way to find the parent element of a
> highlighted selection on the page?

  var s = window.getSelection();
  // A selection may have its .anchorNode after its .focusNode
  // if it was moused right to left.
  // A range always has its .startContainer textually
  // before its .endContainer.

  var r = s.getRangeAt(0);     // using FF's ^F search & click Highlight
                               // gives multiple ranges
   var rs = r.startContainer;  // a #text node
   var rso = r.startOffset;
   var re = r.endContainer;    // a #text node
   var reo = r.endOffset



More information about the Greasemonkey mailing list