npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@xiaohuohumax/xpath-selector

v1.1.3

Published

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

Readme

XPath Selector

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

📥 参数说明

通用函数 xpathSelector(options: Options)

Options 参数说明:

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

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

快捷函数 xpathSelector.select[returnType](expression: string, node?: Node)

Options 参数说明:

| 参数名 | 类型 | 是否必填 | 默认值 | 说明 | | ------------ | ------ | -------- | ---------- | ------------------------------------------------ | | returnType | string | 是 | | 函数名,可选值见上方 Options.returnType 参数说明 | | expression | string | 是 | | 要获取的节点的 XPath 表达式 | | node | Node | 否 | document | 要搜索的节点 |

const title = xpathSelector.selectString('//title/text()')
console.log(title) // hello world

📖 使用方式

方式一:直接引入库文件

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

(function () {
  'use strict'
  const title = xpathSelector.selectString('//title/text()')
  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 button = xpathSelector.selectFirstNode<HTMLButtonElement>('//button')
console.log(button) // Output: <button>Click Me</button>
  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'),
        },
      },
    }),
  ],
})

📦 使用示例

<!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.selectString('//title/text()')
console.log(title) // hello world

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

const pList = xpathSelector.selectStrings('//p/text()')
console.log(pList) // ['hello', 'world']

统计所有 a 节点的个数

const aCount = xpathSelector.selectNumber('count(//a)')
console.log(aCount) // 2

判断是否存在 section 节点

const hasSection = xpathSelector.selectBoolean('boolean(//section)')
console.log(hasSection) // true

获取全部的 a 节点

const aList = xpathSelector.selectNodes('//a')
console.log(aList) // [<a>hello</a>, <a>world</a>]

获取第一个 a 节点

const firstA = xpathSelector.selectFirstNode('//a')
console.log(firstA) // <a>hello</a>

获取 html 节点的全部属性

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

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

const customHtmlTitle = xpathSelector.selectString(
  '//title/text()',
  new DOMParser().parseFromString('<html><title>Hello</title></html>', 'text/html'),
)
console.log(customHtmlTitle) // Hello

🧩 依赖项目

🚨 免责声明

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

♻ 其他说明

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

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