Skip to content

Instantly share code, notes, and snippets.

@remarkablemark
Last active December 6, 2022 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remarkablemark/376477ed8386682cec8644cff5cbb5c9 to your computer and use it in GitHub Desktop.
Save remarkablemark/376477ed8386682cec8644cff5cbb5c9 to your computer and use it in GitHub Desktop.
/**
* Evaluate XPath expression.
*
* @param {string} xpathExpression - XPath expression.
* @param {HTMLElement} [contextNode=document] - Context node for the query.
* @returns {HTMLElement[]}
*/
function evaluateXPath(xpathExpression, contextNode = document) {
const result = document.evaluate(
xpathExpression,
contextNode,
null,
XPathResult.ANY_TYPE,
null
);
let element;
const elements = [];
while ((element = result.iterateNext())) {
elements.push(element);
}
return elements;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment