/[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 65 - (show annotations)
Wed Feb 6 23:59:09 2008 UTC (13 years, 5 months ago) by dpavlin
File size: 5232 byte(s)
- drop down to 8bpp if in debug mode
- first maximize browser, then load page (speedup)

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 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 # wait for $WAIT seconds for page to load
46 WAIT=30
47
48 # some configurable paths
49 BROWSER=mozilla-firefox
50 XSERVER=Xvfb
51 URL=$1
52 FILE=${2:-screenshot.jpg}
53
54 FRAMESET="`mktemp`.html"
55 PROFILE_DIR=`mktemp -d`
56 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 "
82 if HEAD $URL >/dev/null ; then
83 echo "ok"
84 else
85 echo "FAILED"
86 exit 1
87 fi
88
89 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 echo "X server pid $XSERVER_PID"
114
115 function kill_x_server() {
116 echo "Killing server $XSERVER_PID"
117 kill $XSERVER_PID
118 rm -f $FRAMESET
119 rm -fr $PROFILE_DIR
120 trap '' EXIT
121 exit 1
122 }
123 trap 'kill_x_server' INT QUIT TERM SEGV EXIT
124
125 # create frameset to load site and after site is loaded trigger this script
126 cat > $FRAMESET <<END_OF_FRAMESET
127 <html>
128 <head>
129 <frameset rows="0,*" border=0 frameborder=no framespacing=0 onload="window.frames['trigger_frame'].window.location.replace('http://localhost:$PORT');">
130 <frame src="about:blank" name="trigger_frame" scrolling=no marginwidth=0 marginheight=0>
131 <frame src="$URL" name="page_frame" scrolling=no>
132 </frameset>
133 </head>
134 </html>
135 END_OF_FRAMESET
136
137 echo "Using frameset html $FRAMESET"
138
139 echo "preview available with: xwd -display $DISPLAY -root | xwud"
140
141 echo "making 'Screenshot' profile in $PROFILE_DIR"
142 $BROWSER -CreateProfile "Screenshot $PROFILE_DIR" 2>/dev/null | grep Success
143
144 echo "launching browser $BROWSER with $URL"
145 $BROWSER -P Screenshot -width $W -height $H -safemode $FRAMESET 2>/dev/null &
146 BROWSER_PID=$!
147
148 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 function ping_browser() {
161 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 ping_browser
177
178 # get Mozilla Firefox window id (for resize)
179 WINDOW_ID=`xwininfo -display $DISPLAY -root -tree | grep gecko | cut -d\" -f1 | sort -n | head -1`
180
181 if [ -z "$WINDOW_ID" ] ; then
182 echo "can't find window with name 'Mozilla Firefox'"
183 exit 1
184 else
185 echo "working on firefox window $WINDOW_ID"
186 fi
187
188 xdotool search firefox
189
190 xdotool focus $WINDOW_ID
191 xdotool key F11
192
193 ping_browser
194
195 $BROWSER -remote "openURL($FRAMESET)" 2>/dev/null
196
197 echo "waiting for on_load event from browser $BROWSER_PID for ${WAIT}s"
198
199 # there is hard-coded limit here:
200 # 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
205
206 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`
207
208 if [ -z "$DUMP_ID" ] ; then
209 echo "can't find inside area of window. Using whole browser!"
210 DUMP_ID=$WINDOW_ID
211 fi
212
213 echo "saving window $DUMP_ID to $FILE"
214 if [ ! -z "$RESIZE" ] ; then
215 RESIZE="-geometry $RESIZE"
216 fi
217
218 import -window $DUMP_ID $RESIZE $FILE
219
220 if [ "$DEBUG" == 1 ] ; then
221 echo -n "press enter to exit! "
222 read
223 fi

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26