-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathbrowser.sh
More file actions
executable file
·225 lines (203 loc) · 7.16 KB
/
browser.sh
File metadata and controls
executable file
·225 lines (203 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/sh
# Browser automation helper
# Usage:
# ./browser.sh chromedriver - Start chromedriver (finds Chrome automatically)
# ./browser.sh start <url> - Start session and navigate
# ./browser.sh js <script> - Execute JS and print result
# ./browser.sh screenshot <output.png> - Take screenshot
# ./browser.sh dark - Enable dark mode
# ./browser.sh light - Enable light mode
# ./browser.sh mobile - Set mobile viewport (375x812)
# ./browser.sh desktop - Set desktop viewport (1400x900)
# ./browser.sh nav <url> - Navigate to URL
# ./browser.sh stop - End session
# ./browser.sh kill - Kill chromedriver
PORT_FILE="/tmp/chromedriver_port"
SESSION_FILE="/tmp/chrome_session_id"
get_port() {
if [ -f "$PORT_FILE" ]; then
cat "$PORT_FILE"
else
echo "No chromedriver running. Run: ./browser.sh chromedriver" >&2
exit 1
fi
}
find_chrome() {
for cmd in google-chrome-stable google-chrome chromium chromium-browser; do
path=$(which "$cmd" 2>/dev/null)
if [ -n "$path" ]; then
echo "$path"
return 0
fi
done
echo "No Chrome/Chromium found" >&2
exit 1
}
case "$1" in
chromedriver)
# Kill any existing chromedriver
if [ -f "$PORT_FILE" ]; then
OLD_PORT=$(cat "$PORT_FILE")
curl -s -X DELETE "http://localhost:$OLD_PORT/shutdown" 2>/dev/null || true
rm -f "$PORT_FILE" "$SESSION_FILE"
sleep 0.5
fi
# Start chromedriver and capture port
LOG_FILE="/tmp/chromedriver.log"
nohup chromedriver > "$LOG_FILE" 2>&1 &
PID=$!
# Wait for startup and extract port
for i in 1 2 3 4 5; do
sleep 0.5
PORT=$(grep -o 'successfully on port [0-9]*' "$LOG_FILE" 2>/dev/null | grep -o '[0-9]*')
if [ -n "$PORT" ]; then
echo "$PORT" > "$PORT_FILE"
echo "ChromeDriver started on port $PORT (pid $PID)"
exit 0
fi
done
echo "Failed to start ChromeDriver"
cat "$LOG_FILE"
exit 1
;;
start)
PORT=$(get_port)
CHROME=$(find_chrome)
URL="${2:-http://localhost:4000/}"
SESSION=$(curl -s -X POST "http://localhost:$PORT/session" \
-H "Content-Type: application/json" \
-d "{\"capabilities\": {\"alwaysMatch\": {\"goog:chromeOptions\": {\"binary\": \"$CHROME\", \"args\": [\"--headless\", \"--window-size=1400,900\"]}}}}" \
| jq -r '.value.sessionId')
if [ -z "$SESSION" ] || [ "$SESSION" = "null" ]; then
echo "Failed to create session"
exit 1
fi
echo "$SESSION" > "$SESSION_FILE"
curl -s -X POST "http://localhost:$PORT/session/$SESSION/url" \
-H "Content-Type: application/json" \
-d "{\"url\": \"$URL\"}" > /dev/null
echo "Session started: $SESSION"
echo "Navigated to: $URL"
;;
js)
PORT=$(get_port)
SESSION=$(cat "$SESSION_FILE" 2>/dev/null)
if [ -z "$SESSION" ]; then
echo "No session. Run: ./browser.sh start <url>"
exit 1
fi
SCRIPT="$2"
curl -s -X POST "http://localhost:$PORT/session/$SESSION/execute/sync" \
-H "Content-Type: application/json" \
-d "{\"script\": \"return $SCRIPT\", \"args\": []}" \
| jq -r '.value'
;;
screenshot)
PORT=$(get_port)
SESSION=$(cat "$SESSION_FILE" 2>/dev/null)
if [ -z "$SESSION" ]; then
echo "No session. Run: ./browser.sh start <url>"
exit 1
fi
OUTPUT="${2:-/tmp/screenshot.png}"
curl -s "http://localhost:$PORT/session/$SESSION/screenshot" \
| jq -r '.value' | base64 -d > "$OUTPUT"
echo "Screenshot saved to $OUTPUT"
;;
dark)
PORT=$(get_port)
SESSION=$(cat "$SESSION_FILE" 2>/dev/null)
if [ -z "$SESSION" ]; then
echo "No session. Run: ./browser.sh start <url>"
exit 1
fi
curl -s -X POST "http://localhost:$PORT/session/$SESSION/chromium/send_command" \
-H "Content-Type: application/json" \
-d '{"cmd": "Emulation.setEmulatedMedia", "params": {"features": [{"name": "prefers-color-scheme", "value": "dark"}]}}' > /dev/null
curl -s -X POST "http://localhost:$PORT/session/$SESSION/refresh" \
-H "Content-Type: application/json" > /dev/null
echo "Dark mode enabled"
;;
light)
PORT=$(get_port)
SESSION=$(cat "$SESSION_FILE" 2>/dev/null)
if [ -z "$SESSION" ]; then
echo "No session. Run: ./browser.sh start <url>"
exit 1
fi
curl -s -X POST "http://localhost:$PORT/session/$SESSION/chromium/send_command" \
-H "Content-Type: application/json" \
-d '{"cmd": "Emulation.setEmulatedMedia", "params": {"features": [{"name": "prefers-color-scheme", "value": "light"}]}}' > /dev/null
curl -s -X POST "http://localhost:$PORT/session/$SESSION/refresh" \
-H "Content-Type: application/json" > /dev/null
echo "Light mode enabled"
;;
mobile)
PORT=$(get_port)
SESSION=$(cat "$SESSION_FILE" 2>/dev/null)
if [ -z "$SESSION" ]; then
echo "No session. Run: ./browser.sh start <url>"
exit 1
fi
curl -s -X POST "http://localhost:$PORT/session/$SESSION/chromium/send_command" \
-H "Content-Type: application/json" \
-d '{"cmd": "Emulation.setDeviceMetricsOverride", "params": {"width": 375, "height": 812, "deviceScaleFactor": 1, "mobile": true}}' > /dev/null
curl -s -X POST "http://localhost:$PORT/session/$SESSION/refresh" \
-H "Content-Type: application/json" > /dev/null
echo "Mobile viewport set (375x812)"
;;
desktop)
PORT=$(get_port)
SESSION=$(cat "$SESSION_FILE" 2>/dev/null)
if [ -z "$SESSION" ]; then
echo "No session. Run: ./browser.sh start <url>"
exit 1
fi
curl -s -X POST "http://localhost:$PORT/session/$SESSION/chromium/send_command" \
-H "Content-Type: application/json" \
-d '{"cmd": "Emulation.clearDeviceMetricsOverride", "params": {}}' > /dev/null
curl -s -X POST "http://localhost:$PORT/session/$SESSION/refresh" \
-H "Content-Type: application/json" > /dev/null
echo "Desktop viewport restored (1400x900)"
;;
nav|goto)
PORT=$(get_port)
SESSION=$(cat "$SESSION_FILE" 2>/dev/null)
if [ -z "$SESSION" ]; then
echo "No session. Run: ./browser.sh start <url>"
exit 1
fi
URL="$2"
curl -s -X POST "http://localhost:$PORT/session/$SESSION/url" \
-H "Content-Type: application/json" \
-d "{\"url\": \"$URL\"}" > /dev/null
echo "Navigated to: $URL"
;;
stop)
PORT=$(get_port 2>/dev/null) || true
SESSION=$(cat "$SESSION_FILE" 2>/dev/null)
if [ -z "$SESSION" ]; then
echo "No active session"
exit 0
fi
if [ -n "$PORT" ]; then
curl -s -X DELETE "http://localhost:$PORT/session/$SESSION" > /dev/null
fi
rm -f "$SESSION_FILE"
echo "Session ended"
;;
kill)
if [ -f "$PORT_FILE" ]; then
PORT=$(cat "$PORT_FILE")
curl -s -X DELETE "http://localhost:$PORT/shutdown" 2>/dev/null || true
rm -f "$PORT_FILE" "$SESSION_FILE"
echo "ChromeDriver stopped"
else
echo "No chromedriver running"
fi
;;
*)
echo "Usage: ./browser.sh <command> [args]"
echo "Commands: chromedriver, start, js, screenshot, dark, light, mobile, desktop, nav, stop, kill"
;;
esac