Created
April 29, 2009 20:46
-
-
Save ddollar/104043 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getElementXPath(elt) | |
{ | |
var path = ""; | |
for (; elt && elt.nodeType == 1; elt = elt.parentNode) | |
{ | |
idx = getElementIdx(elt); | |
xname = elt.tagName; | |
if (idx > 1) xname += "[" + idx + "]"; | |
path = "/" + xname + path; | |
} | |
return path.toLowerCase(); | |
} | |
function getElementIdx(elt) | |
{ | |
var count = 1; | |
for (var sib = elt.previousSibling; sib ; sib = sib.previousSibling) | |
{ | |
if(sib.nodeType == 1 && sib.tagName == elt.tagName) count++ | |
} | |
return count; | |
} | |
function elementByXpath(xpath) | |
{ | |
var result = document.evaluate(xpath, document, null, 7, null); | |
return(result.snapshotItem(0)); | |
} | |
function elementTop(element) | |
{ | |
var top = element.offsetTop; | |
while (element = element.offsetParent) { | |
top += element.offsetTop; | |
} | |
return(top); | |
} | |
var xpath = getElementXPath(window.getSelection().anchorNode.parentNode); | |
var node = elementByXpath(xpath); | |
var top = elementTop(node); | |
console.log('XPATH: ' + xpath); | |
console.log('NODE: ' + node); | |
console.log('TOP: ' + top); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment