Download dev/tests/web/cdp.js from mchuca/ServerMonitor: direct link, hf CLI and curl.
- Browser
- Download file 2.99 kB
-
https://huggingface.co/mchuca/ServerMonitor/resolve/main/dev/tests/web/cdp.js
- Command line
-
hf download hf://mchuca/ServerMonitor/dev/tests/web/cdp.js
-
curl -L -o cdp.js https://huggingface.co/mchuca/ServerMonitor/resolve/main/dev/tests/web/cdp.js
2.99 kB
| // minimal DevTools-protocol driver for headless Chrome (Node's built-in WebSocket) | |
| const { spawn } = require('child_process'), fs = require('fs'), os = require('os'), path = require('path'); | |
| const sleep = ms => new Promise(r => setTimeout(r, ms)); | |
| exports.sleep = sleep; | |
| exports.launch = async (size = '1500,940') => { | |
| const port = 9800 + (process.pid % 150), dir = fs.mkdtempSync(path.join(os.tmpdir(), 'sm-review-chrome-')); | |
| const chrome = spawn('google-chrome', ['--headless=new', '--no-sandbox', '--password-store=basic', '--disable-gpu', `--remote-debugging-port=${port}`, `--user-data-dir=${dir}`, `--window-size=${size}`, 'about:blank'], { stdio: 'ignore' }); | |
| const list = async () => (await (await fetch(`http://127.0.0.1:${port}/json`)).json()).filter(t => t.type === 'page'); | |
| for (let i = 0; i < 60; ++i) { await sleep(200); try { if ((await list()).length) break; } catch {} } | |
| const attach = async target => { | |
| const ws = new WebSocket(target.webSocketDebuggerUrl), pending = new Map(), problems = []; | |
| let id = 0; | |
| const send = (method, params = {}) => new Promise(res => { pending.set(++id, res); ws.send(JSON.stringify({ id, method, params })); }); | |
| ws.onmessage = e => { | |
| const m = JSON.parse(e.data); | |
| if (m.id) return pending.get(m.id)?.(m.result || m.error); | |
| if (m.method === 'Runtime.exceptionThrown') problems.push('EXCEPTION ' + (m.params.exceptionDetails.exception?.description || m.params.exceptionDetails.text)); | |
| if (m.method === 'Runtime.consoleAPICalled' && ['error', 'warning'].includes(m.params.type)) problems.push('CONSOLE ' + m.params.args.map(a => a.value ?? a.description).join(' ')); | |
| if (m.method === 'Log.entryAdded' && ['error', 'warning'].includes(m.params.entry.level)) problems.push('LOG ' + m.params.entry.text + ' ' + (m.params.entry.url || '')); | |
| }; | |
| await new Promise(r => ws.onopen = r); | |
| await send('Runtime.enable'), await send('Log.enable'), await send('Page.enable'); | |
| const evaluate = async body => { // async function body run in the page | |
| const r = await send('Runtime.evaluate', { expression: `(async () => { ${body} })()`, awaitPromise: true, returnByValue: true }); | |
| if (r.exceptionDetails) throw new Error(r.exceptionDetails.exception?.description || r.exceptionDetails.text); | |
| return r.result?.value; | |
| }; | |
| return { send, evaluate, problems, go: url => send('Page.navigate', { url }), shot: async file => fs.writeFileSync(file, Buffer.from((await send('Page.captureScreenshot', { format: 'png' })).data, 'base64')) }; | |
| }; | |
| const newTab = async url => attach(await (await fetch(`http://127.0.0.1:${port}/json/new?${encodeURIComponent(url)}`, { method: 'PUT' })).json()); | |
| return { first: async () => attach((await list())[0]), newTab, close: () => { chrome.kill(); setTimeout(() => fs.rmSync(dir, { recursive: true, force: true }), 500); } }; | |
| }; | |