@tamasno1/safari-cli 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +204 -0
- package/dist/cli.d.ts +5 -0
- package/dist/cli.js +795 -0
- package/dist/cli.js.map +1 -0
- package/dist/session.d.ts +17 -0
- package/dist/session.js +45 -0
- package/dist/session.js.map +1 -0
- package/dist/webdriver.d.ts +85 -0
- package/dist/webdriver.js +231 -0
- package/dist/webdriver.js.map +1 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tamas Boros
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
# safari-cli
|
|
2
|
+
|
|
3
|
+
A command-line tool to control Safari on macOS via WebDriver. No MCP server needed — just a standalone CLI.
|
|
4
|
+
|
|
5
|
+
Talks directly to `safaridriver` (built into macOS) using the W3C WebDriver protocol. Session state is persisted to `~/.safari-cli/session.json` so commands work across terminal invocations.
|
|
6
|
+
|
|
7
|
+
## Prerequisites
|
|
8
|
+
|
|
9
|
+
- **macOS** (Safari and SafariDriver are macOS-only)
|
|
10
|
+
- **Node.js 18+**
|
|
11
|
+
- **Safari** with Remote Automation enabled:
|
|
12
|
+
1. Safari → Settings → Advanced → check "Show features for web developers"
|
|
13
|
+
2. Develop menu → Allow Remote Automation
|
|
14
|
+
3. Run once: `sudo safaridriver --enable`
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
# From this directory
|
|
20
|
+
npm install
|
|
21
|
+
npm run build
|
|
22
|
+
npm link # makes `safari-cli` available globally
|
|
23
|
+
|
|
24
|
+
# Or run directly
|
|
25
|
+
node dist/cli.js <command>
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Quick Start
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
# Start a Safari session
|
|
32
|
+
safari-cli start
|
|
33
|
+
|
|
34
|
+
# Navigate
|
|
35
|
+
safari-cli navigate https://example.com
|
|
36
|
+
|
|
37
|
+
# Page info
|
|
38
|
+
safari-cli info
|
|
39
|
+
# Title: Example Domain
|
|
40
|
+
# URL: https://example.com/
|
|
41
|
+
|
|
42
|
+
# Take a screenshot
|
|
43
|
+
safari-cli screenshot --output page.png
|
|
44
|
+
|
|
45
|
+
# Execute JavaScript
|
|
46
|
+
safari-cli execute 'document.title'
|
|
47
|
+
|
|
48
|
+
# Read console logs
|
|
49
|
+
safari-cli console
|
|
50
|
+
|
|
51
|
+
# Inspect an element
|
|
52
|
+
safari-cli inspect 'h1'
|
|
53
|
+
|
|
54
|
+
# Click an element
|
|
55
|
+
safari-cli click 'a[href]'
|
|
56
|
+
|
|
57
|
+
# Stop the session
|
|
58
|
+
safari-cli stop
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Commands
|
|
62
|
+
|
|
63
|
+
### Session Management
|
|
64
|
+
|
|
65
|
+
| Command | Description |
|
|
66
|
+
|---------|-------------|
|
|
67
|
+
| `start [--port PORT]` | Start SafariDriver + create browser session (default port: 9515) |
|
|
68
|
+
| `stop` | Close session and kill SafariDriver |
|
|
69
|
+
| `status` | Show session status, PID, current URL |
|
|
70
|
+
|
|
71
|
+
### Navigation
|
|
72
|
+
|
|
73
|
+
| Command | Description |
|
|
74
|
+
|---------|-------------|
|
|
75
|
+
| `navigate <url>` / `go <url>` | Navigate to a URL (auto-prepends `https://`) |
|
|
76
|
+
| `back` | Go back |
|
|
77
|
+
| `forward` | Go forward |
|
|
78
|
+
| `refresh` | Reload the page |
|
|
79
|
+
|
|
80
|
+
### Page Information
|
|
81
|
+
|
|
82
|
+
| Command | Description |
|
|
83
|
+
|---------|-------------|
|
|
84
|
+
| `info` | Get page title and URL |
|
|
85
|
+
| `source [-o file]` | Get page source HTML |
|
|
86
|
+
| `html [selector] [-o file]` | Get outerHTML of element or full page |
|
|
87
|
+
| `perf` | Performance metrics (load times, paint, transfer size) |
|
|
88
|
+
|
|
89
|
+
### Screenshots
|
|
90
|
+
|
|
91
|
+
| Command | Description |
|
|
92
|
+
|---------|-------------|
|
|
93
|
+
| `screenshot [-o file] [-s selector]` | Capture page or element screenshot as PNG |
|
|
94
|
+
|
|
95
|
+
### Developer Console
|
|
96
|
+
|
|
97
|
+
| Command | Description |
|
|
98
|
+
|---------|-------------|
|
|
99
|
+
| `console [--level LEVEL] [--inject]` | Get captured console logs (LOG, WARN, ERROR, INFO, DEBUG) |
|
|
100
|
+
| `console-clear` | Clear captured console logs |
|
|
101
|
+
| `network [--inject]` | Get captured network requests (fetch + XHR) |
|
|
102
|
+
| `network-clear` | Clear captured network logs |
|
|
103
|
+
|
|
104
|
+
> **Note:** Console/network capture works by injecting JavaScript hooks. The first call to `console` or `network` auto-injects the hooks. Logs from before injection won't be captured. Use `--inject` to set up capture early.
|
|
105
|
+
|
|
106
|
+
### JavaScript Execution
|
|
107
|
+
|
|
108
|
+
| Command | Description |
|
|
109
|
+
|---------|-------------|
|
|
110
|
+
| `execute <script>` / `eval <script>` | Execute JS in browser context |
|
|
111
|
+
| `execute --async <script>` | Execute async JS (call `arguments[0]` to resolve) |
|
|
112
|
+
|
|
113
|
+
Multi-statement scripts work: `safari-cli execute 'console.log("hi"); 42'`
|
|
114
|
+
|
|
115
|
+
### DOM Interaction
|
|
116
|
+
|
|
117
|
+
| Command | Description |
|
|
118
|
+
|---------|-------------|
|
|
119
|
+
| `inspect <selector>` | Inspect element (tag, text, rect, attributes, visibility) |
|
|
120
|
+
| `click <selector>` | Click an element |
|
|
121
|
+
| `type <selector> <text> [--clear]` | Type text into input element |
|
|
122
|
+
| `find <selector> [--text]` | Find all matching elements |
|
|
123
|
+
| `wait <selector> [-t ms]` | Wait for element to appear (default: 10s) |
|
|
124
|
+
|
|
125
|
+
Selectors are CSS by default. XPath if starting with `//`.
|
|
126
|
+
|
|
127
|
+
### Cookies
|
|
128
|
+
|
|
129
|
+
| Command | Description |
|
|
130
|
+
|---------|-------------|
|
|
131
|
+
| `cookies [--json]` | List all cookies |
|
|
132
|
+
|
|
133
|
+
### Window Management
|
|
134
|
+
|
|
135
|
+
| Command | Description |
|
|
136
|
+
|---------|-------------|
|
|
137
|
+
| `resize [-w W -h H]` | Get or set window size |
|
|
138
|
+
| `resize --maximize` | Maximize window |
|
|
139
|
+
| `resize --fullscreen` | Fullscreen window |
|
|
140
|
+
| `tabs` | List open tabs/windows |
|
|
141
|
+
| `tab <handle>` | Switch to a tab by handle |
|
|
142
|
+
|
|
143
|
+
### Alerts & Frames
|
|
144
|
+
|
|
145
|
+
| Command | Description |
|
|
146
|
+
|---------|-------------|
|
|
147
|
+
| `alert [--accept] [--dismiss] [--text T]` | Handle browser alerts/prompts |
|
|
148
|
+
| `frame [id]` | Switch iframe (no arg = top level) |
|
|
149
|
+
|
|
150
|
+
## Architecture
|
|
151
|
+
|
|
152
|
+
```
|
|
153
|
+
safari-cli
|
|
154
|
+
├── src/
|
|
155
|
+
│ ├── cli.ts # Commander-based CLI with all commands
|
|
156
|
+
│ ├── webdriver.ts # Raw W3C WebDriver HTTP client (no selenium)
|
|
157
|
+
│ └── session.ts # Session state persistence (~/.safari-cli/)
|
|
158
|
+
├── package.json
|
|
159
|
+
└── tsconfig.json
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
- **Zero heavy dependencies** — only `commander` for CLI parsing
|
|
163
|
+
- Talks to `safaridriver` via raw HTTP (`fetch()`) using the W3C WebDriver protocol
|
|
164
|
+
- Session state stored in `~/.safari-cli/session.json` so commands work across terminal invocations
|
|
165
|
+
- Console/network logging via injected JavaScript hooks (same approach as safari-mcp-server)
|
|
166
|
+
|
|
167
|
+
## Scripting Examples
|
|
168
|
+
|
|
169
|
+
```bash
|
|
170
|
+
# Full workflow
|
|
171
|
+
safari-cli start
|
|
172
|
+
safari-cli navigate https://news.ycombinator.com
|
|
173
|
+
safari-cli console --inject
|
|
174
|
+
safari-cli network --inject
|
|
175
|
+
safari-cli screenshot --output hn.png
|
|
176
|
+
safari-cli execute 'document.querySelectorAll(".titleline a").length'
|
|
177
|
+
safari-cli find '.titleline a' --text
|
|
178
|
+
safari-cli console
|
|
179
|
+
safari-cli network
|
|
180
|
+
safari-cli stop
|
|
181
|
+
|
|
182
|
+
# Quick page audit
|
|
183
|
+
safari-cli start && safari-cli go https://mysite.com
|
|
184
|
+
safari-cli perf
|
|
185
|
+
safari-cli execute 'document.querySelectorAll("img:not([alt])").length'
|
|
186
|
+
safari-cli stop
|
|
187
|
+
|
|
188
|
+
# Fill a form
|
|
189
|
+
safari-cli type '#email' 'user@example.com'
|
|
190
|
+
safari-cli type '#password' 'secret' --clear
|
|
191
|
+
safari-cli click 'button[type=submit]'
|
|
192
|
+
safari-cli wait '.dashboard'
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## Troubleshooting
|
|
196
|
+
|
|
197
|
+
- **"Cannot connect to SafariDriver"** — Run `sudo safaridriver --enable` and enable Develop → Allow Remote Automation in Safari
|
|
198
|
+
- **"Session is stale"** — Run `safari-cli stop` then `safari-cli start`
|
|
199
|
+
- **Console logs empty** — Hooks are injected on first `console` call. Logs from before that aren't captured. Use `console --inject` early.
|
|
200
|
+
- **Only one session at a time** — Safari WebDriver only supports a single session
|
|
201
|
+
|
|
202
|
+
## License
|
|
203
|
+
|
|
204
|
+
MIT
|