|
| 1 | +import { type ClickOptions, createCursor } from '../spoof' |
| 2 | +import { join } from 'path' |
| 3 | +import { promises as fs } from 'fs' |
| 4 | +import installMouseHelper from '../mouse-helper' |
| 5 | +import puppeteer from 'puppeteer' |
| 6 | + |
| 7 | +const delay = async (ms: number): Promise<void> => { |
| 8 | + if (ms < 1) return |
| 9 | + return await new Promise((resolve) => setTimeout(resolve, ms)) |
| 10 | +} |
| 11 | + |
| 12 | +const cursorDefaultOptions = { |
| 13 | + moveDelay: 100, |
| 14 | + moveSpeed: 99, |
| 15 | + hesitate: 100, |
| 16 | + waitForClick: 10, |
| 17 | + scrollDelay: 100, |
| 18 | + scrollSpeed: 40, |
| 19 | + inViewportMargin: 50 |
| 20 | +} as const satisfies ClickOptions |
| 21 | + |
| 22 | +puppeteer.launch({ headless: false }).then(async (browser) => { |
| 23 | + const page = await browser.newPage() |
| 24 | + |
| 25 | + await installMouseHelper(page) |
| 26 | + |
| 27 | + const cursor = createCursor(page, undefined, undefined, { |
| 28 | + move: cursorDefaultOptions, |
| 29 | + click: cursorDefaultOptions, |
| 30 | + moveTo: cursorDefaultOptions |
| 31 | + }) |
| 32 | + |
| 33 | + const html = await fs.readFile(join(__dirname, 'custom-page.html'), 'utf8') |
| 34 | + |
| 35 | + await page.goto('data:text/html,' + encodeURIComponent(html), { |
| 36 | + waitUntil: 'networkidle2' |
| 37 | + }) |
| 38 | + |
| 39 | + const performActions = async (): Promise<void> => { |
| 40 | + await cursor.click('#box1') |
| 41 | + |
| 42 | + await cursor.click('#box2') |
| 43 | + |
| 44 | + await cursor.click('#box3') |
| 45 | + |
| 46 | + await cursor.click('#box1') |
| 47 | + } |
| 48 | + |
| 49 | + await performActions() |
| 50 | + |
| 51 | + // allows us to hit "refresh" button to restart the events |
| 52 | + // eslint-disable-next-line @typescript-eslint/no-misused-promises |
| 53 | + page.on('load', async () => { |
| 54 | + await delay(500) |
| 55 | + await page.evaluate(() => { window.scrollTo(0, 0) }) |
| 56 | + await delay(1000) |
| 57 | + |
| 58 | + await performActions() |
| 59 | + }) |
| 60 | +}).catch((e) => { |
| 61 | + console.error(e) |
| 62 | +}) |
0 commit comments