/[scripts]/trunk/webthumb
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/webthumb

Parent Directory Parent Directory | Revision Log Revision Log


Revision 64 - (show annotations)
Wed Feb 6 23:45:58 2008 UTC (16 years, 2 months ago) by dpavlin
File size: 5184 byte(s)
Xnest doesn't work with xdotool, so moved to Xephyr

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 # import (part of ImageMagick package)
27 # xdotool (much more useful than xwit for this purpose)
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 # turn locally visible 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
44 BROWSER=mozilla-firefox
45 XSERVER=Xvfb
46 URL=$1
47 FILE=${2:-screenshot.jpg}
48
49 FRAMESET="`mktemp`.html"
50 PROFILE_DIR=`mktemp -d`
51 PORT=8888
52
53 LOCAL_DISPLAY=$DISPLAY
54
55 if [ -z "$URL" ] ; then
56 echo "usage: $0 http://url.to.capture [screenshot.jpg]"
57 exit 1
58 fi
59
60 if [ -z "`which xdotool`" ] ; then
61 echo "$0 need xdotool from http://www.semicomplete.com/blog/projects/xdotool/"
62 exit 1
63 fi
64
65 if [ -z "`which $XSERVER`" ] ; then
66 echo "$0 really need $XSERVER to operate. please install it."
67 exit 1
68 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 "
77 if HEAD $URL >/dev/null ; then
78 echo "ok"
79 else
80 echo "FAILED"
81 exit 1
82 fi
83
84 DISPLAY_PORT=6042
85
86 while netstat -ln | grep ":$DISPLAY_PORT " >/dev/null ; do
87 DISPLAY_PORT=`expr $DISPLAY_PORT + 1`
88 done
89
90 DISPLAY=:`expr $DISPLAY_PORT - 6000`
91
92 echo "using DISPLAY=$DISPLAY"
93
94 if [ "$DEBUG" == 1 ] ; then
95 echo "using locally visible debug server on $LOCAL_DISPLAY"
96 DISPLAY=$LOCAL_DISPLAY Xephyr -ac -screen ${W}x${H}x24 $DISPLAY 2>/dev/null &
97 else
98 echo "starting $XSERVER"
99 $XSERVER -ac -screen 0 ${W}x${H}x24 $DISPLAY 2>/dev/null &
100 fi
101
102 if [ -z "$!" ] ; then
103 echo "ABORT: can't start X server!"
104 exit
105 fi
106 XSERVER_PID=$!
107
108 echo "X server pid $XSERVER_PID"
109
110 function kill_x_server() {
111 echo "Killing server $XSERVER_PID"
112 kill $XSERVER_PID
113 rm -f $FRAMESET
114 rm -fr $PROFILE_DIR
115 trap '' EXIT
116 exit 1
117 }
118 trap 'kill_x_server' INT QUIT TERM SEGV EXIT
119
120 # create frameset to load site and after site is loaded trigger this script
121 cat > $FRAMESET <<END_OF_FRAMESET
122 <html>
123 <head>
124 <frameset rows="0,*" border=0 frameborder=no framespacing=0 onload="window.frames['trigger_frame'].window.location.replace('http://localhost:$PORT');">
125 <frame src="about:blank" name="trigger_frame" scrolling=no marginwidth=0 marginheight=0>
126 <frame src="$URL" name="page_frame" scrolling=no>
127 </frameset>
128 </head>
129 </html>
130 END_OF_FRAMESET
131
132 echo "Using frameset html $FRAMESET"
133
134 echo "preview available with: xwd -display $DISPLAY -root | xwud"
135
136 echo "making 'Screenshot' profile in $PROFILE_DIR"
137 $BROWSER -CreateProfile "Screenshot $PROFILE_DIR" 2>/dev/null | grep Success
138
139 echo "launching browser $BROWSER with $URL"
140 $BROWSER -P Screenshot -width $W -height $H -safemode $FRAMESET 2>/dev/null &
141 BROWSER_PID=$!
142
143 function kill_browser() {
144 echo "Killing browser $BROWSER_PID"
145 kill $BROWSER_PID
146 echo "Killing server $XSERVER_PID"
147 kill $XSERVER_PID
148 rm -f $FRAMESET
149 rm -fr $PROFILE_DIR
150 trap '' EXIT
151 exit 1
152 }
153 trap 'kill_browser' INT QUIT TERM SEGV EXIT
154
155 function ping_browser() {
156 echo -n "ping browser"
157 while ! ( $BROWSER -remote "ping();" 2>&1 ) >/dev/null ; do
158 RID=`xdotool search Restore 2>/dev/null`
159 if [ ! -z "$RID" ] ; then
160 echo -n "Esc"
161 xdotool focus $RID
162 xdotool key Escape
163 sleep 1
164 fi
165 echo -n "."
166 sleep 1
167 done
168 echo " OK"
169 }
170
171 ping_browser
172
173 $BROWSER -remote "openURL($FRAMESET)" 2>/dev/null
174
175 echo "waiting for on_load event from browser $BROWSER_PID for ${WAIT}s"
176
177 # there is hard-coded limit here:
178 # we will wait $WAIT sec for page to load and render
179
180 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!"
181
182 # get Mozilla Firefox window id (for resize)
183 WINDOW_ID=`xwininfo -display $DISPLAY -root -tree | grep gecko | cut -d\" -f1 | sort -n | head -1`
184
185 if [ -z "$WINDOW_ID" ] ; then
186 echo "can't find window with name 'Mozilla Firefox'"
187 exit 1
188 else
189 echo "working on firefox window $WINDOW_ID"
190 fi
191
192 xdotool search firefox
193
194 xdotool focus $WINDOW_ID
195 xdotool key F11
196
197 ping_browser
198
199 # try to deduce inside area of window
200
201 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`
202
203 if [ -z "$DUMP_ID" ] ; then
204 echo "can't find inside area of window. Using whole browser!"
205 DUMP_ID=$WINDOW_ID
206 fi
207
208 echo "saving window $DUMP_ID to $FILE"
209 if [ ! -z "$RESIZE" ] ; then
210 RESIZE="-geometry $RESIZE"
211 fi
212
213 import -window $DUMP_ID $RESIZE $FILE
214
215 if [ "$DEBUG" == 1 ] ; then
216 echo -n "press enter to exit! "
217 read
218 fi

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26