/[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 65 - (hide annotations)
Wed Feb 6 23:59:09 2008 UTC (13 years, 8 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 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 65 D=24
33 dpavlin 1
34 dpavlin 2 # and then resize it to (comment out to disable)
35     #RESIZE=200x
36 dpavlin 1
37 dpavlin 64 # turn locally visible server to watch progress
38 dpavlin 61 DEBUG=0
39 dpavlin 65 if [ ! -z "$3" ] ; then
40     echo "DEBUG turned on"
41     DEBUG=1
42     D=8
43     fi
44 dpavlin 61
45     # wait for $WAIT seconds for page to load
46     WAIT=30
47    
48 dpavlin 1 # some configurable paths
49     BROWSER=mozilla-firefox
50 dpavlin 60 XSERVER=Xvfb
51 dpavlin 1 URL=$1
52     FILE=${2:-screenshot.jpg}
53    
54     FRAMESET="`mktemp`.html"
55 dpavlin 60 PROFILE_DIR=`mktemp -d`
56 dpavlin 1 PORT=8888
57    
58 dpavlin 61 LOCAL_DISPLAY=$DISPLAY
59    
60 dpavlin 2 if [ -z "$URL" ] ; then
61     echo "usage: $0 http://url.to.capture [screenshot.jpg]"
62     exit 1
63     fi
64    
65 dpavlin 62 if [ -z "`which xdotool`" ] ; then
66     echo "$0 need xdotool from http://www.semicomplete.com/blog/projects/xdotool/"
67 dpavlin 2 exit 1
68     fi
69    
70 dpavlin 60 if [ -z "`which $XSERVER`" ] ; then
71     echo "$0 really need $XSERVER to operate. please install it."
72     exit 1
73     fi
74    
75 dpavlin 61 while netstat -ln | grep ":$PORT " >/dev/null ; do
76     PORT=`expr $PORT + 1`
77     done
78    
79     echo "### using port $PORT"
80    
81 dpavlin 1 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 dpavlin 64 DISPLAY_PORT=6042
90 dpavlin 1
91 dpavlin 64 while netstat -ln | grep ":$DISPLAY_PORT " >/dev/null ; do
92     DISPLAY_PORT=`expr $DISPLAY_PORT + 1`
93     done
94 dpavlin 1
95 dpavlin 64 DISPLAY=:`expr $DISPLAY_PORT - 6000`
96 dpavlin 60
97 dpavlin 64 echo "using DISPLAY=$DISPLAY"
98    
99 dpavlin 61 if [ "$DEBUG" == 1 ] ; then
100 dpavlin 62 echo "using locally visible debug server on $LOCAL_DISPLAY"
101 dpavlin 65 DISPLAY=$LOCAL_DISPLAY Xephyr -ac -screen ${W}x${H}x${D} $DISPLAY 2>/dev/null &
102 dpavlin 61 else
103 dpavlin 62 echo "starting $XSERVER"
104 dpavlin 65 $XSERVER -ac -screen 0 ${W}x${H}x${D} $DISPLAY 2>/dev/null &
105 dpavlin 61 fi
106    
107 dpavlin 64 if [ -z "$!" ] ; then
108     echo "ABORT: can't start X server!"
109     exit
110     fi
111 dpavlin 60 XSERVER_PID=$!
112 dpavlin 64
113 dpavlin 62 echo "X server pid $XSERVER_PID"
114 dpavlin 60
115     function kill_x_server() {
116 dpavlin 61 echo "Killing server $XSERVER_PID"
117 dpavlin 60 kill $XSERVER_PID
118 dpavlin 1 rm -f $FRAMESET
119 dpavlin 60 rm -fr $PROFILE_DIR
120 dpavlin 62 trap '' EXIT
121 dpavlin 61 exit 1
122 dpavlin 1 }
123 dpavlin 61 trap 'kill_x_server' INT QUIT TERM SEGV EXIT
124 dpavlin 1
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 dpavlin 60 echo "preview available with: xwd -display $DISPLAY -root | xwud"
140 dpavlin 1
141 dpavlin 60 echo "making 'Screenshot' profile in $PROFILE_DIR"
142 dpavlin 61 $BROWSER -CreateProfile "Screenshot $PROFILE_DIR" 2>/dev/null | grep Success
143 dpavlin 1
144     echo "launching browser $BROWSER with $URL"
145 dpavlin 62 $BROWSER -P Screenshot -width $W -height $H -safemode $FRAMESET 2>/dev/null &
146 dpavlin 60 BROWSER_PID=$!
147 dpavlin 1
148 dpavlin 61 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 dpavlin 62 trap '' EXIT
156 dpavlin 61 exit 1
157     }
158     trap 'kill_browser' INT QUIT TERM SEGV EXIT
159 dpavlin 1
160 dpavlin 62 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 dpavlin 1 # get Mozilla Firefox window id (for resize)
179 dpavlin 60 WINDOW_ID=`xwininfo -display $DISPLAY -root -tree | grep gecko | cut -d\" -f1 | sort -n | head -1`
180 dpavlin 1
181     if [ -z "$WINDOW_ID" ] ; then
182     echo "can't find window with name 'Mozilla Firefox'"
183     exit 1
184 dpavlin 62 else
185     echo "working on firefox window $WINDOW_ID"
186 dpavlin 1 fi
187    
188 dpavlin 62 xdotool search firefox
189 dpavlin 1
190 dpavlin 62 xdotool focus $WINDOW_ID
191     xdotool key F11
192 dpavlin 1
193 dpavlin 62 ping_browser
194 dpavlin 1
195 dpavlin 65 $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 dpavlin 1 # 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 dpavlin 2 if [ ! -z "$RESIZE" ] ; then
215     RESIZE="-geometry $RESIZE"
216     fi
217 dpavlin 60
218 dpavlin 2 import -window $DUMP_ID $RESIZE $FILE
219 dpavlin 1
220 dpavlin 62 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