43 lines
3.9 KiB
JavaScript
43 lines
3.9 KiB
JavaScript
import { spawn } from 'node:child_process'
|
|
import { mkdtemp, writeFile } from 'node:fs/promises'
|
|
import { tmpdir } from 'node:os'
|
|
import { join, dirname } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
const url = process.argv[2]
|
|
const EDGE = 'C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe'
|
|
const dir = dirname(fileURLToPath(import.meta.url))
|
|
const profile = await mkdtemp(join(tmpdir(), 'ss-v025b-'))
|
|
const out = {}
|
|
const sleep = ms => new Promise(r => setTimeout(r, ms))
|
|
let browser, ws
|
|
try {
|
|
browser = spawn(EDGE, ['--headless=new','--disable-gpu','--no-first-run','--no-default-browser-check','--remote-debugging-port=0','--user-data-dir=' + profile, 'about:blank'], { stdio: ['ignore','ignore','pipe'] })
|
|
const endpoint = await new Promise((resolve, reject) => { let log=''; const t=setTimeout(()=>reject(Error('timeout')),20000); browser.stderr.on('data', b=>{ log+=b; const m=log.match(/DevTools listening on (.+)/); if(m){clearTimeout(t);resolve(m[1].trim())} }); browser.on('error',reject) })
|
|
ws = new WebSocket(endpoint); await new Promise((r,j)=>{ws.onopen=r;ws.onerror=j})
|
|
let n=0; const pend=new Map()
|
|
ws.onmessage = e => { const m=JSON.parse(e.data); if(m.id){const p=pend.get(m.id);pend.delete(m.id);m.error?p.reject(Error(JSON.stringify(m.error))):p.resolve(m.result)} }
|
|
const cdp=(method,params={},sid)=>new Promise((res,rej)=>{const id=++n;pend.set(id,{resolve:res,reject:rej});ws.send(JSON.stringify({id,method,params,...(sid?{sessionId:sid}:{})}))})
|
|
const { targetId } = await cdp('Target.createTarget', { url: 'about:blank' })
|
|
const { sessionId } = await cdp('Target.attachToTarget', { targetId, flatten: true })
|
|
const call=(m,p={})=>cdp(m,p,sessionId)
|
|
await call('Page.enable'); await call('Runtime.enable')
|
|
const ev = async x => { const r=await call('Runtime.evaluate',{expression:x,returnByValue:true,awaitPromise:true}); if(r.exceptionDetails) throw Error(r.exceptionDetails.exception?.description||r.exceptionDetails.text); return r.result.value }
|
|
const waitFor = async (x,t=60000)=>{ const end=Date.now()+t; while(Date.now()<end){ try{ if(await ev(x)) return true }catch{} await sleep(300) } return false }
|
|
const shot = async name => { const r=await call('Page.captureScreenshot',{format:'png',captureBeyondViewport:false}); await writeFile(join(dir,name),Buffer.from(r.data,'base64')) }
|
|
await call('Emulation.setDeviceMetricsOverride',{width:1440,height:900,deviceScaleFactor:1,mobile:false})
|
|
await call('Page.navigate',{url})
|
|
await waitFor("document.readyState === 'complete'",30000)
|
|
try { await ev("(() => { const b=[...document.querySelectorAll('button')].find(x=>(x.innerText||'').trim()==='继续'); if(b)b.click(); return !!b })()") } catch {}
|
|
await waitFor("!!document.querySelector('.ssw-nav-hit')",60000)
|
|
await ev("document.querySelector('.ssw-nav-hit').click(); true")
|
|
await waitFor("!!document.querySelector('.ssw-root')",20000)
|
|
await sleep(2200)
|
|
out.before = await ev("(() => ({ button: document.querySelector('.ssw-detail-actions button')?.innerText ?? null, count: document.querySelector('.ssw-count')?.innerText ?? null, bulk: (document.body.innerText||'').includes('启用此项目的十席') }))()")
|
|
await shot('v025-unbound.png')
|
|
await ev("(() => { const b=document.querySelector('.ssw-detail-actions button'); if(b) b.click(); return !!b })()")
|
|
await waitFor("(() => { const b=document.querySelector('.ssw-detail-actions button'); return b && b.innerText.includes('开始对话') })()", 90000)
|
|
await sleep(1500)
|
|
out.after = await ev("(() => ({ button: document.querySelector('.ssw-detail-actions button')?.innerText ?? null, count: document.querySelector('.ssw-count')?.innerText ?? null, bound: document.querySelectorAll('.ssw-dot[data-status=bound]').length }))()")
|
|
await shot('v025-one-seat.png')
|
|
} catch(e) { out.fatal = String(e?.stack ?? e) } finally { try{ws?.close()}catch{}; try{browser?.kill()}catch{} }
|
|
console.log(JSON.stringify(out, null, 2)) |