|
| 1 | +# XPath Selector |
| 2 | + |
| 3 | +**一个 XPath 选择器库,快速节点获取数据** |
| 4 | + |
| 5 | +<a href="https://github.com/xiaohuohumax/userscripts/tree/main/apps/libs/xpath-selector"> |
| 6 | + <img src="https://img.shields.io/badge/GITHUB-项目地址-brightgreen?style=for-the-badge&logo=github" alt="项目地址" /> |
| 7 | +</a> |
| 8 | +<a href="https://github.com/xiaohuohumax/userscripts/blob/main/LICENSE"> |
| 9 | + <img src="https://img.shields.io/badge/MIT-开源协议-orange?style=for-the-badge&logo=github" alt="开源协议" /> |
| 10 | +</a> |
| 11 | +<a href="https://github.com/xiaohuohumax/userscripts/blob/main/apps/libs/xpath-selector/CHANGELOG.md"> |
| 12 | + <img src="https://img.shields.io/badge/CHANGELOG-更新日志-blue?style=for-the-badge&logo=github" alt="更新日志" /> |
| 13 | +</a> |
| 14 | +<a href="https://github.com/xiaohuohumax/userscripts/issues"> |
| 15 | + <img src="https://img.shields.io/badge/issues-问题反馈-yellow?style=for-the-badge&logo=github" alt="问题反馈" /> |
| 16 | +</a> |
| 17 | + |
| 18 | +## 📖 使用方式 |
| 19 | + |
| 20 | +### ✍ 添加元数据 |
| 21 | + |
| 22 | +```typescript |
| 23 | +// @require https://**/xpath-selector.js?* |
| 24 | +``` |
| 25 | +### 📥 参数说明 |
| 26 | + |
| 27 | +**Options 参数说明:** |
| 28 | + |
| 29 | +| 参数名 | 类型 | 是否必填 | 默认值 | 说明 | |
| 30 | +| ------------ | ------ | -------- | ---------- | --------------------------------------------------------------------------------------------------------------------------------- | |
| 31 | +| `expression` | string | 是 | | 要获取的节点的 XPath 表达式 | |
| 32 | +| `returnType` | string | 是 | | 获取结果的类型,可选值:`string`、`strings`、`number`、`numbers`、`boolean`、`nodes`、`first-node`、`map`、`array`、`all-results` | |
| 33 | +| `node` | Node | 否 | `document` | 要搜索的节点 | |
| 34 | + |
| 35 | +### 📦 使用示例 |
| 36 | + |
| 37 | +**获取 title 节点的文本内容** |
| 38 | + |
| 39 | +```typescript |
| 40 | +const title = xpathSelector({ |
| 41 | + expression: '//title/text()', |
| 42 | + returnType: 'string' |
| 43 | +}) |
| 44 | +console.log(title) // hello world |
| 45 | +``` |
| 46 | + |
| 47 | +**获取所有 p 节点的文本内容** |
| 48 | + |
| 49 | +```typescript |
| 50 | +const pList = xpathSelector({ |
| 51 | + expression: '//p/text()', |
| 52 | + returnType: 'strings' |
| 53 | +}) |
| 54 | +console.log(pList) // ['hello', 'world'] |
| 55 | +``` |
| 56 | + |
| 57 | +**统计所有 a 节点的个数** |
| 58 | + |
| 59 | +```typescript |
| 60 | +const aCount = xpathSelector({ |
| 61 | + expression: 'count(//a)', |
| 62 | + returnType: 'number' |
| 63 | +}) |
| 64 | +console.log(aCount) // 2 |
| 65 | +``` |
| 66 | + |
| 67 | +**判断是否存在 section 节点** |
| 68 | + |
| 69 | +```typescript |
| 70 | +const hasSection = xpathSelector({ |
| 71 | + expression: 'boolean(//section)', |
| 72 | + returnType: 'boolean' |
| 73 | +}) |
| 74 | +console.log(hasSection) // true |
| 75 | +``` |
| 76 | + |
| 77 | +**获取全部的 a 节点** |
| 78 | + |
| 79 | +```typescript |
| 80 | +const aList = xpathSelector({ |
| 81 | + expression: '//a', |
| 82 | + returnType: 'nodes' |
| 83 | +}) |
| 84 | +console.log(aList) // [<a>hello</a>, <a>world</a>] |
| 85 | +``` |
| 86 | + |
| 87 | +**获取第一个 a 节点** |
| 88 | + |
| 89 | +```typescript |
| 90 | +const firstA = xpathSelector({ |
| 91 | + expression: '//a', |
| 92 | + returnType: 'first-node' |
| 93 | +}) |
| 94 | +console.log(firstA) // <a>hello</a> |
| 95 | +``` |
| 96 | + |
| 97 | +**获取 html 节点的全部属性** |
| 98 | + |
| 99 | +```typescript |
| 100 | +const htmlAttributes = xpathSelector({ |
| 101 | + expression: `map:merge( |
| 102 | + for $attr in //html/@* |
| 103 | + return map:entry(local-name($attr), string($attr)) |
| 104 | + )`, |
| 105 | + returnType: 'map' |
| 106 | +}) |
| 107 | +console.log(htmlAttributes) // {lang: "en", charset: "UTF-8"} |
| 108 | +``` |
| 109 | + |
| 110 | +## 🚨 免责声明 |
| 111 | + |
| 112 | +- 本脚本仅供学习交流使用 |
| 113 | +- 请勿用于任何商业用途 |
| 114 | +- 使用本脚本产生的任何后果由用户自行承担 |
| 115 | + |
| 116 | +## ♻ 其他说明 |
| 117 | + |
| 118 | +GreasyFork 或者 ScriptCat 回复不及时,问题反馈推荐直接在 Github 提 Issue。 |
| 119 | + |
| 120 | +**如果觉得本脚本对你有帮助,欢迎点个 ⭐ Star 支持一下!** |
0 commit comments