MCPcopy Index your code
hub / github.com/hoothin/UserScripts / getElementByXpath

Function getElementByXpath

Pagetual/pagetual.user.js:4532–4554  ·  view source on GitHub ↗
(xpath, doc, contextNode)

Source from the content-addressed store, hash-verified

4530 }
4531
4532 function getElementByXpath(xpath, doc, contextNode) {
4533 if (doc && doc.ownerDocument) doc = doc.ownerDocument;
4534 doc = (doc && doc.evaluate) ? doc : document;
4535 contextNode = contextNode || doc;
4536 try {
4537 let xpathNode = (s, d, n) => {
4538 let result = d.evaluate(s, n, null, XPathResult.ANY_UNORDERED_NODE_TYPE, null);
4539 return result.singleNodeValue && result.singleNodeValue.nodeType === 1 && result.singleNodeValue;
4540 };
4541 let selSplit = xpath.split(" =>> ");
4542 if (selSplit.length === 2) {
4543 let ele = xpathNode(selSplit[0], doc, contextNode);
4544 if (ele && ele.shadowRoot) {
4545 return xpathNode(selSplit[1], ele.shadowRoot, ele.shadowRoot);
4546 }
4547 } else {
4548 return xpathNode(xpath, doc, contextNode);
4549 }
4550 } catch (err) {
4551 debug(`Invalid xpath: ${xpath}`);
4552 }
4553 return null;
4554 }
4555
4556 function getAllElementsByXpath(xpath, contextNode, doc) {
4557 if (doc && doc.ownerDocument) doc = doc.ownerDocument;

Callers 1

getElementFunction · 0.70

Calls 2

xpathNodeFunction · 0.85
debugFunction · 0.85

Tested by

no test coverage detected