Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

agent-open-cli-http-util

通过 OpenCLI Browser Bridge 读取浏览器现有登录态,并把匹配请求 URL 的 Cookie 注入标准 fetch 请求。

环境要求

  • Node.js 20 或更高版本
  • 已安装并运行 OpenCLI Browser Bridge
  • 对应 Chrome Profile 已登录目标站点

安装

npm install agent-open-cli-http-util

单次请求

import { openCliFetch } from 'agent-open-cli-http-util';

const response = await openCliFetch('https://example.com/api/profile', {
  headers: {
    accept: 'application/json',
  },
});

console.log(response.status, await response.json());

复用客户端

import { createOpenCliHttpClient } from 'agent-open-cli-http-util';

const client = createOpenCliHttpClient({
  session: 'my-agent',
});

const response = await client.fetch('https://example.com/api/items', {
  method: 'POST',
  headers: {
    'content-type': 'application/json',
  },
  body: JSON.stringify({ name: 'demo' }),
});

登录页面与请求地址不同

默认使用请求 URL 查询 Cookie。Cookie 为空时,工具会在后台打开该 URL 并重试一次。可以为单次请求指定不同的 Cookie 查询地址和登录页面:

const response = await client.fetch('https://api.example.com/data', {}, {
  cookieUrl: 'https://login.example.com/',
  navigateUrl: 'https://login.example.com/sign-in',
});

如果不希望自动打开页面:

const client = createOpenCliHttpClient({
  autoNavigate: false,
});

FormData

const form = new FormData();
form.append('name', 'demo');
form.append('file', new Blob(['hello']), 'hello.txt');

await client.fetch('https://example.com/api/upload', {
  method: 'POST',
  body: form,
});

不要手工设置 multipart Content-Type,Node.js 会自动生成 boundary。

阶段耗时

onStage 只报告阶段名和耗时,不包含 Cookie:

const client = createOpenCliHttpClient({
  onStage: ({ name, durationMs }) => {
    console.error(`${name}: ${durationMs} ms`);
  },
});

可能的阶段为 loadOpenClireadCookiesnavigatereadCookiesRetryrequest

企业 CA

访问使用企业证书的服务时,由调用方使用系统 CA 启动 Node.js:

node --use-system-ca app.mjs

这个包不会重启宿主进程,也不会关闭 TLS 校验。

安全约束

  • 只接受绝对 HTTP/HTTPS URL。
  • Cookie 仅注入请求,不通过 API、日志或错误信息返回。
  • Cookie 的域名、路径和安全属性由 Browser Bridge 按 cookieUrl 筛选。
  • 跨域重定向遵循 Node.js 原生 fetch 行为。

About

通过 OpenCLI Browser Bridge 读取浏览器现有登录态,并把匹配请求 URL 的 Cookie 注入标准 fetch 请求。

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages