Skip to content

Commit ef4a8f9

Browse files
development: browser-based development tool (#155)
* development: browser-based development tool * whitespace * defaults * scroll boxes * update readme * fix args
1 parent d59b117 commit ef4a8f9

File tree

6 files changed

+107
-5
lines changed

6 files changed

+107
-5
lines changed

.npmignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ node_modules
22
.vscode
33
.cache
44
dist
5-
__test__
5+
__test__
6+
__debug__

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"scripts": {
99
"prepare": "husky && yarn lint && yarn build",
1010
"build": "tsc -p tsconfig.build.json",
11+
"debug": "ts-node src/__debug__/browser-debug.ts",
1112
"lint": "yarn ts-standard --fix",
1213
"test": "jest"
1314
},

src/__debug__/browser-debug.ts

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
})

src/__debug__/custom-page.html

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<html lang="en">
2+
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Document</title>
8+
<style>
9+
.box {
10+
background: #eee;
11+
transition: all 0.4s;
12+
width: 50px;
13+
height: 50px;
14+
position: absolute;
15+
}
16+
</style>
17+
</head>
18+
19+
<body>
20+
<div id="box1" class="box" style="
21+
left: 200;
22+
top: 40;
23+
">Box 1</div>
24+
<div id="box2" class="box" style="
25+
left: 200;
26+
top: 3000;
27+
">Box 2</div>
28+
<div id="box3" class="box" style="
29+
left: 3000;
30+
top: 5000;
31+
">Box 3</div>
32+
</body>
33+
34+
</html>

src/spoof.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const log = debug('ghost-cursor')
1818

1919
export interface BoxOptions {
2020
/**
21-
* Percentage of padding to be added inside the element.
21+
* Percentage of padding to be added inside the element when determining the target point.
2222
* Example:
2323
* - `0` = may be anywhere within the element.
2424
* - `100` = will always be center of element.

tsconfig.build.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{
2-
"extends": "./tsconfig",
3-
"exclude": ["node_modules", "**/__test__/**/*"]
4-
}
2+
"extends": "./tsconfig",
3+
"exclude": [
4+
"node_modules",
5+
"**/__test__/**/*",
6+
"**/__debug__/**/*"
7+
]
8+
}

0 commit comments

Comments
 (0)