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

Function getAllElementsByXpath

Zhiyoo-Enhanced.user.js:444–458  ·  view source on GitHub ↗
(xpath, contextNode, doc = document)

Source from the content-addressed store, hash-verified

442 }
443 }
444 function getAllElementsByXpath(xpath, contextNode, doc = document) {
445 contextNode = contextNode || doc;
446 const result = [];
447 try {
448 const query = doc.evaluate(xpath, contextNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
449 for (let i = 0; i < query.snapshotLength; i++) {
450 const node = query.snapshotItem(i);
451 // 如果是 Element 节点
452 if (node.nodeType === 1) result.push(node);
453 }
454 } catch (err) {
455 throw new Error(`无效 Xpath: ${xpath}`);
456 }
457 return result;
458 }
459 function getAllElements(selector, contextNode = undefined, doc = document, win = window, _cplink = undefined) {
460 if (!selector) return [];
461 contextNode = contextNode || doc;

Callers 1

getAllElementsFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected