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=1024 |
W=1024 |
33 |
# and then resize it to (comment out to disable) |
# and then resize it to (comment out to disable) |
34 |
#RESIZE=200x |
#RESIZE=200x |
35 |
|
|
36 |
|
# turn locally visible Xnest server to watch progress |
37 |
|
DEBUG=0 |
38 |
|
test ! -z "$3" && DEBUG=1 |
39 |
|
|
40 |
|
# wait for $WAIT seconds for page to load |
41 |
|
WAIT=30 |
42 |
|
|
43 |
# some configurable paths |
# some configurable paths |
44 |
BROWSER=mozilla-firefox |
BROWSER=mozilla-firefox |
45 |
XSERVER=Xvfb |
XSERVER=Xvfb |
|
XWIT=xwit |
|
46 |
URL=$1 |
URL=$1 |
47 |
FILE=${2:-screenshot.jpg} |
FILE=${2:-screenshot.jpg} |
48 |
|
|
50 |
PROFILE_DIR=`mktemp -d` |
PROFILE_DIR=`mktemp -d` |
51 |
PORT=8888 |
PORT=8888 |
52 |
|
|
53 |
|
LOCAL_DISPLAY=$DISPLAY |
54 |
|
|
55 |
if [ -z "$URL" ] ; then |
if [ -z "$URL" ] ; then |
56 |
echo "usage: $0 http://url.to.capture [screenshot.jpg]" |
echo "usage: $0 http://url.to.capture [screenshot.jpg]" |
57 |
exit 1 |
exit 1 |
58 |
fi |
fi |
59 |
|
|
60 |
if [ -z "`which $XWIT`" ] ; then |
if [ -z "`which xdotool`" ] ; then |
61 |
echo "$0 really need $XWIT to operate. please install it." |
echo "$0 need xdotool from http://www.semicomplete.com/blog/projects/xdotool/" |
62 |
exit 1 |
exit 1 |
63 |
fi |
fi |
64 |
|
|
67 |
exit 1 |
exit 1 |
68 |
fi |
fi |
69 |
|
|
70 |
|
while netstat -ln | grep ":$PORT " >/dev/null ; do |
71 |
|
PORT=`expr $PORT + 1` |
72 |
|
done |
73 |
|
|
74 |
|
echo "### using port $PORT" |
75 |
|
|
76 |
echo -n "testing URL $URL " |
echo -n "testing URL $URL " |
77 |
if HEAD $URL >/dev/null ; then |
if HEAD $URL >/dev/null ; then |
78 |
echo "ok" |
echo "ok" |
85 |
|
|
86 |
export DISPLAY=:42 |
export DISPLAY=:42 |
87 |
|
|
88 |
$XSERVER -ac -screen 0 ${W}x${H}x24 $DISPLAY & |
|
89 |
# debug server |
if [ "$DEBUG" == 1 ] ; then |
90 |
#Xnest -display :0 -ac -geometry ${W}x${H} -depth 24 $DISPLAY & |
echo "using locally visible debug server on $LOCAL_DISPLAY" |
91 |
|
Xnest -display $LOCAL_DISPLAY -ac -geometry ${W}x${H} -depth 24 $DISPLAY 2>/dev/null & |
92 |
|
else |
93 |
|
echo "starting $XSERVER" |
94 |
|
$XSERVER -ac -screen 0 ${W}x${H}x24 $DISPLAY 2>/dev/null & |
95 |
|
fi |
96 |
|
|
97 |
XSERVER_PID=$! |
XSERVER_PID=$! |
98 |
echo "using pid $xserver_pid for X server" |
echo "X server pid $XSERVER_PID" |
99 |
|
|
100 |
function kill_x_server() { |
function kill_x_server() { |
101 |
kill $BROWSER_PID |
echo "Killing server $XSERVER_PID" |
102 |
kill $XSERVER_PID |
kill $XSERVER_PID |
103 |
rm -f $FRAMESET |
rm -f $FRAMESET |
104 |
rm -fr $PROFILE_DIR |
rm -fr $PROFILE_DIR |
105 |
|
trap '' EXIT |
106 |
|
exit 1 |
107 |
} |
} |
108 |
trap 'echo "QUIT!" >&2; kill_x_server; exit 1' INT QUIT TERM SEGV |
trap 'kill_x_server' INT QUIT TERM SEGV EXIT |
109 |
|
|
110 |
# 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 |
111 |
cat > $FRAMESET <<END_OF_FRAMESET |
cat > $FRAMESET <<END_OF_FRAMESET |
124 |
echo "preview available with: xwd -display $DISPLAY -root | xwud" |
echo "preview available with: xwd -display $DISPLAY -root | xwud" |
125 |
|
|
126 |
echo "making 'Screenshot' profile in $PROFILE_DIR" |
echo "making 'Screenshot' profile in $PROFILE_DIR" |
127 |
$BROWSER -CreateProfile "Screenshot $PROFILE_DIR" | grep Success |
$BROWSER -CreateProfile "Screenshot $PROFILE_DIR" 2>/dev/null | grep Success |
128 |
|
|
129 |
echo "launching browser $BROWSER with $URL" |
echo "launching browser $BROWSER with $URL" |
130 |
$BROWSER -P Screenshot -width $W -height $H $FRAMESET & |
$BROWSER -P Screenshot -width $W -height $H -safemode $FRAMESET 2>/dev/null & |
131 |
BROWSER_PID=$! |
BROWSER_PID=$! |
132 |
|
|
133 |
echo "staring web server (wait for page to load)" |
function kill_browser() { |
134 |
|
echo "Killing browser $BROWSER_PID" |
135 |
|
kill $BROWSER_PID |
136 |
|
echo "Killing server $XSERVER_PID" |
137 |
|
kill $XSERVER_PID |
138 |
|
rm -f $FRAMESET |
139 |
|
rm -fr $PROFILE_DIR |
140 |
|
trap '' EXIT |
141 |
|
exit 1 |
142 |
|
} |
143 |
|
trap 'kill_browser' INT QUIT TERM SEGV EXIT |
144 |
|
|
145 |
|
function ping_browser() { |
146 |
|
echo -n "ping browser" |
147 |
|
while ! ( $BROWSER -remote "ping();" 2>&1 ) >/dev/null ; do |
148 |
|
RID=`xdotool search Restore 2>/dev/null` |
149 |
|
if [ ! -z "$RID" ] ; then |
150 |
|
echo -n "Esc" |
151 |
|
xdotool focus $RID |
152 |
|
xdotool key Escape |
153 |
|
sleep 1 |
154 |
|
fi |
155 |
|
echo -n "." |
156 |
|
sleep 1 |
157 |
|
done |
158 |
|
echo " OK" |
159 |
|
} |
160 |
|
|
161 |
|
ping_browser |
162 |
|
|
163 |
|
$BROWSER -remote "openURL($FRAMESET)" 2>/dev/null |
164 |
|
|
165 |
|
echo "waiting for on_load event from browser $BROWSER_PID for ${WAIT}s" |
166 |
|
|
167 |
# there is hard-coded limit here: |
# there is hard-coded limit here: |
168 |
# we will wait 60sec for page to load and render |
# we will wait $WAIT sec for page to load and render |
169 |
|
|
170 |
echo -e "HTTP/1.0 304 Not modified\r\n\r\n" | nc -l -w 60 -p $PORT >/dev/null |
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!" |
171 |
|
|
172 |
# get Mozilla Firefox window id (for resize) |
# get Mozilla Firefox window id (for resize) |
173 |
WINDOW_ID=`xwininfo -display $DISPLAY -root -tree | grep gecko | cut -d\" -f1 | sort -n | head -1` |
WINDOW_ID=`xwininfo -display $DISPLAY -root -tree | grep gecko | cut -d\" -f1 | sort -n | head -1` |
175 |
if [ -z "$WINDOW_ID" ] ; then |
if [ -z "$WINDOW_ID" ] ; then |
176 |
echo "can't find window with name 'Mozilla Firefox'" |
echo "can't find window with name 'Mozilla Firefox'" |
177 |
exit 1 |
exit 1 |
178 |
|
else |
179 |
|
echo "working on firefox window $WINDOW_ID" |
180 |
fi |
fi |
181 |
|
|
182 |
# move window to foreground |
xdotool search firefox |
|
$XWIT -display $DISPLAY -id $WINDOW_ID -pop |
|
|
|
|
|
echo "resizing window $WINDOW_ID to maximum size" |
|
|
$XWIT -display $DISPLAY -id $WINDOW_ID -move 0 0 |
|
|
$XWIT -display $DISPLAY -id $WINDOW_ID -resize $W $H |
|
183 |
|
|
184 |
|
xdotool focus $WINDOW_ID |
185 |
|
xdotool key F11 |
186 |
|
|
187 |
echo -n "wating for browser ping..." |
ping_browser |
|
while ! ( $BROWSER -remote "ping();" 2>&1 ) >/dev/null ; do |
|
|
echo -n "." |
|
|
sleep 1 |
|
|
done |
|
|
echo |
|
188 |
|
|
189 |
# try to deduce inside area of window |
# try to deduce inside area of window |
190 |
|
|
202 |
|
|
203 |
import -window $DUMP_ID $RESIZE $FILE |
import -window $DUMP_ID $RESIZE $FILE |
204 |
|
|
205 |
kill_x_server |
if [ "$DEBUG" == 1 ] ; then |
206 |
|
echo -n "press enter to exit! " |
207 |
|
read |
208 |
|
fi |