I put together a little web browser which can be controlled by autohotkey, using various postings by 'tank', Sean, Nicolas Bourbaki and several others.
I had two reasons for writing this:
1. scripts recorded by the script recorder that comes with autohotkey cannot tell when a page is busy - and usually fails when the page title does not change.
2. autohotkey running on other browsers cannot scan for text on the page.
I use this autohotkey browser to do regression testing on my web pages. It is a work in progress as I can't get it to realiably read text on a page as yet.
Several include files are needed and can be found on google:
iecontrol.ahk
cohelper.ahk
anchor.ahk
control_anigif.ahk
anigif.dll
systemcursorchange3.ahk (included below)
Code:
; Fong
; Change mouse cursor to resize cursor
CursorChange(ToResize)
{
static normal_cursor
OCR_NORMAL := 32512
OCR_SIZEWE := 32644
if (ToResize)
{
; Make a copy of the original mouse cursor
normal_cursor := DllCall( "LoadCursor", "uint", 0, "uint", OCR_NORMAL )
normal_cursor := DllCall( "CopyImage", "uint", normal_cursor, "uint", 2, "int", 0, "int", 0, "uint", 0 )
; Make a copy of the resize cursor
resize_cursor := DllCall( "LoadCursor", "uint", 0, "uint", OCR_SIZEWE )
resize_cursor := DllCall( "CopyImage", "uint", resize_cursor, "uint", 2, "int", 0, "int", 0, "uint", 0 )
; Set Normal cursor to the resize image
;msgbox, Changing to RESIZE
result := DllCall( "SetSystemCursor", "uint", resize_cursor, "uint", OCR_NORMAL )
} else
{
; Restore the original cursor
;msgbox, Changing to NORMAL
result := DllCall( "SetSystemCursor", "uint", normal_cursor, "uint", OCR_NORMAL )
}
Return
}
These two can be found at
http://picasaweb.google.com/fongmark/Mypublic#Google displays them way bigger than they actually are when saved:
bar.gif
Cdrom_spins_2B.gif
To use this browser, start it up, run the autohotkey recorder and navigate your web pages. Save your recording, then use this script to modify it - it puts in checks to make sure the web page is not busy. Then include your recording in the browser script at F12:: label.
Code:
/* FixRecording.ahk
By Mark Fong
Meant to be used for fixing a recording on my
internet explorer emulator (found in D:\hotkey\IE).
The raw recording does not wait for IE to be ready
before sending the next mouse clicks.
*/
Gui, Add, Edit, x-2 y-4 w370 h270 vMyEdit, Drop .ahk file here
Gui, Add, Button, x366 y26 w70 h30 , Save
Gui, Add, Button, x366 y76 w70 h30 , Exit
; Generated using SmartGUI Creator 4.0
Gui, Show, x131 y91 h265 w443, Fix AHK Recordings
Return
; Get the file name that was dragged onto this window.
GuiDropFiles:
Loop, parse, A_GuiEvent, `n
{
FirstFile = %A_LoopField%
Break
}
; MsgBox, The file name is %FirstFile%
Gosub, MakeChanges
Return
MakeChanges:
; Read the file off the disk into my editor.
FileRead, FileContents, %FirstFile%
GuiControl,, MyEdit, %FileContents%
; Make my changes.
NewStr := RegExReplace(FileContents, "MouseClick.*", "$0`r`nLoop {`r`n Sleep, 250`r`n if IE_Busy(pwb) = 0`r`n Break`r`n}")
GuiControl,, MyEdit, %NewStr%
Return
ButtonSave:
FileDelete, %FirstFile%
FileAppend, %NewStr%, %FirstFile%
GoSub, GuiClose
Return
ButtonExit:
GuiClose:
ExitApp
Here is the browser code:
Code:
/* Add sliding bar to expand URL text box.
Add menu item to View Source code of html.
*/
#Include IEControl.ahk ; internet explorer control
#SingleInstance force
#Include Anchor.ahk ; for autoresize of controls
#include Control_AniGif.ahk ; display animated .gif files
;#include URLtoVariable2.ahk ; for reading the web page contents
;#include GetWebPageContents.ahk
GoSub, GuiStart
Gui, +LastFound +Resize
Menu, FileMenu, Add, &View Source, MenuViewSource
Menu, FileMenu, Add, E&xit, GuiClose
Menu, HelpMenu, Add, &About, MenuHandler
Menu, MyMenuBar, Add, &File, :FileMenu ; Attach the two sub-menus that were created above.
Menu, MyMenuBar, Add, &Help, :HelpMenu
Gui, Menu, MyMenuBar
; Add the busy icon.
;------------------------------------------------
guiID := WinExist()
hAniGif1 := AniGif_CreateControl(guiID, 3, 1, 19, 20)
If hAniGif1 is not integer
MsgBox %hAniGif1%
;------------------------------------------------
; Add URL address text box
Gui, 1:Add, Edit, R1 vMyURL gUserURL xp+14
GuiControl, 1:Move, MyURL, w400 ; make it wide
; Set the FONT size for the URL box.
Gui, 1:Font, s9 cRed Bold, Verdana ; If desired, set a new default font for the window.
GuiControl, 1:Font, MyURL ; Put the above font into effect for a control.
; Set the color of the text 'bar'
; Gui, Color, , Green
; Add a bar to allow slide expand of URL box.
Gui, Add, Picture, xp+405 yp w5 h20 gBarClick, Bar.GIF ; Receives the control name Static1
Gui, 1:Add, Button, xp+11 yp h22, Go
; Create a TextBox to hold the IE control.
Gui, Add, Text, vMyIE r80 xm HwndmyhWnd
GuiControl, Move, MyIE, w480 h400 ; make it wide
Gui, Show, AutoSize, WebBrowser
hWnd := myhWnd ; handle of the textbox that holds the IE control.
CLSID_WebBrowser := "{8856F961-340A-11D0-A96B-00C04FD705A2}"
IID_IWebBrowser2 := "{D30C1661-CDAF-11D0-8A3E-00C04FC9E26E}"
pwb := CreateObject(CLSID_WebBrowser, IID_IWebBrowser2)
; This is for capturing and sending Enter, Delete, Tab to IE control.
PIPAVAR := "{00000117-0000-0000-C000-000000000046}"
pipa := QueryInterface(pwb, PIPAVAR)
AtlAxAttachControl(pwb, hWnd) ; attach web browser control to textbox control
IE_LoadURL(pwb, "www.google.com")
Sleep 2000
;Gui, Maximize
;GoSub, OSDStart
; For the title
prevtitle := ""
prevbusy := 0
SetTimer, UpdateTitle, 300
;SetTimer, UpdateOSD, Off ; Turn off the OSD updates. This allows popup windows like Search to have focus.
;Gui, 2:Destroy ; Remove the OSD
; Change the mouse cursor when it moves over the bar.gif
BarActive := false
initpos := 0
BarPos := 0
#Include SystemCursorChange3.ahk
SetTimer, WatchCursor, 100
Return
; Change the mouse pointer to Resize when over the Bar (next to Go button).
WatchCursor:
MouseGetPos, mouseX, mouseY, id, controlBar
/*
WinGetTitle, title, ahk_id %id%
WinGetClass, class, ahk_id %id%
ToolTip, ahk_id %id%`nahk_class %class%`n%title%`nControl: %controlBar%
*/
if (controlBar == "Static1" and BarActive == false)
{
;MsgBox, Mouse is over BAR
BarActive := true
CursorChange(BarActive)
} else if (controlBar <> "Static1" and BarActive)
{
; Restore the regular mouse cursor
BarActive := false
CursorChange(BarActive)
}
Return
BarClick:
;MsgBox, The BAR was clicked
if (BarActive)
{
Loop
{
GetKeyState, LButtonState, LButton, P
if LButtonState = D ; While Left mouse button is Down, drag Bar.
{
GuiControl, MoveDraw, Static1, % "x" mouseX-6 ; % move the Bar
GuiControl, MoveDraw, Button1, % "x" mouseX-6+11 ; % move the GO button
GuiControl, Move, MyURL, % "w" mouseX-35 ; % change the width of URL text box
Sleep, 100
} else
break
}
}
Return
; ------------------------------
; This section puts the IE Busy state on the screen.
OSDStart:
; Example: On-screen display (OSD) via transparent window:
CustomColor = EEAA99 ; Can be any RGB color (it will be made transparent below).
;Gui 2:+LastFound +AlwaysOnTop -Caption +ToolWindow ; +ToolWindow avoids a taskbar button and an alt-tab menu item.
Gui, 2:+owner1 ; Make #2 window owned by script's main window to prevent display of a taskbar button.
;Gui 2:+LastFound -Caption +ToolWindow ; +ToolWindow avoids a taskbar button and an alt-tab menu item.
; Add URL address text box
Gui, 2:Add, Edit, R1 vMyURL gUserURL section
GuiControl, 2:Move, MyURL, w400 ; make it wide
; Set the FONT size for the URL box.
Gui, 2:Font, s9 cRed Bold, Verdana ; If desired, set a new default font for the window.
GuiControl, 2:Font, MyURL ; Put the above font into effect for a control.
; Gui, 2:Add, Button, ys, Go ;doesn't work with making the edit wide above.
Gui, 2:Add, Button, x420 ys, Go
Gui, 2:Color, %CustomColor%
Gui, 2:Font, s32 ; Set a large font size (32-point).
; Gui, 2:Add, Text, vMyText cLime, XXXXX YYYYY ; XX & YY serve to auto-size the window.
; Make all pixels of this color transparent and make the text itself translucent (150):
WinSet, TransColor, %CustomColor% 150
;SetTimer, UpdateOSD, 1000
;Gosub, UpdateOSD ; Make the first update immediate rather than waiting for the timer.
Gui, 2:Show, x80 y23 Autosize NoActivate ; NoActivate avoids deactivating the currently active window.
Return
UpdateOSD:
; Interesting note these mouse coords are relative to the IE Gui 1: window. (which is good).
;MouseGetPos, MouseX, MouseY
;GuiControl, 2:, MyText, X%MouseX%, Y%MouseY%
; Show IE status
;/*
; READYSTATE_UNINITIALIZED = 0 ; Default initialization state.
; READYSTATE_LOADING = 1 ; Object is currently loading its properties.
; READYSTATE_LOADED = 2 ; Object has been initialized.
; READYSTATE_INTERACTIVE = 3 ; Object is interactive, but not all of its data is available.
; READYSTATE_COMPLETE = 4 ; Object has received all of its data.
;*/
;readystate := IE_ReadyState(pwb)
readystate := IE_Busy(pwb)
GuiControl, 2:, MyText, IE Busy = %readystate%
Return
; Change the Title of the web browser window, so that recorder will key off of it.
; Only want to execute the Show when the title changes, to minimize impact of side effects documented below.
UpdateTitle:
mytitle := IE_GetTitle(pwb)
if(prevtitle <> mytitle)
{
Gui, 1:Show,,%mytitle% ;Keeps window on TOP (if the title changes).
prevtitle := mytitle
; Update the URL box.
curURL := IE_GetUrl(pwb)
;GuiControl, 2:, MyURL, %curURL%
GuiControl, , MyURL, %curURL%
}
; Display the Busy icon
busystate := IE_Busy(pwb)
if(prevbusy <> busystate)
{
if(busystate == true)
{
; http://picasaweb.google.com/fongmark/Mypublic#
AniGif_LoadGifFromFile(hAniGif1, "Cdrom_spins_2B.gif")
} else
{
AniGif_UnloadGif(hAniGif1)
;Sleep 2000
/* Page has finished loading */
;;Gosub, GetWebPageContents
;;webpagecontents1 := GetWebPageContents()
sUrl := IE_GetUrl(pwb)
;msgbox, New URL = %sUrl%
UrlDownloadToFile, %sUrl%, myTemp1.txt
;UrlDownloadToFile, %curUrl%, myTemp1.txt
/* Now the temp file contains the web content that is currently on the screen.
Read it into a variable.
*/
FileRead, webpagecontents, myTemp1.txt
;msgbox, web5 = %webpagecontents%
;Needle := "Password:</TH>"
;Needle = Password:</TH>
;Needle = Password:
Needle = Password
;IfInString, webpagecontents, %Needle%
;pos := InStr(webpagecontents, %Needle%)
pos := InStr(webpagecontents, "Password:</TH>") ; this does not work
;if (InStr(webpagecontents, %Needle%))
if (pos > 0)
{
MsgBox, Password box found.
}
}
prevbusy := busystate
}
Return
UserURL:
; User or program has changed the URL
; MsgBox URL has changed.
; MyURLt := MyURL
Return
ButtonGO:
; GO button was pressed, get the URL from the Edit box.
GuiControlGet, MyURLt, 1:, MyURL
IE_LoadURL(pwb, MyURLt)
Return
MenuViewSource:
IE_ViewSource(pwb)
Return
MenuHandler:
Return
; By Nicolas Bourbaki
; Send Tab, Enter, Delete keys to Internet Explorer control.
WM_KEYDOWN(wParam, lParam, nMsg, hWnd)
{
; Critical 20
If (wParam = 0x09 || wParam = 0x0D || wParam = 0x2E || wParam = 0x26 || wParam = 0x28) ; tab enter delete up down
{
WinGetClass, Class, ahk_id %hWnd%
;msgbox, % Class
If (Class == "Internet Explorer_Server")
{
Global pipa
VarSetCapacity(Msg, 28)
NumPut(hWnd,Msg), NumPut(nMsg,Msg,4), NumPut(wParam,Msg,8), NumPut(lParam,Msg,12)
NumPut(A_EventInfo,Msg,16), NumPut(A_GuiX,Msg,20), NumPut(A_GuiY,Msg,24)
DllCall(NumGet(NumGet(1*pipa)+20), "Uint", pipa, "Uint", &Msg)
Return 0
} else if (Class == "Edit")
{
Gosub, ButtonGO ; Enter key in URL box should click the GO button.
}
}
}
F12::
; #include mytest1.ahk
Return
GuiSize:
Anchor("MyIE", "wh") ; This makes the IE window resize to the Main window size
Return
GuiStart:
OnMessage(WM_KEYDOWN:=0x0100, "WM_KEYDOWN")
OnMessage(WM_KEYUP:=0x0101, "WM_KEYDOWN")
AtlAxWinInit()
CoInitialize()
Return
GuiClose:
Gui, 1:Destroy
Release(pwb)
CoUninitialize()
AtlAxWinTerm()
ExitApp
[/code]