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

Function getElementByXpath

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

Source from the content-addressed store, hash-verified

432 return [].slice.call(contextNode.querySelectorAll(css));
433 }
434 function getElementByXpath(xpath, contextNode, doc = document) {
435 contextNode = contextNode || doc;
436 try {
437 const result = doc.evaluate(xpath, contextNode, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
438 // 应该总是返回一个元素节点
439 return result.singleNodeValue && result.singleNodeValue.nodeType === 1 && result.singleNodeValue;
440 } catch (err) {
441 throw new Error(`Invalid xpath: ${xpath}`);
442 }
443 }
444 function getAllElementsByXpath(xpath, contextNode, doc = document) {
445 contextNode = contextNode || doc;
446 const result = [];

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected