IF NOT A_IsAdmin { Run *RunAs "%A_ScriptFullPath%" ExitApp } #SingleInstance Force xbutton1:: ToolTip, loading... ;We use this because wonderful COM often causes errors. Like GoBack on the first page in history gives an error. ;Once you get used to this function, you will save many hours by not having to learn "the COM way" for almost everthing. ;Maybe it is just me, but I would like to see a chm help file for COM. I hate pouring through 20 pages of ;forum posts at every turn. ;BTW: Use the site:ahkscript.org ??? or site:autohotkey.com ??? with google to have a better chance at searching. ComObjError(false) ;The example myScript var can be a very very long javascript. myScript = ( var1 = document.getElementById(spanId); element = document.getElementById(spanId); ) ;980 coins /* IE_InjectJS(myHwnd, "window.onbeforeunload = null;", "", "Stop", "Quit") ;This uses the 4th and 5th parameters to allow some simple COM commands to be executed on the IE webpage both before the (javascript/get vars) is called and/or after the (javascript/get vars) are called. They can be used to simply call a few COM things like Stop, Quit, GoBack, etc. It moves some simple COM calls out to the function call, which can be anywhere in the script. COM commands can be important too. */ ;This returns 3 vars and injects myScript javascript and executes 1 COM command at the end(COM_to_Call2)(5th parameter). myReturnVar := IE_InjectJS(WinExist("ahk_class IEFrame"), myScript, "document.title,element,var1,message""element") ;This is my favorite way to get the Title and URL of a webpage. ;myReturnVar := IE_InjectJS(WinExist("ahk_class IEFrame"), myScript, "document.title,location.href", "", "") ;This only excutes a COM command (GoBack)(Twice) at the end. No vars are returned. ;IE_InjectJS(WinExist("ahk_class IEFrame"), "", "", "", "GoBack,GoBack") ;This uses myHWND directly which can be obtained many ways. ;WinGet, myHWND, ID, ahk_class IEFrame ;myHWND := WinExist("A") ;IE_InjectJS(myHWND, "", "", "", "GoBack,GoBack") ;MsgBox, 4096,, myReturnVar is: %myReturnVar%, 5 ;if Vars are being returned, this will parse them out and display their values. if myReturnVar { Loop, Parse, myReturnVar, `, { if A_Index = 1 myVar1 = %A_LoopField% if A_Index = 2 myVar2 = %A_LoopField% if A_Index = 3 myVar3 = %A_LoopField% if A_Index = 4 myVar4 = %A_LoopField% } ToolTip,myVar1 is: %myVar1%`nmyVar2 is: %myVar2%`nmyVar3 is: %myVar3%`nmyVar4 is: %myVar4%`nmyVar5 is: %myVar5% %var1% } return ;End F1:: ;COM_to_Call[1|2] can be a [,] delimited string of simple COM commands like GoBack,GoBack etc. ;VarNames_to_Return can be Vars in the webpage, or webpage contents like document.title, location.href etc. ;All parameters are optional except hWnd_MainWindow. Thanks to jethrow. IE_InjectJS(hWnd_MainWindow, JS_to_Inject="", VarNames_to_Return="", COM_to_Call1="", COM_to_Call2="") { window := _win(hWnd_MainWindow) wb := WBGet("ahk_id" hWnd_MainWindow) if COM_to Call1 { ;MsgBox, 4096,, COM_to_Call1 is: %COM_to_Call1%, 4 Loop, Parse, COM_to_Call1, `, { ;MsgBox, 4096,, A_LoopField is: %A_LoopField%, 4 wb[A_LoopField] } } if JS_to_Inject window.execScript(JS_to_Inject) if VarNames_to_Return Loop, Parse, VarNames_to_Return, `, { Loop, Parse, A_LoopField, . result := (A_Index=1? window:result)[A_LoopField] Ret .= result "," } if COM_to_Call2 { ;MsgBox, 4096,, COM_to_Call2 is: %COM_to_Call2%, 4 Loop, Parse, COM_to_Call2, `, { ;MsgBox, 4096,, A_LoopField is: %A_LoopField%, 4 wb[A_LoopField] } } return SubStr(Ret,1,-1) } _win(hwnd, Svr#=1) { ;// based on ComObjQuery docs static msg := DllCall("RegisterWindowMessage", "str", "WM_HTML_GETOBJECT") , IID1 := "{0002DF05-0000-0000-C000-000000000046}" ; IID_IWebBrowserApp , IID2 := "{332C4427-26CB-11D0-B483-00C04FD90119}" ; IID_IHTMLWindow2 SendMessage msg, 0, 0, Internet Explorer_Server1, ahk_id %hwnd% if (ErrorLevel != "FAIL") { lResult:=ErrorLevel, VarSetCapacity(GUID,16,0) if DllCall("ole32\CLSIDFromString", "wstr","{332C4425-26CB-11D0-B483-00C04FD90119}", "ptr",&GUID) >= 0 { DllCall("oleacc\ObjectFromLresult", "ptr",lResult, "ptr",&GUID, "ptr",0, "ptr*",pdoc) return ComObj(9,ComObjQuery(pwb:=ComObjQuery(pdoc,IID1,IID1),IID2,IID2),1) , ObjRelease(pdoc), ObjRelease(pwb) } } } WBGet(WinTitle="ahk_class IEFrame", Svr#=1) { ;// based on ComObjQuery docs static msg := DllCall("RegisterWindowMessage", "str", "WM_HTML_GETOBJECT") , IID := "{0002DF05-0000-0000-C000-000000000046}" ;// IID_IWebBrowserApp ;// , IID := "{332C4427-26CB-11D0-B483-00C04FD90119}" ;// IID_IHTMLWindow2 SendMessage msg, 0, 0, Internet Explorer_Server%Svr#%, %WinTitle% if (ErrorLevel != "FAIL") { lResult:=ErrorLevel, VarSetCapacity(GUID,16,0) if DllCall("ole32\CLSIDFromString", "wstr","{332C4425-26CB-11D0-B483-00C04FD90119}", "ptr",&GUID) >= 0 { DllCall("oleacc\ObjectFromLresult", "ptr",lResult, "ptr",&GUID, "ptr",0, "ptr*",pdoc) return ComObj(9,ComObjQuery(pdoc,IID,IID),1), ObjRelease(pdoc) } } }