1 |
#!/bin/sh |
2 |
# |
3 |
# This script is based on work of mendel from |
4 |
# http://www.lafferty.ca/software/webshots/webshot |
5 |
# webshot: hackish thing to take a picture of a whole webpage. |
6 |
# |
7 |
# idea from sippey (http://www.sippey.com/2004/blog-snaps/) |
8 |
# inspiration from bradfitz |
9 |
# |
10 |
# usage: webshot url filename |
11 |
# |
12 |
# JPEG seems to be the best format. |
13 |
# |
14 |
# Modified by Dobrica Pavlinusic 2004-07-25 |
15 |
# - include signal grabbing |
16 |
# - dynamic vncserver display |
17 |
# - correct WindowID parsing |
18 |
# - wait for browser to load full page (useful on slow links) |
19 |
# |
20 |
# It will need follwing external utilities: |
21 |
# vncserver |
22 |
# mozilla-firefox |
23 |
# HEAD (part or LWP perl library, it can be commented out) |
24 |
# nc (netcat, to simulate web server) |
25 |
# xwininfo (standard part of X-clients) |
26 |
# xwit (not so standard command, but very useful) |
27 |
# import (part of ImageMagick package) |
28 |
# |
29 |
# It will create snapshot of following size |
30 |
W=1024 |
31 |
H=768 |
32 |
|
33 |
# and then resize it to (comment out to disable) |
34 |
#RESIZE=200x |
35 |
|
36 |
# some configurable paths |
37 |
BROWSER=mozilla-firefox |
38 |
XSERVER=Xvfb |
39 |
XWIT=xwit |
40 |
URL=$1 |
41 |
FILE=${2:-screenshot.jpg} |
42 |
|
43 |
FRAMESET="`mktemp`.html" |
44 |
PROFILE_DIR=`mktemp -d` |
45 |
PORT=8888 |
46 |
|
47 |
if [ -z "$URL" ] ; then |
48 |
echo "usage: $0 http://url.to.capture [screenshot.jpg]" |
49 |
exit 1 |
50 |
fi |
51 |
|
52 |
if [ -z "`which $XWIT`" ] ; then |
53 |
echo "$0 really need $XWIT to operate. please install it." |
54 |
exit 1 |
55 |
fi |
56 |
|
57 |
if [ -z "`which $XSERVER`" ] ; then |
58 |
echo "$0 really need $XSERVER to operate. please install it." |
59 |
exit 1 |
60 |
fi |
61 |
|
62 |
echo -n "testing URL $URL " |
63 |
if HEAD $URL >/dev/null ; then |
64 |
echo "ok" |
65 |
else |
66 |
echo "FAILED" |
67 |
exit 1 |
68 |
fi |
69 |
|
70 |
echo "starting X server $XSERVER" |
71 |
|
72 |
export DISPLAY=:42 |
73 |
|
74 |
$XSERVER -ac -screen 0 ${W}x${H}x24 $DISPLAY & |
75 |
# debug server |
76 |
#Xnest -display :0 -ac -geometry ${W}x${H} -depth 24 $DISPLAY & |
77 |
|
78 |
XSERVER_PID=$! |
79 |
echo "using pid $xserver_pid for X server" |
80 |
|
81 |
function kill_x_server() { |
82 |
kill $BROWSER_PID |
83 |
kill $XSERVER_PID |
84 |
rm -f $FRAMESET |
85 |
rm -fr $PROFILE_DIR |
86 |
} |
87 |
trap 'echo "QUIT!" >&2; kill_x_server; exit 1' INT QUIT TERM SEGV |
88 |
|
89 |
# create frameset to load site and after site is loaded trigger this script |
90 |
cat > $FRAMESET <<END_OF_FRAMESET |
91 |
<html> |
92 |
<head> |
93 |
<frameset rows="0,*" border=0 frameborder=no framespacing=0 onload="window.frames['trigger_frame'].window.location.replace('http://localhost:$PORT');"> |
94 |
<frame src="about:blank" name="trigger_frame" scrolling=no marginwidth=0 marginheight=0> |
95 |
<frame src="$URL" name="page_frame" scrolling=no> |
96 |
</frameset> |
97 |
</head> |
98 |
</html> |
99 |
END_OF_FRAMESET |
100 |
|
101 |
echo "Using frameset html $FRAMESET" |
102 |
|
103 |
echo "preview available with: xwd -display $DISPLAY -root | xwud" |
104 |
|
105 |
echo "making 'Screenshot' profile in $PROFILE_DIR" |
106 |
$BROWSER -CreateProfile "Screenshot $PROFILE_DIR" | grep Success |
107 |
|
108 |
echo "launching browser $BROWSER with $URL" |
109 |
$BROWSER -P Screenshot -width $W -height $H $FRAMESET & |
110 |
BROWSER_PID=$! |
111 |
|
112 |
echo "staring web server (wait for page to load)" |
113 |
|
114 |
# there is hard-coded limit here: |
115 |
# we will wait 60sec for page to load and render |
116 |
|
117 |
echo -e "HTTP/1.0 304 Not modified\r\n\r\n" | nc -l -w 60 -p $PORT >/dev/null |
118 |
|
119 |
# get Mozilla Firefox window id (for resize) |
120 |
WINDOW_ID=`xwininfo -display $DISPLAY -root -tree | grep gecko | cut -d\" -f1 | sort -n | head -1` |
121 |
|
122 |
if [ -z "$WINDOW_ID" ] ; then |
123 |
echo "can't find window with name 'Mozilla Firefox'" |
124 |
exit 1 |
125 |
fi |
126 |
|
127 |
# move window to foreground |
128 |
$XWIT -display $DISPLAY -id $WINDOW_ID -pop |
129 |
|
130 |
echo "resizing window $WINDOW_ID to maximum size" |
131 |
$XWIT -display $DISPLAY -id $WINDOW_ID -move 0 0 |
132 |
$XWIT -display $DISPLAY -id $WINDOW_ID -resize $W $H |
133 |
|
134 |
|
135 |
echo -n "wating for browser ping..." |
136 |
while ! ( $BROWSER -remote "ping();" 2>&1 ) >/dev/null ; do |
137 |
echo -n "." |
138 |
sleep 1 |
139 |
done |
140 |
echo |
141 |
|
142 |
# try to deduce inside area of window |
143 |
|
144 |
DUMP_ID=`xwininfo -display $DISPLAY -id $WINDOW_ID -tree | grep 0x | grep -v ${W}x${H} | grep ${W}x | head -1 | sed 's/^ *//' | cut -d' ' -f1` |
145 |
|
146 |
if [ -z "$DUMP_ID" ] ; then |
147 |
echo "can't find inside area of window. Using whole browser!" |
148 |
DUMP_ID=$WINDOW_ID |
149 |
fi |
150 |
|
151 |
echo "saving window $DUMP_ID to $FILE" |
152 |
if [ ! -z "$RESIZE" ] ; then |
153 |
RESIZE="-geometry $RESIZE" |
154 |
fi |
155 |
|
156 |
import -window $DUMP_ID $RESIZE $FILE |
157 |
|
158 |
kill_x_server |
159 |
|