Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

IE_InjectJS => byTT: Inject JavaScript into IE by Tab Tit


  • Please log in to reply
2 replies to this topic
SinClair
  • Members
  • 4 posts
  • Last active: May 25 2008 08:35 PM
  • Joined: 20 Apr 2007
Hey All!

I was looking for a way to control web pages via Autohotkey when I came across ahklerner's IE_InjectJS function (awesome!). It was very close to what I needed, but I wanted to be able to control IE tabs in the background.

So here's the code!

; Insert JavaScript into IE by Tab Title via COM v0.3
; Thanks to AHKLERNER, SEAN and LEXIKOS
; Function Name: IE_InjectJSbyTT
; Parameters:
; Access_Tab_Title    - REQUIRED   - Title of the "Internet Explorer_Server" tab/control. Specify "A" for the active window: EXAMPLE - (Use IE tab containing Google): "Google" - (Use active window/tab) - "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:
; -Run a search in an existing Google tab
; 	IE_InjectJSbyTT("Google"), "javascript:document.f.q.value='autohotkey';document.f.submit();")
; -Execute script it the active IE tab
;	IE_InjectJSbyTT("A", "javascript:ahkvar1='It really Does';ahkvar2='!!!';alert('Hey, it Works!');", "ahkvar1,ahkvar2")
;
;----------------------------------

IE_InjectJSbyTT(Access_Tab_Title, JS_to_Inject, VarNames_to_Return="")
	{
	Global IE_InjectJS_TabLock,IE_InjectJS_TabLock_hWnd
	Loop, 2
		{
		; Set Control to Active Tab
		If (Access_Tab_Title = "A")
			{
			; Clear the locked tab
			IE_InjectJS_TabLock_hWnd := 
			; Set hWnd to active window
			hWnd_MainWindow := WinExist("A")
			; Get a list of the hWnd's owned by the window specified
			WinGet, ControlList, ControlListhWnd, ahk_id %hWnd_MainWindow%
			}
		; Set Control to Tab Title
		Else If (IE_InjectJS_TabLock != Access_Tab_Title) || (!IE_InjectJS_TabLock_hWnd)
			{
			; Globalize tab title
			IE_InjectJS_TabLock := Access_Tab_Title
			; Get a list of the hWnd's owned by Internet Explorer
			WinGet, IE_ControlList, ControlListhWnd, ahk_class IEFrame
			; For use with IE tabs in FireFox
			WinGet, FF_ControlList, ControlListhWnd, ahk_class MozillaUIWindowClass
			; Create combined list. If there identical tabs IE and FF, default to IE
			ControlList := IE_ControlList . "`n" . FF_ControlList
			}
		 ; Set Control to last hWnd by requested the specified tab name
		Else If IE_InjectJS_TabLock_hWnd
			{
			UseLock := True
			ControlList := IE_InjectJS_TabLock_hWnd
			IE_InjectJS_TabLock_hWnd :=
			}
	   ; Go throught the list 1 at a time to determine if it is the correct control
	   ; This will allow the script to find the requested tab in IE7 or FireFox
		Loop, Parse, ControlList, `n
			{
			; Get the class name of the current control
			WinGetClass, ThisWinClass, ahk_id %A_LoopField%
			; If the class name is correct and it is a window, it should support JS execution
			If (ThisWinClass = "Internet Explorer_Server") && (DllCall("IsWindow", UInt, A_LoopField))
				{
				;Initialize the COM interface. Code from AHKLERNER & modified by SEAN
					IID_IHTMLWindow2 := "{332C4427-26CB-11D0-B483-00C04FD90119}"
					ACC_Init()
					pacc := ACC_AccessibleObjectFromWindow(A_LoopField)
					pwin := COM_QueryService(pacc,IID_IHTMLWindow2,IID_IHTMLWindow2)
					COM_Release(pacc)
				; Flag the initialization
				init := True
				; Insert the page title object into a accessible variable
				COM_Invoke(pwin, "execscript", "javascript:var iJS_Title=document.title;")
				; Check for tab matching the request
				If (((Access_Tab_Title = "A") && (DllCall("IsWindowVisible", UInt, A_LoopField))) || (SubStr(COM_Invoke(pwin,"iJS_Title"), 1, StrLen(Access_Tab_Title)) = Access_Tab_Title))
					{
					IE_InjectJS_TabLock_hWnd :=
					;Execute the Javascript (if there is any). Thanks LEXIKOS.
					If JS_to_Inject
						COM_Invoke(pwin, "execscript", JS_to_Inject)
					If VarNames_to_Return {
						;Split the passed variable names into a pseudo 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
						}
					; Globalize the tabs hWnd
					IE_InjectJS_TabLock_hWnd := A_LoopField
					Break
					}
				}
			}
		If init
			{
			; If initialized, close the COM interface
			COM_Release(pwin)
			ACC_Term()
			}	
		; If no control was found
		If !IE_InjectJS_TabLock_hWnd
			{
			; If tab lock was lost, reset and recall
			If UseLock
				{
				UseLock := False
				Continue
				}
			If (Access_Tab_Title != "A")
				MsgBox, Error: Unable to locate the specified IE tab: '%Access_Tab_Title%'.
			Else
				MsgBox, Error: Active control is not an "Internet Explorer_Server" window.
			Return
			}
		;Return a comma separated list of variables in the order they were passed
		Return Ret
		}
	MsgBox, Unexpected error.
	Return
	}


Does any one know a better way to get the tab name than "document.title"????

Thanks :D

AutoHotkey Rocks!

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
the great one answered this some time ago
<!-- m -->http://www.autohotke...topic19255.html<!-- m -->
Never lose.
WIN or LEARN.

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
<!-- m -->http://www.autohotke... ... hlight=ie7<!-- m -->
Never lose.
WIN or LEARN.