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

Function getAllElementsByXpath

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

Source from the content-addressed store, hash-verified

562 }
563 }
564 function getAllElementsByXpath(xpath, contextNode, doc = document) {
565 contextNode = contextNode || doc;
566 const result = [];
567 try {
568 const query = doc.evaluate(xpath, contextNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
569 for (let i = 0; i < query.snapshotLength; i++) {
570 const node = query.snapshotItem(i);
571 // 如果是 Element 节点
572 if (node.nodeType === 1) result.push(node);
573 }
574 } catch (err) {
575 throw new Error(`无效 Xpath: ${xpath}`);
576 }
577 return result;
578 }
579 function getOneElements(selector, contextNode = undefined, doc = document) {
580 if (!selector) return;
581 contextNode = contextNode || doc;

Callers 1

getAllElementsFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected