/[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 61 - (show annotations)
Wed Feb 6 21:26:10 2008 UTC (14 years, 3 months ago) by dpavlin
File size: 4710 byte(s)
- if started with third param (anything) it will turn visible debug Xnest on
- hush some STDERR from X server and firefox
- cleanup after exit

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 # 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 XWIT=xwit
47 URL=$1
48 FILE=${2:-screenshot.jpg}
49
50 FRAMESET="`mktemp`.html"
51 PROFILE_DIR=`mktemp -d`
52 PORT=8888
53
54 LOCAL_DISPLAY=$DISPLAY
55
56 if [ -z "$URL" ] ; then
57 echo "usage: $0 http://url.to.capture [screenshot.jpg]"
58 exit 1
59 fi
60
61 if [ -z "`which $XWIT`" ] ; then
62 echo "$0 really need $XWIT to operate. please install it."
63 exit 1
64 fi
65
66 if [ -z "`which $XSERVER`" ] ; then
67 echo "$0 really need $XSERVER to operate. please install it."
68 exit 1
69 fi
70
71 while netstat -ln | grep ":$PORT " >/dev/null ; do
72 PORT=`expr $PORT + 1`
73 done
74
75 echo "### using port $PORT"
76
77 echo -n "testing URL $URL "
78 if HEAD $URL >/dev/null ; then
79 echo "ok"
80 else
81 echo "FAILED"
82 exit 1
83 fi
84
85 echo "starting X server $XSERVER"
86
87 export DISPLAY=:42
88
89
90 if [ "$DEBUG" == 1 ] ; then
91 echo "Using locally visible debug server on $LOCAL_DISPLAY"
92 Xnest -display $LOCAL_DISPLAY -ac -geometry ${W}x${H} -depth 24 $DISPLAY 2>/dev/null &
93 else
94 $XSERVER -ac -screen 0 ${W}x${H}x24 $DISPLAY 2>/dev/null &
95 fi
96
97 XSERVER_PID=$!
98 echo "using pid $XSERVER_PID for X server"
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 exit 1
106 }
107 trap 'kill_x_server' INT QUIT TERM SEGV EXIT
108
109 # create frameset to load site and after site is loaded trigger this script
110 cat > $FRAMESET <<END_OF_FRAMESET
111 <html>
112 <head>
113 <frameset rows="0,*" border=0 frameborder=no framespacing=0 onload="window.frames['trigger_frame'].window.location.replace('http://localhost:$PORT');">
114 <frame src="about:blank" name="trigger_frame" scrolling=no marginwidth=0 marginheight=0>
115 <frame src="$URL" name="page_frame" scrolling=no>
116 </frameset>
117 </head>
118 </html>
119 END_OF_FRAMESET
120
121 echo "Using frameset html $FRAMESET"
122
123 echo "preview available with: xwd -display $DISPLAY -root | xwud"
124
125 echo "making 'Screenshot' profile in $PROFILE_DIR"
126 $BROWSER -CreateProfile "Screenshot $PROFILE_DIR" 2>/dev/null | grep Success
127
128 echo "launching browser $BROWSER with $URL"
129 $BROWSER -P Screenshot -width $W -height $H $FRAMESET 2>/dev/null &
130 BROWSER_PID=$!
131
132 function kill_browser() {
133 echo "Killing browser $BROWSER_PID"
134 kill $BROWSER_PID
135 echo "Killing server $XSERVER_PID"
136 kill $XSERVER_PID
137 rm -f $FRAMESET
138 rm -fr $PROFILE_DIR
139 exit 1
140 }
141 trap 'kill_browser' INT QUIT TERM SEGV EXIT
142
143 echo "waiting for on_load event from browser $BROWSER_PID for ${WAIT}s"
144
145 # there is hard-coded limit here:
146 # we will wait $WAIT sec for page to load and render
147
148 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!"
149
150 # get Mozilla Firefox window id (for resize)
151 WINDOW_ID=`xwininfo -display $DISPLAY -root -tree | grep gecko | cut -d\" -f1 | sort -n | head -1`
152
153 if [ -z "$WINDOW_ID" ] ; then
154 echo "can't find window with name 'Mozilla Firefox'"
155 exit 1
156 fi
157
158 # move window to foreground
159 $XWIT -display $DISPLAY -id $WINDOW_ID -pop
160
161 echo "resizing window $WINDOW_ID to maximum size"
162 $XWIT -display $DISPLAY -id $WINDOW_ID -move 0 0
163 $XWIT -display $DISPLAY -id $WINDOW_ID -resize $W $H
164
165
166 echo -n "wating for browser ping..."
167 while ! ( $BROWSER -remote "ping();" 2>&1 ) >/dev/null ; do
168 echo -n "."
169 sleep 1
170 done
171 echo
172
173 # try to deduce inside area of window
174
175 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`
176
177 if [ -z "$DUMP_ID" ] ; then
178 echo "can't find inside area of window. Using whole browser!"
179 DUMP_ID=$WINDOW_ID
180 fi
181
182 echo "saving window $DUMP_ID to $FILE"
183 if [ ! -z "$RESIZE" ] ; then
184 RESIZE="-geometry $RESIZE"
185 fi
186
187 import -window $DUMP_ID $RESIZE $FILE
188

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26