[Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No IE!

Post your working scripts, libraries and tools for AHK v1.1 and older
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Post by AHK_user » 09 Mar 2023, 03:13

I have the impression it is not related to redirecting internet explorer to edge, but with the latest Chrome update.

I have systems that are still working, with the redirecting of internet explorer to Edge.

jekko1976
Posts: 97
Joined: 10 Oct 2014, 07:03

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Post by jekko1976 » 09 Mar 2023, 08:44

the problem is Chrome.
I downgraded it and the library came back to work.
I followed this tutorial:

https://browserhow.com/how-to-downgrade-and-install-older-version-of-chrome/

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Post by malcev » 09 Mar 2023, 09:11

Run with "--remote-allow-origins=*" flag.

neogna2
Posts: 586
Joined: 15 Sep 2016, 15:44

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Post by neogna2 » 09 Mar 2023, 09:54

neogna2 wrote:
01 Mar 2023, 14:43
It appears the recent Windows update related to Internet Explorer made Chrome.ahk stop working on a Windows 10 PC I have. [...]
Chrome starts but I then get an error popup saying "Websocket Error!".
malcev wrote:
09 Mar 2023, 09:11
Run with "--remote-allow-origins=*" flag.
Thanks. I confirm that malcev's solution works for me. Example syntax with that flag
Chrome(ChromeProfile, "https://www.google.com", "--remote-allow-origins=*")

So like @AHK_user suggested it appears to be caused by some change in Chrome, not in Windows.

laeal
Posts: 2
Joined: 09 Mar 2023, 02:57

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Post by laeal » 09 Mar 2023, 17:34

i also confirm this works, thanks @malcev and @neogna2 for elaboration!

vadus
Posts: 11
Joined: 26 May 2019, 13:02

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Post by vadus » 10 Mar 2023, 16:20

Yes, it works:

Code: Select all

ChromeInst := new Chrome(ChromeProfile, "https://www.google.com", "--remote-allow-origins=*")
It works:

Code: Select all

if (Chromes := Chrome.FindInstances())
	ChromeInst := {"base": Chrome, "DebugPort": Chromes.MinIndex()}	; or if you know the port:  ChromeInst := {"base": Chrome, "DebugPort": 9222}

URL := ChromeInst.GetPageList()[1].url
Title := ChromeInst.GetPageList()[1].Title
MsgBox URL: %URL%`nTitle: %Title%
But this one doesn't work like before:

Code: Select all

if (Chromes := Chrome.FindInstances())
	ChromeInst := {"base": Chrome, "DebugPort": Chromes.MinIndex()}	; or if you know the port:  ChromeInst := {"base": Chrome, "DebugPort": 9222}

if !(PageInst := ChromeInst.GetPage())
{
	ChromeInst.Kill()
}

PageUrl := PageInst.Evaluate("document.URL;").Value
MsgBox PageUrl: %PageUrl%
What can be done to make it work again?

thaihoa3189
Posts: 35
Joined: 23 Mar 2022, 22:10

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Post by thaihoa3189 » 12 Mar 2023, 21:57

@malcev
thank u, now it works again with 2 flags "--remote-debugging-port=9222 --remote-allow-origins=*"

lassos
Posts: 4
Joined: 14 Mar 2023, 15:17

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Post by lassos » 14 Mar 2023, 15:20

hello, since a few day authotkey does not working anymore with chrome.ahk, any script or simple test script quits with websocket error thrown in chrome.ahk:

any help ?

Code: Select all

			else if (EventName == "Error")
			{
				throw Exception("Websocket Error!")
			}

Code: Select all

#Include ./lib/Chrome.ahk

; Create an instance of the Chrome class using
; the folder ChromeProfile to store the user profile
FileCreateDir, ChromeProfile
ChromeInst := new Chrome("ChromeProfile")

; Connect to the newly opened tab and navigate to another website
; Note: If your first action is to navigate away, it may be just as
; effective to provide the target URL when instantiating the Chrome class
PageInst := ChromeInst.GetPage()
PageInst.Call("Page.navigate", {"url": "https://autohotkey.com/"})
PageInst.WaitForLoad()

; Execute some JavaScript
PageInst.Evaluate("alert('Hello World!');")

; Close the browser (note: this closes *all* pages/tabs)
PageInst.Call("Browser.close")
PageInst.Disconnect()

ExitApp
return

lassos
Posts: 4
Joined: 14 Mar 2023, 15:17

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Post by lassos » 14 Mar 2023, 15:30

tried --remote-allow-origins=* but do not work

Gh0sTG0
Posts: 55
Joined: 25 Jun 2018, 07:58

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Post by Gh0sTG0 » 14 Mar 2023, 15:53

@lassos
As for shortcut version for me works:

Code: Select all

"C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222 --remote-allow-origins=* --user-data-dir="D:\ahk_chrome_profile"

vadus
Posts: 11
Joined: 26 May 2019, 13:02

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Post by vadus » 14 Mar 2023, 16:35

Thanks @malcev and @Gh0sTG0, finally everything works, including this code

Code: Select all

if (Chromes := Chrome.FindInstances())
	ChromeInst := {"base": Chrome, "DebugPort": Chromes.MinIndex()}	; or if you know the port:  ChromeInst := {"base": Chrome, "DebugPort": 9222}

if !(PageInst := ChromeInst.GetPage())
{
	ChromeInst.Kill()
}

PageUrl := PageInst.Evaluate("document.URL;").Value
MsgBox PageUrl: %PageUrl%

lassos
Posts: 4
Joined: 14 Mar 2023, 15:17

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No Selenium!

Post by lassos » 15 Mar 2023, 02:02

Gh0sTG0 wrote:
14 Mar 2023, 15:53
@lassos
As for shortcut version for me works:

Code: Select all

"C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222 --remote-allow-origins=* --user-data-dir="D:\ahk_chrome_profile"
Wow, thanks, this works for me too .-) , pretty cool, it is working again. spent so much time in my automating browser website routines with chrome.ahk. allready considered to develop chrome extension. now i do not need :bravo:

geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No IE!

Post by geek » 20 Mar 2023, 15:19

Chrome.ahk 1.3.0 has been released. It uses new websocket code, and new JSON code. Please test and review.

Gh0sTG0
Posts: 55
Joined: 25 Jun 2018, 07:58

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No IE!

Post by Gh0sTG0 » 20 Mar 2023, 15:24

geek wrote:
20 Mar 2023, 15:19
Chrome.ahk 1.3.0 has been released. It uses new websocket code, and new JSON code. Please test and review.
Wow! After 5 years! Big news!
Some changes in usage from previous one?
PS Still v1?

geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No IE!

Post by geek » 20 Mar 2023, 17:41

Not really, just aimed at performance and bug fixes. thqby has a V2 port which I've heard ok things about.

I don't really have much time to write AHK code anymore, between a full time job and expenses 😅

Friendly reminder I do accept donations, up to date information will always be at the bottom of https://geekdude.io

thaihoa3189
Posts: 35
Joined: 23 Mar 2022, 22:10

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No IE!

Post by thaihoa3189 » 22 Mar 2023, 04:59

@geek
I downloaded the 1.3 source code (Zip) but it is missing websocket.ahk and cJson.ahk. I had to manually copy and paste those 2 files into a single chrome.ahk file

Gh0sTG0
Posts: 55
Joined: 25 Jun 2018, 07:58

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No IE!

Post by Gh0sTG0 » 22 Mar 2023, 05:01

thaihoa3189 wrote:
22 Mar 2023, 04:59
@geek
I downloaded the 1.3 source code but it is missing websocket.ahk and cJson.ahk. I had to manually copy and paste those 2 files into a single chrome.ahk file
Go to github, there should be full file
( in 1st post there is Releases link )

thaihoa3189
Posts: 35
Joined: 23 Mar 2022, 22:10

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No IE!

Post by thaihoa3189 » 22 Mar 2023, 20:25

@Gh0sTG0
I saw it. Thank you

cnelutescu
Posts: 1
Joined: 06 Apr 2023, 13:21

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No IE!

Post by cnelutescu » 06 Apr 2023, 14:01

geek wrote:
20 Mar 2023, 15:19
Chrome.ahk 1.3.0 has been released. It uses new websocket code, and new JSON code. Please test and review.
I really appreciate your work! Thanks, GeekDude. :bravo:

Because sometimes, in rare occasions, the script freeze in Call function (in Chrome.ahk) waiting for response from Chrome, I moved the instruction to clear the flag (this.responses[ID] := False) before instruction to send command to Chrome: this.ws.Send(Chrome.Jxon_Dump….
Because multitasking environment, after sending to Chrome a command, sometimes Chrome can respond instantly, stop execution of our AHK script, execute the command and set the flag to True. Than resume our AHK script, but the script was setting the flag from True to False and freeze waiting.

Now, my Call function looks like this and works like a charm:

Code: Select all

		Call(DomainAndMethod, Params:="", WaitForResponse:=True)
		{
			if !this.Connected
				throw Exception("Not connected to tab")
			
			 ;Use a temporary variable for ID in case more calls are made
			 ;before we receive a response.
			ID := this.ID += 1
			this.responses[ID] := False				;<----- clear the flag was moved here
			this.ws.Send(Chrome.Jxon_Dump({"id": ID
			, "params": Params ? Params : {}
			, "method": DomainAndMethod}))
			
			if !WaitForResponse
				return
			
			 ;Wait for the response
			;----- this.responses[ID] := False			;-----> clear the flag NOT here
			while !this.responses[ID]
				Sleep, 50
			
			 ;Get the response, check if it's an error
			response := this.responses.Delete(ID)
			if (response.error)
				throw Exception("Chrome indicated error in response",, Chrome.Jxon_Dump(response.error))
			
			return response.result
		}

acessonegato
Posts: 2
Joined: 09 Apr 2023, 04:37

Re: [Library] Chrome.ahk - Automate Google Chrome using native AutoHotkey. No IE!

Post by acessonegato » 10 Apr 2023, 04:22

Hi,
Can chrome be started in minimize and/or hidden mode?
If yes how and would all the commands work?

Post Reply

Return to “Scripts and Functions (v1)”