Skip to content
This repository has been archived by the owner on Oct 17, 2023. It is now read-only.

Commit

Permalink
修复问题 (#9)
Browse files Browse the repository at this point in the history
* 修复 cookie 解析

* 修复登录判断

* 修复登录判断

* 修复登录判断

* 修复登录判断

* 点击[我知道了]
  • Loading branch information
zhuweiyou committed Sep 11, 2023
1 parent 270b3f0 commit 23d7b44
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
31 changes: 25 additions & 6 deletions headless.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,27 @@ export async function headless({cookie, timeout = 1000 * 60, headless = 'new', p
})
}

await page.goto('https://yiyan.baidu.com')
await page.goto('https://yiyan.baidu.com', {waitUntil: 'networkidle0'})

const need_login = await page.evaluate(() => {
const body_text = document.body.innerText
if (body_text.includes('接受协议') && body_text.includes('暂时退出')) {
for (const div of document.querySelectorAll('div')) {
if (div.textContent.includes('接受协议')) {
div?.click()
for (const element of document.querySelectorAll('div, span')) {
if (element.textContent === '接受协议') {
element?.click()
}
}
}

if (document.querySelector(`img[src*="https://himg.bdimg.com/sys/portrait"]`)) {
for (const element of document.querySelectorAll('div, span')) {
if (element.textContent === '开始体验') {
element?.click()
}
}
return false
}

return body_text.includes('登录') && (body_text.includes('加入体验') || body_text.includes('开始体验'))
})
if (need_login) {
Expand All @@ -45,6 +54,15 @@ export async function headless({cookie, timeout = 1000 * 60, headless = 'new', p
const message_input = await page.waitForSelector('#dialogue-input', {
timeout,
})

await page.evaluate(() => {
for (const element of document.querySelectorAll('div, span')) {
if (element.textContent === '我知道了') {
element?.click()
}
}
})

await message_input.type(prompt)
await sleep(1000)

Expand All @@ -68,7 +86,7 @@ export async function headless({cookie, timeout = 1000 * 60, headless = 'new', p
async response => {
try {
const response_url = response.url()
console.log(response_url)
// console.log(response_url)
if (response_url.startsWith('https://yiyan.baidu.com/eb/chat/conversation/v2')) {
// event-stream
// 参考 chat_conversation.txt
Expand Down Expand Up @@ -104,9 +122,10 @@ function parse_cookie(cookie) {
.map(item => {
const [name, ...value] = item.split('=')
return {
name,
name: name?.trim(),
value: value.join('='),
domain: 'yiyan.baidu.com',
}
})
.filter(item => item.name)
}
25 changes: 17 additions & 8 deletions headless_test.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
// 设置环境变量 export YIYAN_COOKIE=xxxx 之后运行 npm test 测试
import { headless } from './headless.mjs'

console.log(
await headless({
cookie: process.env.YIYAN_COOKIE,
prompt: '画个牛',
headless: false,
})
)
async function headless_test() {
const cookie = process.env.YIYAN_COOKIE
if (!cookie) {
console.log('设置环境变量 export YIYAN_COOKIE=xxxx 之后, 运行 npm test 测试')
return
}

console.log(
await headless({
cookie,
prompt: '画个牛',
headless: false,
})
)
}

headless_test()

0 comments on commit 23d7b44

Please sign in to comment.