--- trunk/webthumb 2004/07/26 06:48:59 1 +++ trunk/webthumb 2008/02/06 23:45:58 64 @@ -23,25 +23,56 @@ # HEAD (part or LWP perl library, it can be commented out) # nc (netcat, to simulate web server) # xwininfo (standard part of X-clients) -# xwit (not so standard command, but very useful) # import (part of ImageMagick package) +# xdotool (much more useful than xwit for this purpose) # # It will create snapshot of following size -W=800 -H=800 +W=1024 +H=768 -# and then resize it to -RESIZE=200x +# and then resize it to (comment out to disable) +#RESIZE=200x + +# turn locally visible server to watch progress +DEBUG=0 +test ! -z "$3" && DEBUG=1 + +# wait for $WAIT seconds for page to load +WAIT=30 # some configurable paths BROWSER=mozilla-firefox -XSERVER=vncserver +XSERVER=Xvfb URL=$1 FILE=${2:-screenshot.jpg} FRAMESET="`mktemp`.html" +PROFILE_DIR=`mktemp -d` PORT=8888 +LOCAL_DISPLAY=$DISPLAY + +if [ -z "$URL" ] ; then + echo "usage: $0 http://url.to.capture [screenshot.jpg]" + exit 1 +fi + +if [ -z "`which xdotool`" ] ; then + echo "$0 need xdotool from http://www.semicomplete.com/blog/projects/xdotool/" + exit 1 +fi + +if [ -z "`which $XSERVER`" ] ; then + echo "$0 really need $XSERVER to operate. please install it." + exit 1 +fi + +while netstat -ln | grep ":$PORT " >/dev/null ; do + PORT=`expr $PORT + 1` +done + +echo "### using port $PORT" + echo -n "testing URL $URL " if HEAD $URL >/dev/null ; then echo "ok" @@ -50,16 +81,41 @@ exit 1 fi -echo "starting vnc X server" +DISPLAY_PORT=6042 -export DISPLAY=`$XSERVER -geometry ${W}x${H} -depth 24 2>&1 \ - | grep : | head -1 | cut -d: -f2 | sed 's/^/:/'` +while netstat -ln | grep ":$DISPLAY_PORT " >/dev/null ; do + DISPLAY_PORT=`expr $DISPLAY_PORT + 1` +done + +DISPLAY=:`expr $DISPLAY_PORT - 6000` + +echo "using DISPLAY=$DISPLAY" -function kill_vnc() { - vncserver -kill $DISPLAY +if [ "$DEBUG" == 1 ] ; then + echo "using locally visible debug server on $LOCAL_DISPLAY" + DISPLAY=$LOCAL_DISPLAY Xephyr -ac -screen ${W}x${H}x24 $DISPLAY 2>/dev/null & +else + echo "starting $XSERVER" + $XSERVER -ac -screen 0 ${W}x${H}x24 $DISPLAY 2>/dev/null & +fi + +if [ -z "$!" ] ; then + echo "ABORT: can't start X server!" + exit +fi +XSERVER_PID=$! + +echo "X server pid $XSERVER_PID" + +function kill_x_server() { + echo "Killing server $XSERVER_PID" + kill $XSERVER_PID rm -f $FRAMESET + rm -fr $PROFILE_DIR + trap '' EXIT + exit 1 } -trap 'echo "QUIT!" >&2; kill_vnc; exit 1' INT QUIT TERM SEGV +trap 'kill_x_server' INT QUIT TERM SEGV EXIT # create frameset to load site and after site is loaded trigger this script cat > $FRAMESET </dev/null | grep Success echo "launching browser $BROWSER with $URL" -$BROWSER -id Screenshot -P Screenshot -width $W -height $H $FRAMESET 2>&1 >/dev/null & +$BROWSER -P Screenshot -width $W -height $H -safemode $FRAMESET 2>/dev/null & +BROWSER_PID=$! -echo "staring web server (wait for page to load)" +function kill_browser() { + echo "Killing browser $BROWSER_PID" + kill $BROWSER_PID + echo "Killing server $XSERVER_PID" + kill $XSERVER_PID + rm -f $FRAMESET + rm -fr $PROFILE_DIR + trap '' EXIT + exit 1 +} +trap 'kill_browser' INT QUIT TERM SEGV EXIT + +function ping_browser() { + echo -n "ping browser" + while ! ( $BROWSER -remote "ping();" 2>&1 ) >/dev/null ; do + RID=`xdotool search Restore 2>/dev/null` + if [ ! -z "$RID" ] ; then + echo -n "Esc" + xdotool focus $RID + xdotool key Escape + sleep 1 + fi + echo -n "." + sleep 1 + done + echo " OK" +} + +ping_browser + +$BROWSER -remote "openURL($FRAMESET)" 2>/dev/null + +echo "waiting for on_load event from browser $BROWSER_PID for ${WAIT}s" # there is hard-coded limit here: -# we will wait 60sec for page to load and render +# we will wait $WAIT sec for page to load and render -echo -e "HTTP/1.0 304 Not modified\r\n\r\n" | nc -l -w 60 -p $PORT >/dev/null +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!" # get Mozilla Firefox window id (for resize) - -WINDOW_ID=`xwininfo -display $DISPLAY -name "Mozilla Firefox" | grep "Window id:" | cut -d" " -f4` +WINDOW_ID=`xwininfo -display $DISPLAY -root -tree | grep gecko | cut -d\" -f1 | sort -n | head -1` if [ -z "$WINDOW_ID" ] ; then echo "can't find window with name 'Mozilla Firefox'" exit 1 +else + echo "working on firefox window $WINDOW_ID" fi +xdotool search firefox -echo "resizing window $WINDOW_ID to maximum size" -xwit -display $DISPLAY -id $WINDOW_ID -resize $W $H +xdotool focus $WINDOW_ID +xdotool key F11 - -echo -n "wating for browser ping..." -while ! ( $BROWSER -remote "ping();" 2>&1 ) >/dev/null ; do - echo -n "." - sleep 1 -done -echo +ping_browser # try to deduce inside area of window @@ -121,7 +206,13 @@ fi echo "saving window $DUMP_ID to $FILE" -import -window $DUMP_ID -geometry $RESIZE $FILE +if [ ! -z "$RESIZE" ] ; then + RESIZE="-geometry $RESIZE" +fi -kill_vnc +import -window $DUMP_ID $RESIZE $FILE +if [ "$DEBUG" == 1 ] ; then + echo -n "press enter to exit! " + read +fi