xpath-selector

一个 XPath 选择器库,快速获取节点数据

이 스크립트는 직접 설치하는 용도가 아닙니다. 다른 스크립트에서 메타 지시문 // @require https://update.greasyfork.org/scripts/559142/1716151/xpath-selector.js을(를) 사용하여 포함하는 라이브러리입니다.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

You will need to install an extension such as Tampermonkey to install this script.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

작성자
xiaohuohumax
버전
1.0.5
생성일
2025-12-16
갱신일
2025-12-18
크기
478KB
라이선스
MIT

XPath Selector

一个 XPath 选择器库,快速获取节点数据

项目地址 开源协议 更新日志 问题反馈

📥 参数说明

Options 参数说明:

参数名 类型 是否必填 默认值 说明
expression string 要获取的节点的 XPath 表达式
returnType string 获取结果的类型,可选值:stringstringsnumbernumbersbooleannodesfirst-nodemaparrayall-results
node Node document 要搜索的节点

📦 使用示例

<!DOCTYPE html>
<html lang="en" charset="UTF-8">
<head>
    <title>hello world</title>
</head>
<body>
    <p>hello</p>
    <p>world</p>
    <a href="#">hello</a>
    <a href="#">world</a>
    <section>
        <!-- section内容 -->
    </section>
</body>
</html>

获取 title 节点的文本内容

const title = xpathSelector({
  expression: '//title/text()',
  returnType: 'string'
})
console.log(title) // hello world

获取所有 p 节点的文本内容

const pList = xpathSelector({
  expression: '//p/text()',
  returnType: 'strings'
})
console.log(pList) // ['hello', 'world']

统计所有 a 节点的个数

const aCount = xpathSelector({
  expression: 'count(//a)',
  returnType: 'number'
})
console.log(aCount) // 2

判断是否存在 section 节点

const hasSection = xpathSelector({
  expression: 'boolean(//section)',
  returnType: 'boolean'
})
console.log(hasSection) // true

获取全部的 a 节点

const aList = xpathSelector({
  expression: '//a',
  returnType: 'nodes'
})
console.log(aList) // [<a>hello</a>, <a>world</a>]

获取第一个 a 节点

const firstA = xpathSelector({
  expression: '//a',
  returnType: 'first-node'
})
console.log(firstA) // <a>hello</a>

获取 html 节点的全部属性

const htmlAttributes = xpathSelector({
  expression: `map:merge(
    for $attr in //html/@*
    return map:entry(local-name($attr), string($attr))
  )`,
  returnType: 'map'
})
console.log(htmlAttributes) // {lang: "en", charset: "UTF-8"}

获取自定义 html 节点的 title 节点的文本内容

const customHtmlTitle = xpathSelector({
  expression: '//title/text()',
  node: new DOMParser().parseFromString('<html><title>Hello</title></html>', 'text/html'),
  returnType: 'string',
})
console.log(customHtmlTitle) // Hello

📖 使用方式

方式一:直接引入库文件

// ==UserScript==
// @require      https://**/xpath-selector.js?*
// ==/UserScript==

(function () {
  'use strict'
  const title = xpathSelector({
    expression: '//title/text()',
    returnType: 'string'
  })
  console.log(title) // hello world
})()

方式二:vite + vite-plugin-monkey [推荐]

  1. 初始化项目
npm create monkey
  1. 安装 xpath-selector 依赖
npm i @xiaohuohumax/xpath-selector
  1. 在 main.ts 中使用 xpath-selector
import xpathSelector from '@xiaohuohumax/xpath-selector'

const title = xpathSelector({
  expression: '//title/text()',
  returnType: 'string'
})

console.log(title) // Output: "Test Page"
  1. 修改 vite.config.ts 排除 xpath-selector 依赖
import { defineConfig } from 'vite'
import monkey, { cdn } from 'vite-plugin-monkey'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    monkey({
      build: {
        externalGlobals: {
          '@xiaohuohumax/xpath-selector': cdn.jsdelivr('xpathSelector', 'dist/index.lib.js'),
        },
      },
    }),
  ],
})

🧩 依赖项目

🚨 免责声明

  • 本脚本仅供学习交流使用
  • 请勿用于任何商业用途
  • 使用本脚本产生的任何后果由用户自行承担

♻ 其他说明

GreasyFork 或者 ScriptCat 回复不及时,问题反馈推荐直接在 Github 提 Issue。

如果觉得本脚本对你有帮助,欢迎点个 ⭐ Star 支持一下!