--- trunk/webthumb 2008/02/06 21:26:10 61 +++ trunk/webthumb 2008/02/06 23:59:09 65 @@ -23,19 +23,24 @@ # 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=1024 H=768 +D=24 # and then resize it to (comment out to disable) #RESIZE=200x -# turn locally visible Xnest server to watch progress +# turn locally visible server to watch progress DEBUG=0 -test ! -z "$3" && DEBUG=1 +if [ ! -z "$3" ] ; then + echo "DEBUG turned on" + DEBUG=1 + D=8 +fi # wait for $WAIT seconds for page to load WAIT=30 @@ -43,7 +48,6 @@ # some configurable paths BROWSER=mozilla-firefox XSERVER=Xvfb -XWIT=xwit URL=$1 FILE=${2:-screenshot.jpg} @@ -58,8 +62,8 @@ exit 1 fi -if [ -z "`which $XWIT`" ] ; then - echo "$0 really need $XWIT to operate. please install it." +if [ -z "`which xdotool`" ] ; then + echo "$0 need xdotool from http://www.semicomplete.com/blog/projects/xdotool/" exit 1 fi @@ -82,26 +86,38 @@ exit 1 fi -echo "starting X server $XSERVER" +DISPLAY_PORT=6042 + +while netstat -ln | grep ":$DISPLAY_PORT " >/dev/null ; do + DISPLAY_PORT=`expr $DISPLAY_PORT + 1` +done -export DISPLAY=:42 +DISPLAY=:`expr $DISPLAY_PORT - 6000` +echo "using DISPLAY=$DISPLAY" if [ "$DEBUG" == 1 ] ; then - echo "Using locally visible debug server on $LOCAL_DISPLAY" - Xnest -display $LOCAL_DISPLAY -ac -geometry ${W}x${H} -depth 24 $DISPLAY 2>/dev/null & + echo "using locally visible debug server on $LOCAL_DISPLAY" + DISPLAY=$LOCAL_DISPLAY Xephyr -ac -screen ${W}x${H}x${D} $DISPLAY 2>/dev/null & else - $XSERVER -ac -screen 0 ${W}x${H}x24 $DISPLAY 2>/dev/null & + echo "starting $XSERVER" + $XSERVER -ac -screen 0 ${W}x${H}x${D} $DISPLAY 2>/dev/null & fi +if [ -z "$!" ] ; then + echo "ABORT: can't start X server!" + exit +fi XSERVER_PID=$! -echo "using pid $XSERVER_PID for X server" + +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 'kill_x_server' INT QUIT TERM SEGV EXIT @@ -126,7 +142,7 @@ $BROWSER -CreateProfile "Screenshot $PROFILE_DIR" 2>/dev/null | grep Success echo "launching browser $BROWSER with $URL" -$BROWSER -P Screenshot -width $W -height $H $FRAMESET 2>/dev/null & +$BROWSER -P Screenshot -width $W -height $H -safemode $FRAMESET 2>/dev/null & BROWSER_PID=$! function kill_browser() { @@ -136,16 +152,28 @@ kill $XSERVER_PID rm -f $FRAMESET rm -fr $PROFILE_DIR + trap '' EXIT exit 1 } trap 'kill_browser' INT QUIT TERM SEGV EXIT -echo "waiting for on_load event from browser $BROWSER_PID for ${WAIT}s" - -# there is hard-coded limit here: -# we will wait $WAIT sec for page to load and render +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" +} -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!" +ping_browser # get Mozilla Firefox window id (for resize) WINDOW_ID=`xwininfo -display $DISPLAY -root -tree | grep gecko | cut -d\" -f1 | sort -n | head -1` @@ -153,22 +181,25 @@ 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 -# move window to foreground -$XWIT -display $DISPLAY -id $WINDOW_ID -pop +xdotool search firefox -echo "resizing window $WINDOW_ID to maximum size" -$XWIT -display $DISPLAY -id $WINDOW_ID -move 0 0 -$XWIT -display $DISPLAY -id $WINDOW_ID -resize $W $H +xdotool focus $WINDOW_ID +xdotool key F11 +ping_browser -echo -n "wating for browser ping..." -while ! ( $BROWSER -remote "ping();" 2>&1 ) >/dev/null ; do - echo -n "." - sleep 1 -done -echo +$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 $WAIT sec for page to load and render + +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!" # try to deduce inside area of window @@ -186,3 +217,7 @@ import -window $DUMP_ID $RESIZE $FILE +if [ "$DEBUG" == 1 ] ; then + echo -n "press enter to exit! " + read +fi