Spaces:
Paused
Paused
Update worker.js
Browse files
worker.js
CHANGED
|
@@ -31,19 +31,48 @@ async function initBrowser() {
|
|
| 31 |
return browser;
|
| 32 |
}
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
async function chatWithBrowser(prompt, model) {
|
| 35 |
const browser = await initBrowser();
|
| 36 |
const page = await browser.newPage();
|
| 37 |
try {
|
| 38 |
console.log(`📝 聊天请求: ${prompt.substring(0, 50)}...`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
await page.goto('https://toolbaz.com/writer/ai-writer', { waitUntil: 'networkidle2', timeout: 30000 });
|
| 40 |
|
| 41 |
// 等待输入框
|
| 42 |
await page.waitForSelector('#input', { timeout: 10000 });
|
| 43 |
|
| 44 |
-
// 清空并输入内容
|
| 45 |
await page.click('#input', { clickCount: 3 });
|
| 46 |
-
await page.type('#input',
|
| 47 |
|
| 48 |
// 点击发送按钮
|
| 49 |
await page.click('#main_btn');
|
|
|
|
| 31 |
return browser;
|
| 32 |
}
|
| 33 |
|
| 34 |
+
function cleanPromptPrefix(prompt) {
|
| 35 |
+
const prefixPatterns = [
|
| 36 |
+
/^Write a code snippet that does the following in the specified language\s*:\s*/i,
|
| 37 |
+
/^Generate an original and engaging piece of writing on the following topic\s*:\s*/i,
|
| 38 |
+
/^Translate the following text to\s*\w+\s*:\s*/i,
|
| 39 |
+
/^Summarize the following text\s*:\s*/i,
|
| 40 |
+
/^Explain the following concept\s*:\s*/i,
|
| 41 |
+
/^Write a function that\s*:\s*/i,
|
| 42 |
+
/^Create a\s*\w+\s*that\s*:\s*/i
|
| 43 |
+
];
|
| 44 |
+
|
| 45 |
+
let cleanedPrompt = prompt;
|
| 46 |
+
for (const pattern of prefixPatterns) {
|
| 47 |
+
if (pattern.test(cleanedPrompt)) {
|
| 48 |
+
cleanedPrompt = cleanedPrompt.replace(pattern, '');
|
| 49 |
+
break;
|
| 50 |
+
}
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
return cleanedPrompt.trim();
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
async function chatWithBrowser(prompt, model) {
|
| 57 |
const browser = await initBrowser();
|
| 58 |
const page = await browser.newPage();
|
| 59 |
try {
|
| 60 |
console.log(`📝 聊天请求: ${prompt.substring(0, 50)}...`);
|
| 61 |
+
|
| 62 |
+
// 过滤前缀文本
|
| 63 |
+
const cleanedPrompt = cleanPromptPrefix(prompt);
|
| 64 |
+
if (cleanedPrompt !== prompt) {
|
| 65 |
+
console.log(`🧹 已过滤前缀: ${prompt.substring(0, 50)}... -> ${cleanedPrompt.substring(0, 50)}...`);
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
await page.goto('https://toolbaz.com/writer/ai-writer', { waitUntil: 'networkidle2', timeout: 30000 });
|
| 69 |
|
| 70 |
// 等待输入框
|
| 71 |
await page.waitForSelector('#input', { timeout: 10000 });
|
| 72 |
|
| 73 |
+
// 清空并输入过滤后的内容
|
| 74 |
await page.click('#input', { clickCount: 3 });
|
| 75 |
+
await page.type('#input', cleanedPrompt);
|
| 76 |
|
| 77 |
// 点击发送按钮
|
| 78 |
await page.click('#main_btn');
|