@agent-infra/browser-context

中文文档
0.2.2发布于 2026/01/30TypeScriptESM + CJS构建来源已验证近 30 天下载 2377

get browser context for AI Agent

原始使用文档
文档目录17 节
  1. Features
  2. Installation
  3. Usage
  4. Basic Content Extraction
  5. Manual Content Extraction
  6. HTML to Markdown Conversion
  7. Advanced HTML to Markdown Options
  8. API Reference
  9. extractContent(page: Page)
  10. extractWithDefuddle(html: string, url: string, options: Def…
  11. extractWithReadability(page: Page, options?)
  12. toMarkdown(html: string, options?: ToMarkdownOptions)
  13. Content Extraction Strategy
  14. Removed HTML Elements
  15. Browser Compatibility
  16. License
  17. Credits

Browser Context

A powerful web content extraction library that extracts clean, readable content from web pages and converts it to Markdown format. Built for browser automation and content processing workflows.

Features

  • Smart Content Extraction: Uses advanced algorithms (Defuddle + Mozilla Readability) to extract main content from web pages
  • HTML to Markdown Conversion: Clean conversion with support for GitHub Flavored Markdown (GFM)
  • Browser Integration: Works seamlessly with Puppeteer and other browser automation tools
  • Fallback Strategy: Automatically falls back to Readability when primary extraction fails
  • Customizable: Configurable tag removal and conversion options

Installation

pnpm install @agent-infra/browser-context

Usage

Basic Content Extraction

Extract clean content from a web page using Puppeteer:

import { extractContent } from '@agent-infra/browser-context';
import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com/article');

// Extract content as Markdown
const result = await extractContent(page);
console.log(result.title); // Article title
console.log(result.content); // Clean Markdown content

await browser.close();

Manual Content Extraction

Extract content from HTML strings:

import {
  extractWithDefuddle,
  extractWithReadability,
} from '@agent-infra/browser-context';

// Using Defuddle (primary method)
const result1 = await extractWithDefuddle(htmlString, url, {
  markdown: true,
});

// Using Readability (fallback method)
const result2 = await extractWithReadability(page, {
  markdown: true,
});

HTML to Markdown Conversion

Convert HTML content to Markdown:

import { toMarkdown } from '@agent-infra/browser-context';

const html = '<h1>Title</h1><p>Content with <strong>bold</strong> text</p>';
const markdown = toMarkdown(html, {
  gfmExtension: true, // Enable GitHub Flavored Markdown
  codeBlockStyle: 'fenced', // Use fenced code blocks
  headingStyle: 'atx', // Use # style headings
});

console.log(markdown);
// # Title
//
// Content with **bold** text

Advanced HTML to Markdown Options

import {
  toMarkdown,
  DEFAULT_TAGS_TO_REMOVE,
} from '@agent-infra/browser-context';

const options = {
  gfmExtension: true,
  codeBlockStyle: 'fenced' as const,
  headingStyle: 'atx' as const,
  emDelimiter: '*',
  strongDelimiter: '**',
  removeTags: [...DEFAULT_TAGS_TO_REMOVE, 'footer', 'nav'], // Remove additional tags
};

const markdown = toMarkdown(htmlContent, options);

API Reference

extractContent(page: Page)

Main extraction function that automatically tries Defuddle first, then falls back to Readability.

Parameters:

  • page: Puppeteer page instance

Returns:

  • Promise<{title: string, content: string}>: Extracted title and Markdown content

extractWithDefuddle(html: string, url: string, options: DefuddleOptions)

Extract content using the Defuddle library.

Parameters:

  • html: HTML content string
  • url: Page URL
  • options: Defuddle configuration options

extractWithReadability(page: Page, options?)

Extract content using Mozilla's Readability algorithm.

Parameters:

  • page: Puppeteer page instance
  • options.markdown: Whether to convert to Markdown (default: false)

toMarkdown(html: string, options?: ToMarkdownOptions)

Convert HTML to Markdown format.

Parameters:

  • html: HTML content string
  • options: Conversion options

ToMarkdownOptions:

  • gfmExtension: Enable GitHub Flavored Markdown (default: true)
  • removeTags: Array of HTML tags to remove
  • codeBlockStyle: 'indented' | 'fenced'
  • headingStyle: 'setext' | 'atx'
  • emDelimiter: Emphasis delimiter
  • strongDelimiter: Strong emphasis delimiter

Content Extraction Strategy

The library uses a smart two-tier extraction strategy:

  1. Primary: Defuddle library for accurate content extraction
  2. Fallback: Mozilla Readability algorithm when Defuddle fails

This ensures maximum compatibility and extraction success across different website structures.

Removed HTML Elements

By default, the following HTML elements are removed during Markdown conversion:

  • script, style, link, head
  • iframe, video, audio, canvas
  • object, embed, noscript
  • aside, dialog

You can customize this list using the removeTags option.

Browser Compatibility

This library is designed to work with:

  • Puppeteer
  • Playwright
  • Any browser automation tool that provides a Page-like interface

License

Apache License 2.0.

Credits

Special thanks to the open source projects that inspired this toolkit:

  • readability - A standalone version of the readability lib

版本与发布记录

@agent-infra/browser-context 共发布 12 个版本,首次发布于 2025/07/25;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.2latest2026/01/309 个月前
0.2.12025/11/1011 个月前
0.2.02025/11/1011 个月前
0.2.0-alpha.5beta2025/11/061 年前
0.2.0-alpha.42025/11/061 年前
0.2.0-alpha.32025/11/061 年前

关于 @agent-infra/browser-context 的常见问题

@agent-infra/browser-context 是什么?

get browser context for AI Agent

如何安装 @agent-infra/browser-context?

在项目目录运行 npm install @agent-infra/browser-context,或使用 pnpm add / yarn add / bun add。

@agent-infra/browser-context 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@agent-infra/browser-context 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@agent-infra/browser-context 可以商用吗?

@agent-infra/browser-context 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@agent-infra/browser-context 的下载量有多少?

根据 npm 官方下载统计,@agent-infra/browser-context 在 2026/10/05 – 2026/11/03 这 30 天内被下载 2,377 次,日均约 79 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@agent-infra/browser-context 还在维护吗?

最新版本 0.2.2 发布于 2026/01/30(9 个月前);至今共发布 12 个版本,其中过去 12 个月发布了 3 个;首次发布于 2025/07/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。