Facebook
From floowsnaake, 6 Years ago, written in Autohotkey.
Embed
Download Paste or View Raw
Hits: 334
  1. IF NOT A_IsAdmin
  2. {
  3.    Run *RunAs "%A_ScriptFullPath%"
  4.    ExitApp
  5. }
  6.  
  7.  
  8.  
  9. xbutton1::
  10. ToolTip, loading...
  11. ;We use this because wonderful COM often causes errors. Like GoBack on the first page in history gives an error.
  12. ;Once you get used to this function, you will save many hours by not having to learn "the COM way" for almost everthing.
  13. ;Maybe it is just me, but I would like to see a chm help file for COM. I hate pouring through 20 pages of
  14. ;forum posts at every turn.
  15. ;BTW: Use the site:ahkscript.org ??? or site:autohotkey.com ??? with google to have a better chance at searching.  
  16. ComObjError(false)
  17.  
  18. ;The example myScript var can be a very very long javascript.
  19. myScript =
  20. (
  21. var1 = document.getElementById(spanId);
  22. element = document.getElementById(spanId);
  23. )
  24. ;<span id="inv_coins">980 coins</span>
  25. /*
  26. IE_InjectJS(myHwnd, "window.onbeforeunload = null;", "", "Stop", "Quit")  ;This uses the 4th and 5th parameters
  27. to allow some simple COM commands to be executed on the IE webpage both before the (javascript/get vars) is called
  28. and/or after the (javascript/get vars) are called.  
  29. They can be used to simply call a few COM things like Stop, Quit, GoBack, etc. It moves some simple COM
  30. calls out to the function call, which can be anywhere in the script. COM commands can be important too.  
  31. */
  32. ;This returns 3 vars and injects myScript javascript and executes 1 COM command at the end(COM_to_Call2)(5th parameter).
  33. myReturnVar := IE_InjectJS(WinExist("ahk_class IEFrame"), myScript, "document.title,element,var1,message""element")
  34.  
  35. ;This is my favorite way to get the Title and URL of a webpage.
  36. ;myReturnVar := IE_InjectJS(WinExist("ahk_class IEFrame"), myScript, "document.title,location.href", "", "")
  37.  
  38. ;This only excutes a COM command (GoBack)(Twice) at the end. No vars are returned.
  39. ;IE_InjectJS(WinExist("ahk_class IEFrame"), "", "", "", "GoBack,GoBack")
  40.  
  41. ;This uses myHWND directly which can be obtained many ways.
  42. ;WinGet, myHWND, ID, ahk_class IEFrame
  43. ;myHWND := WinExist("A")
  44. ;IE_InjectJS(myHWND, "", "", "", "GoBack,GoBack")
  45.  
  46. ;MsgBox, 4096,, myReturnVar is: %myReturnVar%, 5
  47.  
  48. ;if Vars are being returned, this will parse them out and display their values.
  49. if myReturnVar
  50. {
  51. Loop, Parse, myReturnVar, `,
  52. {
  53. if A_Index = 1
  54. myVar1 = %A_LoopField%
  55. if A_Index = 2
  56. myVar2 = %A_LoopField%
  57. if A_Index = 3
  58. myVar3 = %A_LoopField%
  59. if A_Index = 4
  60. myVar4 = %A_LoopField%
  61. }
  62. ToolTip,myVar1 is: %myVar1%`nmyVar2 is: %myVar2%`nmyVar3 is: %myVar3%`nmyVar4 is: %myVar4%`nmyVar5 is: %myVar5% %var1%
  63. }
  64.  
  65. return ;End F1::
  66.  
  67.  
  68. ;COM_to_Call[1|2] can be a [,] delimited string of simple COM commands like GoBack,GoBack etc.
  69. ;VarNames_to_Return can be Vars in the webpage, or webpage contents like document.title, location.href etc.
  70. ;All parameters are optional except hWnd_MainWindow. Thanks to jethrow.
  71. IE_InjectJS(hWnd_MainWindow, JS_to_Inject="", VarNames_to_Return="", COM_to_Call1="", COM_to_Call2="") {
  72.    
  73.    window := _win(hWnd_MainWindow)
  74.    
  75.    wb := WBGet("ahk_id" hWnd_MainWindow)
  76.    
  77.    if COM_to Call1
  78.    {
  79.     ;MsgBox, 4096,, COM_to_Call1 is: %COM_to_Call1%, 4      
  80.     Loop, Parse, COM_to_Call1, `,
  81.     {
  82.     ;MsgBox, 4096,, A_LoopField is: %A_LoopField%, 4
  83.     wb[A_LoopField]    
  84.     }    
  85.    }    
  86.            
  87.    if JS_to_Inject
  88.       window.execScript(JS_to_Inject)
  89.            
  90.    if VarNames_to_Return
  91.       Loop, Parse, VarNames_to_Return, `,
  92.       {
  93.          Loop, Parse, A_LoopField, .
  94.             result := (A_Index=1? window:result)[A_LoopField]
  95.          Ret .= result ","
  96.       }
  97.  
  98.    if COM_to_Call2
  99.    {
  100.     ;MsgBox, 4096,, COM_to_Call2 is: %COM_to_Call2%, 4                
  101.     Loop, Parse, COM_to_Call2, `,
  102.     {
  103.     ;MsgBox, 4096,, A_LoopField is: %A_LoopField%, 4
  104.     wb[A_LoopField]    
  105.     }              
  106.    }  
  107.  
  108.    return SubStr(Ret,1,-1)
  109. }
  110.  
  111. _win(hwnd, Svr#=1) {               ;// based on ComObjQuery docs
  112.  
  113.    static msg := DllCall("RegisterWindowMessage", "str", "WM_HTML_GETOBJECT")
  114.         , IID1 := "{0002DF05-0000-0000-C000-000000000046}"      ; IID_IWebBrowserApp
  115.                 , IID2 := "{332C4427-26CB-11D0-B483-00C04FD90119}"      ; IID_IHTMLWindow2
  116.  
  117.    SendMessage msg, 0, 0, Internet Explorer_Server1, ahk_id %hwnd%
  118.  
  119.    if (ErrorLevel != "FAIL") {
  120.  
  121.       lResult:=ErrorLevel, VarSetCapacity(GUID,16,0)
  122.  
  123.       if DllCall("ole32\CLSIDFromString", "wstr","{332C4425-26CB-11D0-B483-00C04FD90119}", "ptr",&GUID) >= 0 {
  124.  
  125.          DllCall("oleacc\ObjectFromLresult", "ptr",lResult, "ptr",&GUID, "ptr",0, "ptr*",pdoc)
  126.  
  127.                         return  ComObj(9,ComObjQuery(pwb:=ComObjQuery(pdoc,IID1,IID1),IID2,IID2),1)
  128.                                 ,       ObjRelease(pdoc), ObjRelease(pwb)
  129.  
  130.       }
  131.  
  132.    }
  133.  
  134. }
  135.  
  136. WBGet(WinTitle="ahk_class IEFrame", Svr#=1) {               ;// based on ComObjQuery docs
  137.    static msg := DllCall("RegisterWindowMessage", "str", "WM_HTML_GETOBJECT")
  138.         , IID := "{0002DF05-0000-0000-C000-000000000046}"   ;// IID_IWebBrowserApp
  139. ;//     , IID := "{332C4427-26CB-11D0-B483-00C04FD90119}"   ;// IID_IHTMLWindow2
  140.    SendMessage msg, 0, 0, Internet Explorer_Server%Svr#%, %WinTitle%
  141.    if (ErrorLevel != "FAIL") {
  142.       lResult:=ErrorLevel, VarSetCapacity(GUID,16,0)
  143.       if DllCall("ole32\CLSIDFromString", "wstr","{332C4425-26CB-11D0-B483-00C04FD90119}", "ptr",&GUID) >= 0 {
  144.          DllCall("oleacc\ObjectFromLresult", "ptr",lResult, "ptr",&GUID, "ptr",0, "ptr*",pdoc)
  145.          return ComObj(9,ComObjQuery(pdoc,IID,IID),1), ObjRelease(pdoc)
  146.       }
  147.    }
  148. }