Skip to content

Instantly share code, notes, and snippets.

@os0x
Created July 24, 2008 03:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save os0x/2040 to your computer and use it in GitHub Desktop.
Save os0x/2040 to your computer and use it in GitHub Desktop.
simple $X
// 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