MCPcopy Create free account
hub / github.com/XIU2/UserScript / getElementByXpath

Function getElementByXpath

V2ex-Enhanced.user.js:554–563  ·  view source on GitHub ↗
(xpath, contextNode, doc = document)

Source from the content-addressed store, hash-verified

552 return [].slice.call(contextNode.querySelectorAll(css));
553 }
554 function getElementByXpath(xpath, contextNode, doc = document) {
555 contextNode = contextNode || doc;
556 try {
557 const result = doc.evaluate(xpath, contextNode, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
558 // 应该总是返回一个元素节点
559 return result.singleNodeValue && result.singleNodeValue.nodeType === 1 && result.singleNodeValue;
560 } catch (err) {
561 throw new Error(`Invalid xpath: ${xpath}`);
562 }
563 }
564 function getAllElementsByXpath(xpath, contextNode, doc = document) {
565 contextNode = contextNode || doc;
566 const result = [];

Callers 1

getOneElementsFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected