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

Function getElementByXpath

3dm-Enhanced.user.js:368–377  ·  view source on GitHub ↗
(xpath, contextNode, doc = document)

Source from the content-addressed store, hash-verified

366 return [].slice.call(contextNode.querySelectorAll(css));
367 }
368 function getElementByXpath(xpath, contextNode, doc = document) {
369 contextNode = contextNode || doc;
370 try {
371 const result = doc.evaluate(xpath, contextNode, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
372 // 应该总是返回一个元素节点
373 return result.singleNodeValue && result.singleNodeValue.nodeType === 1 && result.singleNodeValue;
374 } catch (err) {
375 throw new Error(`Invalid xpath: ${xpath}`);
376 }
377 }
378 function getAllElementsByXpath(xpath, contextNode, doc = document) {
379 contextNode = contextNode || doc;
380 const result = [];

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected