/[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 63 - (show annotations)
Wed Feb 6 23:34:35 2008 UTC (13 years, 8 months ago) by dpavlin
File size: 4994 byte(s)
rest of changes

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 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
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 echo "starting X server $XSERVER"
85
86 export DISPLAY=:42
87
88
89 if [ "$DEBUG" == 1 ] ; then
90 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=$!
98 echo "X server pid $XSERVER_PID"
99
100 function kill_x_server() {
101 echo "Killing server $XSERVER_PID"
102 kill $XSERVER_PID
103 rm -f $FRAMESET
104 rm -fr $PROFILE_DIR
105 trap '' EXIT
106 exit 1
107 }
108 trap 'kill_x_server' INT QUIT TERM SEGV EXIT
109
110 # create frameset to load site and after site is loaded trigger this script
111 cat > $FRAMESET <<END_OF_FRAMESET
112 <html>
113 <head>
114 <frameset rows="0,*" border=0 frameborder=no framespacing=0 onload="window.frames['trigger_frame'].window.location.replace('http://localhost:$PORT');">
115 <frame src="about:blank" name="trigger_frame" scrolling=no marginwidth=0 marginheight=0>
116 <frame src="$URL" name="page_frame" scrolling=no>
117 </frameset>
118 </head>
119 </html>
120 END_OF_FRAMESET
121
122 echo "Using frameset html $FRAMESET"
123
124 echo "preview available with: xwd -display $DISPLAY -root | xwud"
125
126 echo "making 'Screenshot' profile in $PROFILE_DIR"
127 $BROWSER -CreateProfile "Screenshot $PROFILE_DIR" 2>/dev/null | grep Success
128
129 echo "launching browser $BROWSER with $URL"
130 $BROWSER -P Screenshot -width $W -height $H -safemode $FRAMESET 2>/dev/null &
131 BROWSER_PID=$!
132
133 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:
168 # 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 $WAIT -p $PORT >/dev/null || echo "Timeout after $WAIT sec!"
171
172 # get Mozilla Firefox window id (for resize)
173 WINDOW_ID=`xwininfo -display $DISPLAY -root -tree | grep gecko | cut -d\" -f1 | sort -n | head -1`
174
175 if [ -z "$WINDOW_ID" ] ; then
176 echo "can't find window with name 'Mozilla Firefox'"
177 exit 1
178 else
179 echo "working on firefox window $WINDOW_ID"
180 fi
181
182 xdotool search firefox
183
184 xdotool focus $WINDOW_ID
185 xdotool key F11
186
187 ping_browser
188
189 # try to deduce inside area of window
190
191 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`
192
193 if [ -z "$DUMP_ID" ] ; then
194 echo "can't find inside area of window. Using whole browser!"
195 DUMP_ID=$WINDOW_ID
196 fi
197
198 echo "saving window $DUMP_ID to $FILE"
199 if [ ! -z "$RESIZE" ] ; then
200 RESIZE="-geometry $RESIZE"
201 fi
202
203 import -window $DUMP_ID $RESIZE $FILE
204
205 if [ "$DEBUG" == 1 ] ; then
206 echo -n "press enter to exit! "
207 read
208 fi

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26