通过 OpenCLI Browser Bridge 读取浏览器现有登录态,并把匹配请求 URL 的 Cookie 注入标准 fetch 请求。
- Node.js 20 或更高版本
- 已安装并运行 OpenCLI Browser Bridge
- 对应 Chrome Profile 已登录目标站点
npm install agent-open-cli-http-utilimport { 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,
});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`);
},
});可能的阶段为 loadOpenCli、readCookies、navigate、readCookiesRetry 和 request。
访问使用企业证书的服务时,由调用方使用系统 CA 启动 Node.js:
node --use-system-ca app.mjs这个包不会重启宿主进程,也不会关闭 TLS 校验。
- 只接受绝对 HTTP/HTTPS URL。
- Cookie 仅注入请求,不通过 API、日志或错误信息返回。
- Cookie 的域名、路径和安全属性由 Browser Bridge 按
cookieUrl筛选。 - 跨域重定向遵循 Node.js 原生
fetch行为。