Internet Explorer: Google: focus input field

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Internet Explorer: Google: focus input field

14 Nov 2018, 12:18

- [EDIT:] This script is specifically for Google, but for a generic script that tries to focus the main input field on any webpage, see here:
Internet Explorer: focus input field - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=59571

- I noticed in the last few days that this line of code, that had worked for a long time on Google, now only works on the Google home page, and no longer works on search result pages.
oWB.document.getElementById("lst-ib").focus()

- Here's a fix.

Code: Select all

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

^#f:: ;internet explorer - focus input field
WinGet, hWnd, ID, A
oWB := WBGet("ahk_id " hWnd)
vUrl := oWB.document.url
;vTitle := oWB.document.title

;oElt := oWB.document.activeElement
;MsgBox, % Format("id: {}`r`n" "tag: {}`r`n" "class: {}", oElt.id, oElt.tagName, oElt.className)
;oElt := ""

if InStr(vUrl, ".google.")
{
	if IsObject(oWB.document.getElementById("lst-ib"))
		oWB.document.getElementById("lst-ib").focus()
	else
		oWB.document.getElementsByTagName("input").1.focus()
}
oWB := ""
return
- Btw I needed to check if an element with a specific ID existed, I used IsObject, but I wondered if there were any other ways.
Last edited by jeeswg on 03 Dec 2018, 15:17, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: Internet Explorer: Google: focus input field

14 Nov 2018, 14:15

@jeeswg, thanks again for staying on top of this.
Regards,
burque505
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Internet Explorer: Google: focus input field

28 Nov 2018, 21:54

- I found that it wasn't working on the Google home page or on image search, so I came up with this instead. Before I started this thread, my previous code had been working for a long time (possibly years).

Code: Select all

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

^#f:: ;internet explorer - focus input field
WinGet, hWnd, ID, A
oWB := WBGet("ahk_id " hWnd)
vUrl := oWB.document.url

if InStr(vUrl, ".google.")
	oWB.document.getElementsByClassName("gLFyf gsfi").0.focus()

oWB := ""
return
- @burque505: no problem.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
rommmcek
Posts: 1473
Joined: 15 Aug 2014, 15:18

Re: Internet Explorer: Google: focus input field

30 Nov 2018, 00:38

One might want insert also:

Code: Select all

if InStr(vUrl, ".youtube.")
	oWB.document.getElementsByClassName("search-term masthead-search-renderer-input yt-uix-form-input-bidi").0.select()
before oWB := "" for YouTube.

P.s.: I prefer .select() over .focus()
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Internet Explorer: Google: focus input field

02 Dec 2018, 21:20

Hello rommmcek! Why select over focus? Thanks.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
rommmcek
Posts: 1473
Joined: 15 Aug 2014, 15:18

Re: Internet Explorer: Google: focus input field

03 Dec 2018, 08:41

- .select() is better for a new search if there is already an eventual previous entry (usually what I need), however there is almost no disadvantage for an edit of a previous entry (except maybe for prepending XOR appending additional search criterium/criteria to previous entry)

bye!

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 63 guests