| 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; |