23 |
# HEAD (part or LWP perl library, it can be commented out) |
# HEAD (part or LWP perl library, it can be commented out) |
24 |
# nc (netcat, to simulate web server) |
# nc (netcat, to simulate web server) |
25 |
# xwininfo (standard part of X-clients) |
# xwininfo (standard part of X-clients) |
|
# xwit (not so standard command, but very useful) |
|
26 |
# import (part of ImageMagick package) |
# import (part of ImageMagick package) |
27 |
|
# xdotool (much more useful than xwit for this purpose) |
28 |
# |
# |
29 |
# It will create snapshot of following size |
# It will create snapshot of following size |
30 |
W=800 |
W=1024 |
31 |
H=800 |
H=768 |
32 |
|
D=24 |
33 |
|
|
34 |
|
# and then resize it to (comment out to disable) |
35 |
|
#RESIZE=200x |
36 |
|
|
37 |
|
# turn locally visible server to watch progress |
38 |
|
DEBUG=0 |
39 |
|
if [ ! -z "$3" ] ; then |
40 |
|
echo "DEBUG turned on" |
41 |
|
DEBUG=1 |
42 |
|
D=8 |
43 |
|
fi |
44 |
|
|
45 |
# and then resize it to |
# wait for $WAIT seconds for page to load |
46 |
RESIZE=200x |
WAIT=30 |
47 |
|
|
48 |
# some configurable paths |
# some configurable paths |
49 |
BROWSER=mozilla-firefox |
BROWSER=mozilla-firefox |
50 |
XSERVER=vncserver |
XSERVER=Xvfb |
51 |
URL=$1 |
URL=$1 |
52 |
FILE=${2:-screenshot.jpg} |
FILE=${2:-screenshot.jpg} |
53 |
|
|
54 |
FRAMESET="`mktemp`.html" |
FRAMESET="`mktemp`.html" |
55 |
|
PROFILE_DIR=`mktemp -d` |
56 |
PORT=8888 |
PORT=8888 |
57 |
|
|
58 |
|
LOCAL_DISPLAY=$DISPLAY |
59 |
|
|
60 |
|
if [ -z "$URL" ] ; then |
61 |
|
echo "usage: $0 http://url.to.capture [screenshot.jpg]" |
62 |
|
exit 1 |
63 |
|
fi |
64 |
|
|
65 |
|
if [ -z "`which xdotool`" ] ; then |
66 |
|
echo "$0 need xdotool from http://www.semicomplete.com/blog/projects/xdotool/" |
67 |
|
exit 1 |
68 |
|
fi |
69 |
|
|
70 |
|
if [ -z "`which $XSERVER`" ] ; then |
71 |
|
echo "$0 really need $XSERVER to operate. please install it." |
72 |
|
exit 1 |
73 |
|
fi |
74 |
|
|
75 |
|
while netstat -ln | grep ":$PORT " >/dev/null ; do |
76 |
|
PORT=`expr $PORT + 1` |
77 |
|
done |
78 |
|
|
79 |
|
echo "### using port $PORT" |
80 |
|
|
81 |
echo -n "testing URL $URL " |
echo -n "testing URL $URL " |
82 |
if HEAD $URL >/dev/null ; then |
if HEAD $URL >/dev/null ; then |
83 |
echo "ok" |
echo "ok" |
86 |
exit 1 |
exit 1 |
87 |
fi |
fi |
88 |
|
|
89 |
echo "starting vnc X server" |
DISPLAY_PORT=6042 |
90 |
|
|
91 |
|
while netstat -ln | grep ":$DISPLAY_PORT " >/dev/null ; do |
92 |
|
DISPLAY_PORT=`expr $DISPLAY_PORT + 1` |
93 |
|
done |
94 |
|
|
95 |
|
DISPLAY=:`expr $DISPLAY_PORT - 6000` |
96 |
|
|
97 |
|
echo "using DISPLAY=$DISPLAY" |
98 |
|
|
99 |
|
if [ "$DEBUG" == 1 ] ; then |
100 |
|
echo "using locally visible debug server on $LOCAL_DISPLAY" |
101 |
|
DISPLAY=$LOCAL_DISPLAY Xephyr -ac -screen ${W}x${H}x${D} $DISPLAY 2>/dev/null & |
102 |
|
else |
103 |
|
echo "starting $XSERVER" |
104 |
|
$XSERVER -ac -screen 0 ${W}x${H}x${D} $DISPLAY 2>/dev/null & |
105 |
|
fi |
106 |
|
|
107 |
|
if [ -z "$!" ] ; then |
108 |
|
echo "ABORT: can't start X server!" |
109 |
|
exit |
110 |
|
fi |
111 |
|
XSERVER_PID=$! |
112 |
|
|
113 |
export DISPLAY=`$XSERVER -geometry ${W}x${H} -depth 24 2>&1 \ |
echo "X server pid $XSERVER_PID" |
|
| grep : | head -1 | cut -d: -f2 | sed 's/^/:/'` |
|
114 |
|
|
115 |
function kill_vnc() { |
function kill_x_server() { |
116 |
vncserver -kill $DISPLAY |
echo "Killing server $XSERVER_PID" |
117 |
|
kill $XSERVER_PID |
118 |
rm -f $FRAMESET |
rm -f $FRAMESET |
119 |
|
rm -fr $PROFILE_DIR |
120 |
|
trap '' EXIT |
121 |
|
exit 1 |
122 |
} |
} |
123 |
trap 'echo "QUIT!" >&2; kill_vnc; exit 1' INT QUIT TERM SEGV |
trap 'kill_x_server' INT QUIT TERM SEGV EXIT |
124 |
|
|
125 |
# create frameset to load site and after site is loaded trigger this script |
# create frameset to load site and after site is loaded trigger this script |
126 |
cat > $FRAMESET <<END_OF_FRAMESET |
cat > $FRAMESET <<END_OF_FRAMESET |
136 |
|
|
137 |
echo "Using frameset html $FRAMESET" |
echo "Using frameset html $FRAMESET" |
138 |
|
|
139 |
echo "preview available with: vncviewer $DISPLAY" |
echo "preview available with: xwd -display $DISPLAY -root | xwud" |
140 |
|
|
141 |
echo "making 'Screenshot' profile" |
echo "making 'Screenshot' profile in $PROFILE_DIR" |
142 |
$BROWSER -CreateProfile Screenshot | grep Success |
$BROWSER -CreateProfile "Screenshot $PROFILE_DIR" 2>/dev/null | grep Success |
143 |
|
|
144 |
echo "launching browser $BROWSER with $URL" |
echo "launching browser $BROWSER with $URL" |
145 |
$BROWSER -id Screenshot -P Screenshot -width $W -height $H $FRAMESET 2>&1 >/dev/null & |
$BROWSER -P Screenshot -width $W -height $H -safemode $FRAMESET 2>/dev/null & |
146 |
|
BROWSER_PID=$! |
147 |
|
|
148 |
echo "staring web server (wait for page to load)" |
function kill_browser() { |
149 |
|
echo "Killing browser $BROWSER_PID" |
150 |
|
kill $BROWSER_PID |
151 |
|
echo "Killing server $XSERVER_PID" |
152 |
|
kill $XSERVER_PID |
153 |
|
rm -f $FRAMESET |
154 |
|
rm -fr $PROFILE_DIR |
155 |
|
trap '' EXIT |
156 |
|
exit 1 |
157 |
|
} |
158 |
|
trap 'kill_browser' INT QUIT TERM SEGV EXIT |
159 |
|
|
160 |
# there is hard-coded limit here: |
function ping_browser() { |
161 |
# we will wait 60sec for page to load and render |
echo -n "ping browser" |
162 |
|
while ! ( $BROWSER -remote "ping();" 2>&1 ) >/dev/null ; do |
163 |
|
RID=`xdotool search Restore 2>/dev/null` |
164 |
|
if [ ! -z "$RID" ] ; then |
165 |
|
echo -n "Esc" |
166 |
|
xdotool focus $RID |
167 |
|
xdotool key Escape |
168 |
|
sleep 1 |
169 |
|
fi |
170 |
|
echo -n "." |
171 |
|
sleep 1 |
172 |
|
done |
173 |
|
echo " OK" |
174 |
|
} |
175 |
|
|
176 |
echo -e "HTTP/1.0 304 Not modified\r\n\r\n" | nc -l -w 60 -p $PORT >/dev/null |
ping_browser |
177 |
|
|
178 |
# get Mozilla Firefox window id (for resize) |
# get Mozilla Firefox window id (for resize) |
179 |
|
WINDOW_ID=`xwininfo -display $DISPLAY -root -tree | grep gecko | cut -d\" -f1 | sort -n | head -1` |
|
WINDOW_ID=`xwininfo -display $DISPLAY -name "Mozilla Firefox" | grep "Window id:" | cut -d" " -f4` |
|
180 |
|
|
181 |
if [ -z "$WINDOW_ID" ] ; then |
if [ -z "$WINDOW_ID" ] ; then |
182 |
echo "can't find window with name 'Mozilla Firefox'" |
echo "can't find window with name 'Mozilla Firefox'" |
183 |
exit 1 |
exit 1 |
184 |
|
else |
185 |
|
echo "working on firefox window $WINDOW_ID" |
186 |
fi |
fi |
187 |
|
|
188 |
|
xdotool search firefox |
189 |
|
|
190 |
echo "resizing window $WINDOW_ID to maximum size" |
xdotool focus $WINDOW_ID |
191 |
xwit -display $DISPLAY -id $WINDOW_ID -resize $W $H |
xdotool key F11 |
192 |
|
|
193 |
|
ping_browser |
194 |
|
|
195 |
echo -n "wating for browser ping..." |
$BROWSER -remote "openURL($FRAMESET)" 2>/dev/null |
196 |
while ! ( $BROWSER -remote "ping();" 2>&1 ) >/dev/null ; do |
|
197 |
echo -n "." |
echo "waiting for on_load event from browser $BROWSER_PID for ${WAIT}s" |
198 |
sleep 1 |
|
199 |
done |
# there is hard-coded limit here: |
200 |
echo |
# we will wait $WAIT sec for page to load and render |
201 |
|
|
202 |
|
echo -e "HTTP/1.0 304 Not modified\r\n\r\n" | nc -l -w $WAIT -p $PORT >/dev/null || echo "Timeout after $WAIT sec!" |
203 |
|
|
204 |
# try to deduce inside area of window |
# try to deduce inside area of window |
205 |
|
|
211 |
fi |
fi |
212 |
|
|
213 |
echo "saving window $DUMP_ID to $FILE" |
echo "saving window $DUMP_ID to $FILE" |
214 |
import -window $DUMP_ID -geometry $RESIZE $FILE |
if [ ! -z "$RESIZE" ] ; then |
215 |
|
RESIZE="-geometry $RESIZE" |
216 |
|
fi |
217 |
|
|
218 |
kill_vnc |
import -window $DUMP_ID $RESIZE $FILE |
219 |
|
|
220 |
|
if [ "$DEBUG" == 1 ] ; then |
221 |
|
echo -n "press enter to exit! " |
222 |
|
read |
223 |
|
fi |