Internet Explorer: focus input field

Post your working scripts, libraries and tools
User avatar
jeeswg
Posts: 5442
Joined: 19 Dec 2016, 01:58
Location: UK

Internet Explorer: focus input field

02 Dec 2018, 21:25

- This script tries to focus the main input field of a webpage.
- If this script doesn't work correctly on a particular website, you could check oWB.document.url or oWB.document.title, and use specific handling for that website.
- Here's an example of some specific handling for Google. (Although the script below did work on Google.)
Internet Explorer: Google: focus input field - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=59054

Code: Select all

;test websites:
;https://www.amazon.com/
;https://autohotkey.com/boards/viewforum.php?f=6
;https://www.bing.com/
;https://www.dailymotion.com/us
;https://duckduckgo.com/
;https://www.ebay.com/
;https://www.facebook.com/
;https://accounts.google.com/
;https://www.google.com/
;https://www.imdb.com/
;https://en.wikipedia.org/wiki/Main_Page
;https://en.wiktionary.org/wiki/Wiktionary:Main_Page
;https://www.wolframalpha.com/
;https://www.youtube.com/

;WBGet function - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=39869

q:: ;focus input field
;^#f:: ;focus input field
WinGet, hWnd, ID, A
oWB := WBGet("ahk_id " hWnd)
oElts := oWB.document.getElementsByTagName("input")
Loop, % oElts.length
{
	oElt := oElts[A_Index-1]
	if (oElt.type = "checkbox")
	|| (oElt.type = "hidden")
	|| (oElt.type = "submit")
	|| (oElt.type = "number")
	|| (oElt.type = "button")
	|| (oElt.type = "radio")
		continue
	;oElt.focus()
	oElt.select()
	;MsgBox, % "type/outerHTML:`r`n`r`n" oElt.type "`r`n`r`n" oElt.outerHTML
	break
}
oWB := oElts := oElt := ""
return
- I'd experimented in the past with scripts to do this, checking for the presence of any IDs, and checking for the word 'search' in various properties, but these approaches were unreliable. However, this time I noticed the usefulness of the 'type' property, and it gave me perfect results, it didn't fail with any of the websites I tested.

- [EDIT:] Fix:

Code: Select all

;before:
Loop, % oWB.document.getElementsByTagName("input").length
{
	oElt := oWB.document.getElementsByTagName("input")[A_Index-1]

;after:
oElts := oWB.document.getElementsByTagName("input")
Loop, % oElts.length
{
	oElt := oElts[A_Index-1]
Last edited by jeeswg on 06 Dec 2018, 21:51, edited 1 time in total.
User avatar
kczx3
Posts: 694
Joined: 06 Oct 2015, 21:39

Re: Internet Explorer: focus input field

04 Dec 2018, 19:31

That’s kind of a horrible script. You wastfully call getElementsByTagName twice when you should call it once and then loop over that stored object.

Additionally, you can use the querySelectorAll method instead to avoid the need to check the input type. In fact, you could probably do this without a loop at all.

I’d recommend you brush up on your DOM API knowledge before posting more of these kinds of tutorials.
User avatar
kczx3
Posts: 694
Joined: 06 Oct 2015, 21:39

Re: Internet Explorer: focus input field

04 Dec 2018, 19:50

There ya go. Don't think you can combine all the :not([type="<type>"]) instances unfortunatly.

Code: Select all

oWB.document.querySelector("input:not([type='checkbox']):not([type='hidden']):not([type='submit']):not([type='number']):not([type='button']):not([type='radio'])").select()
User avatar
jeeswg
Posts: 5442
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Internet Explorer: focus input field

06 Dec 2018, 18:04

- @kczx3: The getElementsByTagName code was based on some template code. While writing this script I noticed the issue. I have been/am improving various forum posts for the minor performance gains.
- Brush up on what exactly? I've responded re. getElementsByTagName, and re. querySelector/querySelectorAll, I've added some links here:
jeeswg's Internet Explorer and HTML tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=31766
- Btw the DOM API is one of the more awkward things to study, people have been saying before, we need a good list of handy methods/properties/tips, people tend to stumble across them randomly or after intensive googling. I will be adding such a list to my tutorial above.
- Also, many of the more difficult general DOM API problems that I've experienced (IE zoom, tables, svg files in GUIs), I've had to fix them myself. There has been a dearth of material on the forums here. Against that trend, however, thanks especially to A_AhkUser, also Blackholyman and jethrow. You're welcome to take some proactive action on some of these issues.
malcev
Posts: 111
Joined: 12 Aug 2014, 12:37

Re: Internet Explorer: focus input field

07 Dec 2018, 05:05

If You push f12 to open developer tools and then push f12 once again to hide it - your code will not work properly.
Therefore I think it will be better to modify WBGet function for example like this:

Code: Select all

f11:: ;internet explorer - get active window webpage title and url
ControlGetFocus, focus, A
oWB := WBGet(focus)
MsgBox, % oWB.document.title "`r`n" oWB.document.url
oWB := ""
return


;[WBGet function for AHK v1.1]
;WBGet function - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=39869

WBGet(Control="Internet Explorer_Server1", WinTitle="ahk_class IEFrame") {               ;// 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, %Control%, %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)
      }
   }
}

Return to “Scripts and Functions”

Who is online

Users browsing this forum: leosouza85, robodesign and 41 guests