/[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

Annotation of /trunk/webthumb

Parent Directory Parent Directory | Revision Log Revision Log


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

1 dpavlin 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 dpavlin 62 # xdotool (much more useful than xwit for this purpose)
28 dpavlin 1 #
29     # It will create snapshot of following size
30 dpavlin 2 W=1024
31     H=768
32 dpavlin 1
33 dpavlin 2 # and then resize it to (comment out to disable)
34     #RESIZE=200x
35 dpavlin 1
36 dpavlin 64 # turn locally visible server to watch progress
37 dpavlin 61 DEBUG=0
38     test ! -z "$3" && DEBUG=1
39    
40     # wait for $WAIT seconds for page to load
41     WAIT=30
42    
43 dpavlin 1 # some configurable paths
44     BROWSER=mozilla-firefox
45 dpavlin 60 XSERVER=Xvfb
46 dpavlin 1 URL=$1
47     FILE=${2:-screenshot.jpg}
48    
49     FRAMESET="`mktemp`.html"
50 dpavlin 60 PROFILE_DIR=`mktemp -d`
51 dpavlin 1 PORT=8888
52    
53 dpavlin 61 LOCAL_DISPLAY=$DISPLAY
54    
55 dpavlin 2 if [ -z "$URL" ] ; then
56     echo "usage: $0 http://url.to.capture [screenshot.jpg]"
57     exit 1
58     fi
59    
60 dpavlin 62 if [ -z "`which xdotool`" ] ; then
61     echo "$0 need xdotool from http://www.semicomplete.com/blog/projects/xdotool/"
62 dpavlin 2 exit 1
63     fi
64    
65 dpavlin 60 if [ -z "`which $XSERVER`" ] ; then
66     echo "$0 really need $XSERVER to operate. please install it."
67     exit 1
68     fi
69    
70 dpavlin 61 while netstat -ln | grep ":$PORT " >/dev/null ; do
71     PORT=`expr $PORT + 1`
72     done
73    
74     echo "### using port $PORT"
75    
76 dpavlin 1 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 dpavlin 64 DISPLAY_PORT=6042
85 dpavlin 1
86 dpavlin 64 while netstat -ln | grep ":$DISPLAY_PORT " >/dev/null ; do
87     DISPLAY_PORT=`expr $DISPLAY_PORT + 1`
88     done
89 dpavlin 1
90 dpavlin 64 DISPLAY=:`expr $DISPLAY_PORT - 6000`
91 dpavlin 60
92 dpavlin 64 echo "using DISPLAY=$DISPLAY"
93    
94 dpavlin 61 if [ "$DEBUG" == 1 ] ; then
95 dpavlin 62 echo "using locally visible debug server on $LOCAL_DISPLAY"
96 dpavlin 64 DISPLAY=$LOCAL_DISPLAY Xephyr -ac -screen ${W}x${H}x24 $DISPLAY 2>/dev/null &
97 dpavlin 61 else
98 dpavlin 62 echo "starting $XSERVER"
99 dpavlin 61 $XSERVER -ac -screen 0 ${W}x${H}x24 $DISPLAY 2>/dev/null &
100     fi
101    
102 dpavlin 64 if [ -z "$!" ] ; then
103     echo "ABORT: can't start X server!"
104     exit
105     fi
106 dpavlin 60 XSERVER_PID=$!
107 dpavlin 64
108 dpavlin 62 echo "X server pid $XSERVER_PID"
109 dpavlin 60
110     function kill_x_server() {
111 dpavlin 61 echo "Killing server $XSERVER_PID"
112 dpavlin 60 kill $XSERVER_PID
113 dpavlin 1 rm -f $FRAMESET
114 dpavlin 60 rm -fr $PROFILE_DIR
115 dpavlin 62 trap '' EXIT
116 dpavlin 61 exit 1
117 dpavlin 1 }
118 dpavlin 61 trap 'kill_x_server' INT QUIT TERM SEGV EXIT
119 dpavlin 1
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 dpavlin 60 echo "preview available with: xwd -display $DISPLAY -root | xwud"
135 dpavlin 1
136 dpavlin 60 echo "making 'Screenshot' profile in $PROFILE_DIR"
137 dpavlin 61 $BROWSER -CreateProfile "Screenshot $PROFILE_DIR" 2>/dev/null | grep Success
138 dpavlin 1
139     echo "launching browser $BROWSER with $URL"
140 dpavlin 62 $BROWSER -P Screenshot -width $W -height $H -safemode $FRAMESET 2>/dev/null &
141 dpavlin 60 BROWSER_PID=$!
142 dpavlin 1
143 dpavlin 61 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 dpavlin 62 trap '' EXIT
151 dpavlin 61 exit 1
152     }
153     trap 'kill_browser' INT QUIT TERM SEGV EXIT
154 dpavlin 1
155 dpavlin 62 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 dpavlin 63 $BROWSER -remote "openURL($FRAMESET)" 2>/dev/null
174 dpavlin 62
175 dpavlin 61 echo "waiting for on_load event from browser $BROWSER_PID for ${WAIT}s"
176    
177 dpavlin 1 # there is hard-coded limit here:
178 dpavlin 61 # we will wait $WAIT sec for page to load and render
179 dpavlin 1
180 dpavlin 61 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 dpavlin 1
182     # get Mozilla Firefox window id (for resize)
183 dpavlin 60 WINDOW_ID=`xwininfo -display $DISPLAY -root -tree | grep gecko | cut -d\" -f1 | sort -n | head -1`
184 dpavlin 1
185     if [ -z "$WINDOW_ID" ] ; then
186     echo "can't find window with name 'Mozilla Firefox'"
187     exit 1
188 dpavlin 62 else
189     echo "working on firefox window $WINDOW_ID"
190 dpavlin 1 fi
191    
192 dpavlin 62 xdotool search firefox
193 dpavlin 1
194 dpavlin 62 xdotool focus $WINDOW_ID
195     xdotool key F11
196 dpavlin 1
197 dpavlin 62 ping_browser
198 dpavlin 1
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 dpavlin 2 if [ ! -z "$RESIZE" ] ; then
210     RESIZE="-geometry $RESIZE"
211     fi
212 dpavlin 60
213 dpavlin 2 import -window $DUMP_ID $RESIZE $FILE
214 dpavlin 1
215 dpavlin 62 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