Created
July 24, 2008 03:59
-
-
Save os0x/2040 to your computer and use it in GitHub Desktop.
simple $X
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
// Simple $X | |
// Thx! $X from http://coderepos.org/share/browser/lang/javascript/userscripts/jautopagerize.user.js | |
// http://subtech.g.hatena.ne.jp/cho45/20071114/1195017746 | |
function $X (exp, context) { | |
context || (context = document); | |
var exp = (context.ownerDocument || context).createExpression(exp, function (prefix) { | |
return (document.createNSResolver((context.ownerDocument == null | |
? context : context.ownerDocument).documentElement) | |
.lookupNamespaceURI(prefix) || document.documentElement.namespaceURI); | |
}); | |
var result = exp.evaluate(context, XPathResult.ANY_TYPE, null); | |
switch (result.resultType) { | |
case XPathResult.STRING_TYPE : return result.stringValue; | |
case XPathResult.NUMBER_TYPE : return result.numberValue; | |
case XPathResult.BOOLEAN_TYPE: return result.booleanValue; | |
case XPathResult.UNORDERED_NODE_ITERATOR_TYPE: { | |
// not ensure the order. | |
var ret = []; | |
var i = null; | |
while (i = result.iterateNext()) { | |
ret.push(i); | |
} | |
return ret; | |
} | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment