修改完成55%
This commit is contained in:
@@ -5,14 +5,14 @@ const [url, selector, output, width = '360', height = '800', action] = process.a
|
||||
const renderSettleMilliseconds = 900
|
||||
|
||||
if (!url || !selector || !output) {
|
||||
throw new Error('Usage: node scripts/capture-chrome-page.js <url> <selector> <output> [width] [height] [sms-tab]')
|
||||
throw new Error('Usage: node scripts/capture-chrome-page.js <url> <selector> <output> [width] [height] [sms-tab|g06-results|g06-empty|fresh-navigation]')
|
||||
}
|
||||
|
||||
const sleep = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds))
|
||||
|
||||
const connect = async () => {
|
||||
const pages = await (await fetch('http://127.0.0.1:9222/json/list')).json()
|
||||
const page = pages.find((item) => item.type === 'page' && item.url.includes('localhost:5173'))
|
||||
const page = pages.find((item) => item.type === 'page' && item.url.startsWith('http://localhost:5173'))
|
||||
if (!page) throw new Error('Chrome debugging has no localhost:5173 page')
|
||||
|
||||
const socket = new WebSocket(page.webSocketDebuggerUrl)
|
||||
@@ -77,6 +77,36 @@ const waitForFreshDocument = async (send, documentTimeOrigin) => {
|
||||
}
|
||||
}
|
||||
|
||||
const prepareG06State = async (send, action) => {
|
||||
if (action !== 'g06-results' && action !== 'g06-empty') return
|
||||
|
||||
const keyword = action === 'g06-results' ? '汤' : '不存在的家谱'
|
||||
const prepared = await send('Runtime.evaluate', {
|
||||
expression: `(() => {
|
||||
const input = document.querySelector('.search-input input')
|
||||
const button = document.querySelector('.search-action')
|
||||
if (!input || !button) return false
|
||||
input.value = ${JSON.stringify(keyword)}
|
||||
input.dispatchEvent(new Event('input', { bubbles: true }))
|
||||
button.click()
|
||||
return true
|
||||
})()`,
|
||||
returnByValue: true
|
||||
})
|
||||
if (!prepared.result?.value) throw new Error(`Could not prepare ${action}`)
|
||||
|
||||
const stateSelector = action === 'g06-results' ? '.search-results .genealogy-card' : '.search-empty'
|
||||
for (let attempt = 0; attempt < 30; attempt += 1) {
|
||||
const result = await send('Runtime.evaluate', {
|
||||
expression: `Boolean(document.querySelector(${JSON.stringify(stateSelector)}))`,
|
||||
returnByValue: true
|
||||
})
|
||||
if (result.result?.value) return
|
||||
if (attempt === 29) throw new Error(`Expected G06 state did not render: ${action}`)
|
||||
await sleep(100)
|
||||
}
|
||||
}
|
||||
|
||||
const capture = async () => {
|
||||
const { socket, send } = await connect()
|
||||
const viewportWidth = Number(width)
|
||||
@@ -100,8 +130,10 @@ const capture = async () => {
|
||||
const documentTimeOrigin = timeOriginResult.result?.value
|
||||
await send('Page.navigate', { url })
|
||||
await waitForRequestedUrl(send, url)
|
||||
await send('Page.reload')
|
||||
await waitForFreshDocument(send, documentTimeOrigin)
|
||||
if (action !== 'fresh-navigation') {
|
||||
await send('Page.reload')
|
||||
await waitForFreshDocument(send, documentTimeOrigin)
|
||||
}
|
||||
|
||||
for (let attempt = 0; attempt < 30; attempt += 1) {
|
||||
const result = await send('Runtime.evaluate', {
|
||||
@@ -118,6 +150,7 @@ const capture = async () => {
|
||||
expression: "document.querySelectorAll('.login-tab')[1]?.click()"
|
||||
})
|
||||
}
|
||||
await prepareG06State(send, action)
|
||||
|
||||
await waitForLoadedImages(send)
|
||||
await sleep(renderSettleMilliseconds)
|
||||
|
||||
Reference in New Issue
Block a user