Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ opencli plugin uninstall my-tool
| [opencli-plugin-github-trending](https://github.com/ByteYue/opencli-plugin-github-trending) | YAML | GitHub Trending repositories |
| [opencli-plugin-hot-digest](https://github.com/ByteYue/opencli-plugin-hot-digest) | TS | Multi-platform trending aggregator |
| [opencli-plugin-juejin](https://github.com/Astro-Han/opencli-plugin-juejin) | YAML | 稀土掘金 (Juejin) hot articles |
| [opencli-plugin-gemini-web](https://github.com/AstaTus/opencli-plugin-gemini-web) | — | Web端Gemini交互 |

See [Plugins Guide](./docs/guide/plugins.md) for creating your own plugin.

Expand Down
1 change: 1 addition & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ opencli plugin uninstall my-tool # 卸载
| [opencli-plugin-github-trending](https://github.com/ByteYue/opencli-plugin-github-trending) | YAML | GitHub Trending 仓库 |
| [opencli-plugin-hot-digest](https://github.com/ByteYue/opencli-plugin-hot-digest) | TS | 多平台热榜聚合 |
| [opencli-plugin-juejin](https://github.com/Astro-Han/opencli-plugin-juejin) | YAML | 稀土掘金热门文章 |
| [opencli-plugin-gemini-web](https://github.com/AstaTus/opencli-plugin-gemini-web) | — | Web端Gemini交互 |

详见 [插件指南](./docs/zh/guide/plugins.md) 了解如何创建自己的插件。

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jackwener/opencli",
"version": "1.5.5",
"version": "1.5.6",
"publishConfig": {
"access": "public"
},
Expand Down
16 changes: 16 additions & 0 deletions src/browser/cdp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
waitForDomStableJs,
waitForCaptureJs,
waitForSelectorJs,
waitForStreamCaptureJs,
} from './dom-helpers.js';
import { isRecord, saveBase64ToFile } from '../utils.js';

Expand Down Expand Up @@ -347,6 +348,21 @@ class CDPPage implements IPage {
const maxMs = timeout * 1000;
await this.evaluate(waitForCaptureJs(maxMs));
}

async installStreamingInterceptor(pattern: string): Promise<void> {
const { generateStreamingInterceptorJs } = await import('../interceptor.js');
await this.evaluate(generateStreamingInterceptorJs(JSON.stringify(pattern)));
}

async getStreamedResponses(): Promise<{ text: string; events: any[]; done: boolean; errors: any[] }> {
const { generateReadStreamJs } = await import('../interceptor.js');
return (await this.evaluate(generateReadStreamJs())) as { text: string; events: any[]; done: boolean; errors: any[] };
}

async waitForStreamCapture(timeout: number = 30, opts?: { minChars?: number; waitForDone?: boolean }): Promise<void> {
const maxMs = timeout * 1000;
await this.evaluate(waitForStreamCaptureJs(maxMs, opts));
}
}

function isCookie(value: unknown): value is BrowserCookie {
Expand Down
3 changes: 2 additions & 1 deletion src/browser/daemon-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export async function isExtensionConnected(): Promise<boolean> {
export async function sendCommand(
action: DaemonCommand['action'],
params: Omit<DaemonCommand, 'id' | 'action'> = {},
timeoutMs: number = 30000,
): Promise<unknown> {
const maxRetries = 4;

Expand All @@ -94,7 +95,7 @@ export async function sendCommand(
const command: DaemonCommand = { id, action, ...params };
try {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 30000);
const timer = setTimeout(() => controller.abort(), timeoutMs);

const res = await fetch(`${DAEMON_URL}/command`, {
method: 'POST',
Expand Down
29 changes: 29 additions & 0 deletions src/browser/dom-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,35 @@ export function waitForCaptureJs(maxMs: number): string {
`;
}

/**
* Generate JS to wait until the streaming interceptor has captured data.
* Polls window.__opencli_stream_text length. Optionally waits for stream completion.
* 50ms interval, rejects after maxMs.
*/
export function waitForStreamCaptureJs(
maxMs: number,
opts: { minChars?: number; waitForDone?: boolean; prefix?: string } = {},
): string {
const minChars = opts.minChars ?? 1;
const waitForDone = opts.waitForDone ?? false;
const prefix = opts.prefix ?? '__opencli_stream';
return `
new Promise((resolve, reject) => {
const deadline = Date.now() + ${maxMs};
const check = () => {
const text = window.${prefix}_text || '';
const done = window.${prefix}_done || false;
if (text.length >= ${minChars} && (${waitForDone} ? done : true)) {
return resolve('captured');
}
if (Date.now() > deadline) return reject(new Error('Stream capture timeout'));
setTimeout(check, 50);
};
check();
})
`;
}

/**
* Generate JS to wait until document.querySelector(selector) returns a match.
* Uses MutationObserver for near-instant resolution; falls back to reject after timeoutMs.
Expand Down
19 changes: 19 additions & 0 deletions src/browser/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
waitForTextJs,
waitForCaptureJs,
waitForSelectorJs,
waitForStreamCaptureJs,
scrollJs,
autoScrollJs,
networkRequestsJs,
Expand Down Expand Up @@ -346,6 +347,24 @@ export class Page implements IPage {
...this._cmdOpts(),
});
}

async installStreamingInterceptor(pattern: string): Promise<void> {
const { generateStreamingInterceptorJs } = await import('../interceptor.js');
await this.evaluate(generateStreamingInterceptorJs(JSON.stringify(pattern)));
}

async getStreamedResponses(): Promise<{ text: string; events: any[]; done: boolean; errors: any[] }> {
const { generateReadStreamJs } = await import('../interceptor.js');
return (await this.evaluate(generateReadStreamJs())) as { text: string; events: any[]; done: boolean; errors: any[] };
}

async waitForStreamCapture(timeout: number = 30, opts?: { minChars?: number; waitForDone?: boolean }): Promise<void> {
const maxMs = timeout * 1000;
await sendCommand('exec', {
code: waitForStreamCaptureJs(maxMs, opts),
...this._cmdOpts(),
}, maxMs + 10000); // HTTP timeout = browser timeout + 10s buffer
}
}

// (End of file)
3 changes: 3 additions & 0 deletions src/clis/xiaohongshu/comments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ function createPageMock(evaluateResult: any): IPage {
getCookies: vi.fn().mockResolvedValue([]),
screenshot: vi.fn().mockResolvedValue(''),
waitForCapture: vi.fn().mockResolvedValue(undefined),
installStreamingInterceptor: vi.fn().mockResolvedValue(undefined),
getStreamedResponses: vi.fn().mockResolvedValue({ text: '', events: [], done: false, errors: [] }),
waitForStreamCapture: vi.fn().mockResolvedValue(undefined),
};
}

Expand Down
3 changes: 3 additions & 0 deletions src/clis/xiaohongshu/creator-note-detail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ function createPageMock(evaluateResult: any): IPage {
getCookies: vi.fn().mockResolvedValue([]),
screenshot: vi.fn().mockResolvedValue(''),
waitForCapture: vi.fn().mockResolvedValue(undefined),
installStreamingInterceptor: vi.fn().mockResolvedValue(undefined),
getStreamedResponses: vi.fn().mockResolvedValue({ text: '', events: [], done: false, errors: [] }),
waitForStreamCapture: vi.fn().mockResolvedValue(undefined),
};
}

Expand Down
3 changes: 3 additions & 0 deletions src/clis/xiaohongshu/creator-notes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ function createPageMock(evaluateResult: any, interceptedRequests: any[] = []): I
getCookies: vi.fn().mockResolvedValue([]),
screenshot: vi.fn().mockResolvedValue(''),
waitForCapture: vi.fn().mockResolvedValue(undefined),
installStreamingInterceptor: vi.fn().mockResolvedValue(undefined),
getStreamedResponses: vi.fn().mockResolvedValue({ text: '', events: [], done: false, errors: [] }),
waitForStreamCapture: vi.fn().mockResolvedValue(undefined),
};
}

Expand Down
3 changes: 3 additions & 0 deletions src/clis/xiaohongshu/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ function createPageMock(evaluateResults: any[]): IPage {
getCookies: vi.fn().mockResolvedValue([]),
screenshot: vi.fn().mockResolvedValue(''),
waitForCapture: vi.fn().mockResolvedValue(undefined),
installStreamingInterceptor: vi.fn().mockResolvedValue(undefined),
getStreamedResponses: vi.fn().mockResolvedValue({ text: '', events: [], done: false, errors: [] }),
waitForStreamCapture: vi.fn().mockResolvedValue(undefined),
};
}

Expand Down
3 changes: 3 additions & 0 deletions src/clis/xiaohongshu/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ function createPageMock(evaluateResults: any[]): IPage {
getCookies: vi.fn().mockResolvedValue([]),
screenshot: vi.fn().mockResolvedValue(''),
waitForCapture: vi.fn().mockResolvedValue(undefined),
installStreamingInterceptor: vi.fn().mockResolvedValue(undefined),
getStreamedResponses: vi.fn().mockResolvedValue({ text: '', events: [], done: false, errors: [] }),
waitForStreamCapture: vi.fn().mockResolvedValue(undefined),
};
}

Expand Down
Loading