/[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 60 - (hide annotations)
Wed Feb 6 19:48:56 2008 UTC (13 years, 7 months ago) by dpavlin
File size: 4010 byte(s)
- use Xvfb instead of vncserver, check if it's installed
- commented out debugging Xnest
- create clean profile for each screenshot
- kill browser and than X server to work-around firefox session saver

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     # some configurable paths
37     BROWSER=mozilla-firefox
38 dpavlin 60 XSERVER=Xvfb
39 dpavlin 2 XWIT=xwit
40 dpavlin 1 URL=$1
41     FILE=${2:-screenshot.jpg}
42    
43     FRAMESET="`mktemp`.html"
44 dpavlin 60 PROFILE_DIR=`mktemp -d`
45 dpavlin 1 PORT=8888
46    
47 dpavlin 2 if [ -z "$URL" ] ; then
48     echo "usage: $0 http://url.to.capture [screenshot.jpg]"
49     exit 1
50     fi
51    
52     if [ -z "`which $XWIT`" ] ; then
53 dpavlin 60 echo "$0 really need $XWIT to operate. please install it."
54 dpavlin 2 exit 1
55     fi
56    
57 dpavlin 60 if [ -z "`which $XSERVER`" ] ; then
58     echo "$0 really need $XSERVER to operate. please install it."
59     exit 1
60     fi
61    
62 dpavlin 1 echo -n "testing URL $URL "
63     if HEAD $URL >/dev/null ; then
64     echo "ok"
65     else
66     echo "FAILED"
67     exit 1
68     fi
69    
70 dpavlin 60 echo "starting X server $XSERVER"
71 dpavlin 1
72 dpavlin 60 export DISPLAY=:42
73 dpavlin 1
74 dpavlin 60 $XSERVER -ac -screen 0 ${W}x${H}x24 $DISPLAY &
75     # debug server
76     #Xnest -display :0 -ac -geometry ${W}x${H} -depth 24 $DISPLAY &
77    
78     XSERVER_PID=$!
79     echo "using pid $xserver_pid for X server"
80    
81     function kill_x_server() {
82     kill $BROWSER_PID
83     kill $XSERVER_PID
84 dpavlin 1 rm -f $FRAMESET
85 dpavlin 60 rm -fr $PROFILE_DIR
86 dpavlin 1 }
87 dpavlin 60 trap 'echo "QUIT!" >&2; kill_x_server; exit 1' INT QUIT TERM SEGV
88 dpavlin 1
89     # create frameset to load site and after site is loaded trigger this script
90     cat > $FRAMESET <<END_OF_FRAMESET
91     <html>
92     <head>
93     <frameset rows="0,*" border=0 frameborder=no framespacing=0 onload="window.frames['trigger_frame'].window.location.replace('http://localhost:$PORT');">
94     <frame src="about:blank" name="trigger_frame" scrolling=no marginwidth=0 marginheight=0>
95     <frame src="$URL" name="page_frame" scrolling=no>
96     </frameset>
97     </head>
98     </html>
99     END_OF_FRAMESET
100    
101     echo "Using frameset html $FRAMESET"
102    
103 dpavlin 60 echo "preview available with: xwd -display $DISPLAY -root | xwud"
104 dpavlin 1
105 dpavlin 60 echo "making 'Screenshot' profile in $PROFILE_DIR"
106     $BROWSER -CreateProfile "Screenshot $PROFILE_DIR" | grep Success
107 dpavlin 1
108     echo "launching browser $BROWSER with $URL"
109 dpavlin 60 $BROWSER -P Screenshot -width $W -height $H $FRAMESET &
110     BROWSER_PID=$!
111 dpavlin 1
112     echo "staring web server (wait for page to load)"
113    
114     # there is hard-coded limit here:
115     # we will wait 60sec for page to load and render
116    
117     echo -e "HTTP/1.0 304 Not modified\r\n\r\n" | nc -l -w 60 -p $PORT >/dev/null
118    
119     # get Mozilla Firefox window id (for resize)
120 dpavlin 60 WINDOW_ID=`xwininfo -display $DISPLAY -root -tree | grep gecko | cut -d\" -f1 | sort -n | head -1`
121 dpavlin 1
122     if [ -z "$WINDOW_ID" ] ; then
123     echo "can't find window with name 'Mozilla Firefox'"
124     exit 1
125     fi
126    
127 dpavlin 2 # move window to foreground
128     $XWIT -display $DISPLAY -id $WINDOW_ID -pop
129 dpavlin 1
130     echo "resizing window $WINDOW_ID to maximum size"
131 dpavlin 2 $XWIT -display $DISPLAY -id $WINDOW_ID -move 0 0
132     $XWIT -display $DISPLAY -id $WINDOW_ID -resize $W $H
133 dpavlin 1
134    
135     echo -n "wating for browser ping..."
136     while ! ( $BROWSER -remote "ping();" 2>&1 ) >/dev/null ; do
137     echo -n "."
138     sleep 1
139     done
140     echo
141    
142     # try to deduce inside area of window
143    
144     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`
145    
146     if [ -z "$DUMP_ID" ] ; then
147     echo "can't find inside area of window. Using whole browser!"
148     DUMP_ID=$WINDOW_ID
149     fi
150    
151     echo "saving window $DUMP_ID to $FILE"
152 dpavlin 2 if [ ! -z "$RESIZE" ] ; then
153     RESIZE="-geometry $RESIZE"
154     fi
155 dpavlin 60
156 dpavlin 2 import -window $DUMP_ID $RESIZE $FILE
157 dpavlin 1
158 dpavlin 60 kill_x_server
159 dpavlin 1

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26