feat: 增加指定模型和查看可指定模型功能

This commit is contained in:
foxhui
2025-11-24 19:07:02 +08:00
Unverified
parent 55377de092
commit 4e37ff272e
8 changed files with 228 additions and 10 deletions
+31 -2
View File
@@ -399,9 +399,10 @@ async function initBrowser(config) {
* @param {object} context 浏览器上下文 {page, client, width, height}
* @param {string} prompt 提示词
* @param {string[]} imgPaths 图片路径数组
* @param {string|null} modelId 模型 UUID (可选)
* @returns {Promise<{image?: string, text?: string, error?: string}>}
*/
async function generateImage(context, prompt, imgPaths) {
async function generateImage(context, prompt, imgPaths, modelId) {
const { page, client, width, height } = context;
const textareaSelector = 'textarea';
@@ -417,7 +418,6 @@ async function generateImage(context, prompt, imgPaths) {
// 2. 粘贴图片
if (imgPaths && imgPaths.length > 0) {
await pasteImages(page, textareaSelector, imgPaths);
} else {
// 如果没有图片,也点击一下输入框获取焦点
await safeClick(page, textareaSelector);
}
@@ -427,6 +427,35 @@ async function generateImage(context, prompt, imgPaths) {
await humanType(page, textareaSelector, prompt);
await sleep(800, 1500);
// --- 注入 Fetch 拦截器 ---
if (modelId) {
await page.evaluate((targetModelId) => {
const originalFetch = window.fetch;
window.fetch = async (...args) => {
let [resource, config] = args;
// 简单判断 URL
const url = resource instanceof Request ? resource.url : resource.toString();
if (url.includes('/nextjs-api/stream/') && config && config.method === 'POST' && config.body) {
try {
const data = JSON.parse(config.body);
console.log(`[Browser] 正在拦截请求。原始 modelAId: ${data.modelAId}`);
// 修改 modelAId
data.modelAId = targetModelId;
config.body = JSON.stringify(data);
console.log(`[Browser] 请求已修改。新 modelAId: ${data.modelAId}`);
} catch (e) {
console.error('[Browser] 拦截失败:', e);
}
}
return originalFetch.apply(window, args);
};
}, modelId);
console.log(`>>> [Test] 已注入 Fetch 拦截器,目标模型: ${modelId}`);
}
// 4. 发送
const btnSelector = 'button[type="submit"]';
await safeClick(page, btnSelector);