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

Function getAllElementsByXpath

52pojie-Enhanced.user.js:363–377  ·  view source on GitHub ↗
(xpath, contextNode, doc = document)

Source from the content-addressed store, hash-verified

361 }
362 }
363 function getAllElementsByXpath(xpath, contextNode, doc = document) {
364 contextNode = contextNode || doc;
365 const result = [];
366 try {
367 const query = doc.evaluate(xpath, contextNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
368 for (let i = 0; i < query.snapshotLength; i++) {
369 const node = query.snapshotItem(i);
370 // 如果是 Element 节点
371 if (node.nodeType === 1) result.push(node);
372 }
373 } catch (err) {
374 throw new Error(`无效 Xpath: ${xpath}`);
375 }
376 return result;
377 }
378 function getAllElements(selector, contextNode = undefined, doc = document, win = window, _cplink = undefined) {
379 if (!selector) return [];
380 contextNode = contextNode || doc;

Callers 1

getAllElementsFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected