/[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 61 - (hide 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 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     # xwit (not so standard command, but very useful)
27     # import (part of ImageMagick package)
28     #
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 61 # 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 dpavlin 1 # some configurable paths
44     BROWSER=mozilla-firefox
45 dpavlin 60 XSERVER=Xvfb
46 dpavlin 2 XWIT=xwit
47 dpavlin 1 URL=$1
48     FILE=${2:-screenshot.jpg}
49    
50     FRAMESET="`mktemp`.html"
51 dpavlin 60 PROFILE_DIR=`mktemp -d`
52 dpavlin 1 PORT=8888
53    
54 dpavlin 61 LOCAL_DISPLAY=$DISPLAY
55    
56 dpavlin 2 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 dpavlin 60 echo "$0 really need $XWIT to operate. please install it."
63 dpavlin 2 exit 1
64     fi
65    
66 dpavlin 60 if [ -z "`which $XSERVER`" ] ; then
67     echo "$0 really need $XSERVER to operate. please install it."
68     exit 1
69     fi
70    
71 dpavlin 61 while netstat -ln | grep ":$PORT " >/dev/null ; do
72     PORT=`expr $PORT + 1`
73     done
74    
75     echo "### using port $PORT"
76    
77 dpavlin 1 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 dpavlin 60 echo "starting X server $XSERVER"
86 dpavlin 1
87 dpavlin 60 export DISPLAY=:42
88 dpavlin 1
89 dpavlin 60
90 dpavlin 61 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 dpavlin 60 XSERVER_PID=$!
98 dpavlin 61 echo "using pid $XSERVER_PID for X server"
99 dpavlin 60
100     function kill_x_server() {
101 dpavlin 61 echo "Killing server $XSERVER_PID"
102 dpavlin 60 kill $XSERVER_PID
103 dpavlin 1 rm -f $FRAMESET
104 dpavlin 60 rm -fr $PROFILE_DIR
105 dpavlin 61 exit 1
106 dpavlin 1 }
107 dpavlin 61 trap 'kill_x_server' INT QUIT TERM SEGV EXIT
108 dpavlin 1
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 dpavlin 60 echo "preview available with: xwd -display $DISPLAY -root | xwud"
124 dpavlin 1
125 dpavlin 60 echo "making 'Screenshot' profile in $PROFILE_DIR"
126 dpavlin 61 $BROWSER -CreateProfile "Screenshot $PROFILE_DIR" 2>/dev/null | grep Success
127 dpavlin 1
128     echo "launching browser $BROWSER with $URL"
129 dpavlin 61 $BROWSER -P Screenshot -width $W -height $H $FRAMESET 2>/dev/null &
130 dpavlin 60 BROWSER_PID=$!
131 dpavlin 1
132 dpavlin 61 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 dpavlin 1
143 dpavlin 61 echo "waiting for on_load event from browser $BROWSER_PID for ${WAIT}s"
144    
145 dpavlin 1 # there is hard-coded limit here:
146 dpavlin 61 # we will wait $WAIT sec for page to load and render
147 dpavlin 1
148 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!"
149 dpavlin 1
150     # get Mozilla Firefox window id (for resize)
151 dpavlin 60 WINDOW_ID=`xwininfo -display $DISPLAY -root -tree | grep gecko | cut -d\" -f1 | sort -n | head -1`
152 dpavlin 1
153     if [ -z "$WINDOW_ID" ] ; then
154     echo "can't find window with name 'Mozilla Firefox'"
155     exit 1
156     fi
157    
158 dpavlin 2 # move window to foreground
159     $XWIT -display $DISPLAY -id $WINDOW_ID -pop
160 dpavlin 1
161     echo "resizing window $WINDOW_ID to maximum size"
162 dpavlin 2 $XWIT -display $DISPLAY -id $WINDOW_ID -move 0 0
163     $XWIT -display $DISPLAY -id $WINDOW_ID -resize $W $H
164 dpavlin 1
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 dpavlin 2 if [ ! -z "$RESIZE" ] ; then
184     RESIZE="-geometry $RESIZE"
185     fi
186 dpavlin 60
187 dpavlin 2 import -window $DUMP_ID $RESIZE $FILE
188 dpavlin 1

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26