Requires acc.ahk here:
<!-- m -->http://www.autohotke...pic.php?t=24234<!-- m -->
and
com.ahk here:
<!-- m -->http://www.autohotke...pic.php?t=19225<!-- m -->
EDIT:
-It Works! Script has been updated to use lexikos' method. THANKS
-Added check so that code is injected into correct tab on IE7.
-Removed unnecessary variable
-Added function
- Added functionality to return variables to script. (in function)
#Include acc.ahk #Include com.ahk ^F12:: IE_MainWindow := WinExist("A") WinGet, ActiveControlList, ControlListhWnd, ahk_id %IE_MainWindow% Loop, Parse, ActiveControlList, `n { WinGetClass, ThisWinClass, ahk_id %A_LoopField% If (ThisWinClass = "Internet Explorer_Server") and (DllCall("IsWindowVisible", UInt, A_LoopField)) hIESvr := A_LoopField } If !hIESvr { MsgBox, Control "Internet Explorer_Server" not found. Return } IID_IHTMLWindow2 := "{332C4427-26CB-11D0-B483-00C04FD90119}" ACC_Init() pacc := ACC_AccessibleObjectFromWindow(hIESvr) pwin := COM_QueryService(pacc,IID_IHTMLWindow2,IID_IHTMLWindow2) COM_Release(pacc) ;Load an URL ;COM_Invoke(pwin, "Navigate", "http://www.google.com") ;Load Javascript JStoSend=javascript:alert('it works!'); COM_Invoke(pwin, "execscript", JStoSend) COM_Release(pwin) ACC_Term() return esc::exitappFunction
Usage:
;WinExist("A") uses the active window MsgBox % "AHKSCRIPT:" . IE_InjectJS(WinExist("A"), "javascript:ahkvar1='It really Does';ahkvar2='!!!';alert('JAVASCRIPT:Hey, it Works!');", "ahkvar1,ahkvar2")
; Function Name: IE_InjectJS ; Parameters: ; hWnd_MainWindow - REQUIRED - the hWnd of the main window containing the "Internet Explorer_Server" control : EXAMPLE (using active window) - WinExist("A") ; JS_to_Inject - REQUIRED - Javascript to execute in the browser window : EXAMPLE - "javascript:ahkvar1='It really Does';ahkvar2='!!!';alert('Hey, it Works!');" ; VarNames_to_Return - OPTIONAL - Comma delimited list of global javascript variables to return : EXAMPLE - "ahkvar1,ahkvar2" ; ; Return: ; Returns a comma delimited list of the variables contents (In the order passed to the function) ; Calling Example: ; IE_InjectJS(WinExist("A"), "javascript:ahkvar1='It really Does';ahkvar2='!!!';alert('Hey, it Works!');", "ahkvar1,ahkvar2") ; IE_InjectJS(hWnd_MainWindow, JS_to_Inject, VarNames_to_Return="") { ;Get a list of the hWnd's owned by the window specified WinGet, ActiveControlList, ControlListhWnd, ahk_id %hWnd_MainWindow% ;Go throught the list 1 at a time to determine if it is the correct control ;This will allow the script to find the current tab in IE7 Loop, Parse, ActiveControlList, `n { ;Get the classname of the current control WinGetClass, ThisWinClass, ahk_id %A_LoopField% ;If the classname is correct and it is visible, it is the correct tab If (ThisWinClass = "Internet Explorer_Server") and (DllCall("IsWindowVisible", UInt, A_LoopField)) hIESvr := A_LoopField } ;If a control was not found, give a message and return, doing nothing If !hIESvr { MsgBox, Control "Internet Explorer_Server" not found. Return } ;Initialize the COM interface. code modified from SEAN IID_IHTMLWindow2 := "{332C4427-26CB-11D0-B483-00C04FD90119}" ACC_Init() pacc := ACC_AccessibleObjectFromWindow(hIESvr) pwin := COM_QueryService(pacc,IID_IHTMLWindow2,IID_IHTMLWindow2) COM_Release(pacc) ;Execute the Javascript (if there is any). Thanks LEXIKOS. If JS_to_Inject COM_Invoke(pwin, "execscript", JS_to_Inject) ;Get the value of the variables, if any. If VarNames_to_Return { ;Split the passed variable names into a psuedo array of ahk variables StringSplit, Vars_, VarNames_to_Return, `, ;Get the value of each javascript variable in the order it was passed Loop, %Vars_0% Ret .= COM_Invoke(pwin,Vars_%A_Index%) . "," ;Remove the trailing comma StringTrimRight, Ret, Ret, 1 } ; Cleanup COM_Release(pwin) ACC_Term() ;Return a comma seperated list of variables in the order they were passed Return Ret }