Spaces:
Paused
Paused
github-actions[bot] commited on
Commit ·
2275d80
1
Parent(s): 23645dc
Update from GitHub Actions
Browse files- src/index.ts +17 -46
src/index.ts
CHANGED
|
@@ -10,8 +10,7 @@ const app = new Hono()
|
|
| 10 |
// 浏览器实例
|
| 11 |
let browser: Browser | null = null
|
| 12 |
let gensparkContext: BrowserContext | null = null
|
| 13 |
-
|
| 14 |
-
let gensparkPage: Page | null = null
|
| 15 |
const userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3';
|
| 16 |
// 初始化浏览器
|
| 17 |
async function initBrowser() {
|
|
@@ -49,20 +48,11 @@ async function initGensparkPage(cookies?: any[]) {
|
|
| 49 |
acceptDownloads: true,
|
| 50 |
})
|
| 51 |
}
|
| 52 |
-
|
| 53 |
-
if (!gensparkPage) {
|
| 54 |
-
gensparkPage = await gensparkContext.newPage()
|
| 55 |
-
}
|
| 56 |
if (cookies && cookies.length > 0) {
|
|
|
|
| 57 |
await gensparkContext.addCookies(cookies);
|
| 58 |
-
// 首次加载页面
|
| 59 |
-
await gensparkPage.goto('https://www.genspark.ai', {
|
| 60 |
-
waitUntil: 'networkidle',
|
| 61 |
-
timeout: 60000
|
| 62 |
-
})
|
| 63 |
-
console.log('GenSpark页面已初始化')
|
| 64 |
}
|
| 65 |
-
|
| 66 |
return gensparkPage
|
| 67 |
}
|
| 68 |
|
|
@@ -191,18 +181,18 @@ app.get('/', async (c) => {
|
|
| 191 |
|
| 192 |
// 修改点 2: 添加 /genspark 路由来获取reCAPTCHA令牌
|
| 193 |
app.get('/genspark', async (c) => {
|
| 194 |
-
try {
|
| 195 |
-
const headers = Object.fromEntries(c.req.raw.headers)
|
| 196 |
-
// Get the cookie string from headers
|
| 197 |
-
const cookieString = headers.cookie || '';
|
| 198 |
-
// Parse cookies into an array of objects with name and value properties
|
| 199 |
-
const cookies = cookieString.split(';').map(cookie => {
|
| 200 |
-
const [name, value] = cookie.trim().split('=');
|
| 201 |
-
return { name, value, domain: 'www.genspark.ai', path: '/' };
|
| 202 |
-
}).filter(cookie => cookie.name && cookie.value);
|
| 203 |
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
|
|
|
|
|
|
| 206 |
//刷新页面以确保获取新令牌
|
| 207 |
await gensparkPage.goto('https://www.genspark.ai/agents?type=moa_chat', {
|
| 208 |
waitUntil: 'networkidle',
|
|
@@ -236,18 +226,16 @@ app.get('/genspark', async (c) => {
|
|
| 236 |
}
|
| 237 |
catch (error) {
|
| 238 |
console.error('获取令牌失败:', error)
|
| 239 |
-
if (gensparkPage) {
|
| 240 |
-
await gensparkPage.close().catch(() => { });
|
| 241 |
-
gensparkPage = null;
|
| 242 |
-
}
|
| 243 |
-
|
| 244 |
if (gensparkContext) {
|
| 245 |
await gensparkContext.close().catch(() => { });
|
| 246 |
gensparkContext = null;
|
| 247 |
}
|
| 248 |
-
|
|
|
|
|
|
|
| 249 |
}
|
| 250 |
|
|
|
|
| 251 |
})
|
| 252 |
|
| 253 |
// 处理所有 HTTP 方法
|
|
@@ -287,11 +275,6 @@ app.all('*', async (c) => {
|
|
| 287 |
|
| 288 |
// 清理函数
|
| 289 |
async function cleanup() {
|
| 290 |
-
if (gensparkPage) {
|
| 291 |
-
await gensparkPage.close().catch(() => { });
|
| 292 |
-
gensparkPage = null;
|
| 293 |
-
}
|
| 294 |
-
|
| 295 |
if (gensparkContext) {
|
| 296 |
await gensparkContext.close().catch(() => { });
|
| 297 |
gensparkContext = null;
|
|
@@ -308,18 +291,6 @@ async function cleanup() {
|
|
| 308 |
process.on('SIGINT', cleanup)
|
| 309 |
process.on('SIGTERM', cleanup)
|
| 310 |
|
| 311 |
-
initGensparkPage().catch(async () => {
|
| 312 |
-
if (gensparkPage) {
|
| 313 |
-
await gensparkPage.close().catch(() => { });
|
| 314 |
-
gensparkPage = null;
|
| 315 |
-
}
|
| 316 |
-
|
| 317 |
-
if (gensparkContext) {
|
| 318 |
-
await gensparkContext.close().catch(() => { });
|
| 319 |
-
gensparkContext = null;
|
| 320 |
-
}
|
| 321 |
-
})
|
| 322 |
-
|
| 323 |
const port = Number(process.env.PORT || '7860');
|
| 324 |
console.log(`Server is running on port http://localhost:${port}`)
|
| 325 |
|
|
|
|
| 10 |
// 浏览器实例
|
| 11 |
let browser: Browser | null = null
|
| 12 |
let gensparkContext: BrowserContext | null = null
|
| 13 |
+
|
|
|
|
| 14 |
const userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3';
|
| 15 |
// 初始化浏览器
|
| 16 |
async function initBrowser() {
|
|
|
|
| 48 |
acceptDownloads: true,
|
| 49 |
})
|
| 50 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
if (cookies && cookies.length > 0) {
|
| 52 |
+
await gensparkContext.clearCookies()
|
| 53 |
await gensparkContext.addCookies(cookies);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
}
|
| 55 |
+
const gensparkPage = await gensparkContext.newPage()
|
| 56 |
return gensparkPage
|
| 57 |
}
|
| 58 |
|
|
|
|
| 181 |
|
| 182 |
// 修改点 2: 添加 /genspark 路由来获取reCAPTCHA令牌
|
| 183 |
app.get('/genspark', async (c) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
+
const headers = Object.fromEntries(c.req.raw.headers)
|
| 186 |
+
// Get the cookie string from headers
|
| 187 |
+
const cookieString = headers.cookie || '';
|
| 188 |
+
// Parse cookies into an array of objects with name and value properties
|
| 189 |
+
const cookies = cookieString.split(';').map(cookie => {
|
| 190 |
+
const [name, value] = cookie.trim().split('=');
|
| 191 |
+
return { name, value, domain: 'www.genspark.ai', path: '/' };
|
| 192 |
+
}).filter(cookie => cookie.name && cookie.value);
|
| 193 |
|
| 194 |
+
const gensparkPage = await initGensparkPage(cookies)
|
| 195 |
+
try {
|
| 196 |
//刷新页面以确保获取新令牌
|
| 197 |
await gensparkPage.goto('https://www.genspark.ai/agents?type=moa_chat', {
|
| 198 |
waitUntil: 'networkidle',
|
|
|
|
| 226 |
}
|
| 227 |
catch (error) {
|
| 228 |
console.error('获取令牌失败:', error)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
if (gensparkContext) {
|
| 230 |
await gensparkContext.close().catch(() => { });
|
| 231 |
gensparkContext = null;
|
| 232 |
}
|
| 233 |
+
}
|
| 234 |
+
finally {
|
| 235 |
+
await gensparkPage.close().catch(() => { });
|
| 236 |
}
|
| 237 |
|
| 238 |
+
return c.json({ code: 500, message: '获取令牌失败' })
|
| 239 |
})
|
| 240 |
|
| 241 |
// 处理所有 HTTP 方法
|
|
|
|
| 275 |
|
| 276 |
// 清理函数
|
| 277 |
async function cleanup() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
if (gensparkContext) {
|
| 279 |
await gensparkContext.close().catch(() => { });
|
| 280 |
gensparkContext = null;
|
|
|
|
| 291 |
process.on('SIGINT', cleanup)
|
| 292 |
process.on('SIGTERM', cleanup)
|
| 293 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
const port = Number(process.env.PORT || '7860');
|
| 295 |
console.log(`Server is running on port http://localhost:${port}`)
|
| 296 |
|