add delayed sending switch
This commit is contained in:
+47
-31
@@ -806,17 +806,23 @@ class YouProvider {
|
|||||||
}]));
|
}]));
|
||||||
req_param.append("chat", JSON.stringify(userMessage));
|
req_param.append("chat", JSON.stringify(userMessage));
|
||||||
const url = "https://you.com/api/streamingSearch?" + req_param.toString();
|
const url = "https://you.com/api/streamingSearch?" + req_param.toString();
|
||||||
await page.goto(`https://you.com/search?q=&fromSearchBar=true&tbm=youchat&chatMode=custom`, {waitUntil: "domcontentloaded"});
|
const enableDelayLogic = process.env.ENABLE_DELAY_LOGIC === 'true'; // 是否启用延迟逻辑
|
||||||
|
|
||||||
|
if (enableDelayLogic) {
|
||||||
|
await page.goto(`https://you.com/search?q=&fromSearchBar=true&tbm=youchat&chatMode=custom`, {waitUntil: "domcontentloaded"});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async function establishConnection(session, page, emitter, traceId) {
|
async function establishConnection(session, page, emitter, traceId) {
|
||||||
try {
|
try {
|
||||||
await session.page.goto("https://you.com", {waitUntil: 'domcontentloaded'});
|
await session.page.goto("https://you.com", {waitUntil: 'domcontentloaded'});
|
||||||
for (let i = 0; i < 60; i++) {
|
for (let i = 0; i < 40; i++) {
|
||||||
await sleep(1000);
|
await sleep(1000);
|
||||||
console.log(`[${60 - i}]秒后开始发送请求`);
|
console.log(`[${40 - i}]秒后开始发送请求`);
|
||||||
}
|
}
|
||||||
|
|
||||||
await page.goto(`https://you.com/search?q=&fromSearchBar=true&tbm=youchat&chatMode=custom`, {waitUntil: "domcontentloaded"});
|
await page.goto(`https://you.com/search?q=&fromSearchBar=true&tbm=youchat&chatMode=custom`, {waitUntil: "domcontentloaded"});
|
||||||
|
await sleep(4000);
|
||||||
|
|
||||||
const connectionEstablished = await delayedRequestWithRetry();
|
const connectionEstablished = await delayedRequestWithRetry();
|
||||||
if (!connectionEstablished) {
|
if (!connectionEstablished) {
|
||||||
@@ -885,40 +891,46 @@ class YouProvider {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 4000)); // 4秒延迟
|
if (enableDelayLogic) {
|
||||||
console.log(`尝试发送请求 (尝试 ${attempt}/${maxRetries})`);
|
await new Promise(resolve => setTimeout(resolve, 4000)); // 4秒延迟
|
||||||
|
console.log(`尝试发送请求 (尝试 ${attempt}/${maxRetries})`);
|
||||||
|
|
||||||
const {connected, cloudflareDetected, error} = await checkConnectionAndCloudflare(page);
|
const {connected, cloudflareDetected, error} = await checkConnectionAndCloudflare(page);
|
||||||
|
|
||||||
if (connected) {
|
if (connected) {
|
||||||
console.log("连接成功,准备唤醒浏览器");
|
console.log("连接成功,准备唤醒浏览器");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 唤醒浏览器
|
// 唤醒浏览器
|
||||||
await page.evaluate(() => {
|
await page.evaluate(() => {
|
||||||
window.scrollTo(0, 100);
|
window.scrollTo(0, 100);
|
||||||
window.scrollTo(0, 0);
|
window.scrollTo(0, 0);
|
||||||
const body = document.body;
|
const body = document.body;
|
||||||
if (body) {
|
if (body) {
|
||||||
body.click();
|
body.click();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||||
|
|
||||||
console.log("开始发送请求");
|
console.log("开始发送请求");
|
||||||
emitter.emit("start", traceId);
|
emitter.emit("start", traceId);
|
||||||
return true;
|
return true;
|
||||||
} catch (wakeupError) {
|
} catch (wakeupError) {
|
||||||
console.error("浏览器唤醒失败:", wakeupError);
|
console.error("浏览器唤醒失败:", wakeupError);
|
||||||
emitter.emit("start", traceId);
|
emitter.emit("start", traceId);
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
} else if (cloudflareDetected) {
|
||||||
|
console.error("检测到 Cloudflare 拦截");
|
||||||
|
emitter.emit("error", new Error("Cloudflare challenge detected"));
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
console.log(`连接失败,准备重试 (${attempt}/${maxRetries}). 错误: ${error || 'Unknown'}`);
|
||||||
}
|
}
|
||||||
} else if (cloudflareDetected) {
|
|
||||||
console.error("检测到 Cloudflare 拦截");
|
|
||||||
emitter.emit("error", new Error("Cloudflare challenge detected"));
|
|
||||||
return false;
|
|
||||||
} else {
|
} else {
|
||||||
console.log(`连接失败,准备重试 (${attempt}/${maxRetries}). 错误: ${error || 'Unknown'}`);
|
console.log("开始发送请求");
|
||||||
|
emitter.emit("start", traceId);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.error("达到最大重试次数,连接失败");
|
console.error("达到最大重试次数,连接失败");
|
||||||
@@ -1012,6 +1024,10 @@ class YouProvider {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!enableDelayLogic) {
|
||||||
|
await page.goto(`https://you.com/search?q=&fromSearchBar=true&tbm=youchat&chatMode=custom`, {waitUntil: "domcontentloaded"});
|
||||||
|
}
|
||||||
|
|
||||||
responseTimeout = setTimeout(async () => {
|
responseTimeout = setTimeout(async () => {
|
||||||
if (!responseStarted) {
|
if (!responseStarted) {
|
||||||
console.log("40秒内没有收到响应,重新建立连接");
|
console.log("40秒内没有收到响应,重新建立连接");
|
||||||
|
|||||||
Reference in New Issue
Block a user