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

Post your working scripts, libraries and tools for AHK v1.1 and older
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

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

Post by gregster » 16 Jun 2021, 08:23

neogna2 wrote:
16 Jun 2021, 07:47
About PageInst.Disconnect() the Chrome.ahk source says
Disconnect from the page's debug interface, allowing the instance to be garbage collected.
This method should always be called when you are finished with a page or else your script will leak memory.
But isn't the memory released when the script exits with ExitApp anyway, making Disconnect() redundant there?
At least, Disconnect() can help to close websocket connections (to pages) which are not needed anymore. Otherwise, you can run out of them pretty quick (as the default is 6, afaik, which can be changed only by registry edit) if you try to handle many different tabs, eventually resulting in a 'security error' message. This means, this might happen long before any ExitApp might be called.
Not sure, if there are other implications... but the quote seems to hint at possible memory issues at well, also connected to the websocket connection(s). It surely doesn*t hurt to disconnect, when you are done with a page - at least, if you are not planning to close the script soon.

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

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

Post by neogna2 » 16 Jun 2021, 08:45

gregster wrote:
16 Jun 2021, 08:23
At least, Disconnect() can help to close websocket connections (to pages) which are not needed anymore. Otherwise, you can run out of them pretty quick
Good point, and you have a good post about that. I meant only right before ExitApp.

There is a lot of good Chrome.ahk information spread out in this 23 page thread and other forum posts but it would probably be more accessible in as a single long FAQ post, jeeswg-style.

burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

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

Post by burque505 » 17 Jun 2021, 07:29

@neogna2, good suggestion, a FAQ would be really helpful! :thumbup:

Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

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

Post by Tre4shunter » 28 Jun 2021, 09:09

I have alot of Apps which ive built using Chrome.ahk, and the one thing which gives my users is the most headaches...is opening up the right chrome window.

If they have multiple chrome instances running for browsing, doing their other works, etc and then my app also running in chrome...there is not a good way to differentiate it on the windows task bar, as it just looks like another chrome window.

Anyone have a way around this? Im looking to essentially 'hide' the chrome taskbar icon when chrome is minimized, and have my .ahk program be the only way to open/activate it.

Thanks!

burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

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

Post by burque505 » 29 Jun 2021, 07:53

@Tre4shunter, could you use evilC's TrayMinimizer on your app?
Regards,
burque505

Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

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

Post by Tre4shunter » 29 Jun 2021, 12:47

I wasnt familiar with that @burque505, but ill check it out!

In the meantime, i solved it like so:

Rather than point to a local file, point to a hosted website - and have chrome launch it in App Mode. It will use your custom Icon, and no longer just look like another Chrome Tab.

https://imgur.com/e9CcbpC

Making the Chrome Instance:

Code: Select all

ChromeInst := new Chrome(Profile,"--app=https://www.mywebsite.com/chromeapp/index.html", "--window-size=1010,900 ",,port)
Thanks!

burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

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

Post by burque505 » 29 Jun 2021, 14:07

@Tre4shunter, that's slick! Thanks for the tip.

User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

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

Post by kczx3 » 01 Jul 2021, 10:12

@Tre4shunter The --app flag does also work with local files. Neat find by the way!

msedge.exe --app="file:///C:\Users\someUser\Desktop\test.html"

Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

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

Post by Tre4shunter » 01 Jul 2021, 10:39

Hi @kczx3 -

I think the main thing for me was that if its a local file, it still opens in on your taskbar as a 'chrome icon' rather than an 'app' with its own icon. Atleast not in my testing, maybe I missed something?

Thanks!

tre4

User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

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

Post by kczx3 » 01 Jul 2021, 10:57

Seems to open separately for me and I think it uses the favicon in the taskbar button

erohtar
Posts: 20
Joined: 24 Jun 2021, 10:53

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

Post by erohtar » 01 Jul 2021, 15:36

I honestly LOVE this library - I had it all set up for myself with some custom functions added to make my operations easier. I also got it set up so it works with portable chrome, runs in incognito each time - basically a setup that I could port to pretty much any other computer easily and have the same results.

Then I came across a particular login page that, after literally spending HOURS, made me turn to Selenium :(

Here's the page: https://login.channeladvisor.com
And here are the problems I faced: Filling in the username and password fields using chrome.ahk doesn't register with the page, so if you press the "Log In" button, it finds the fields empty.
I tried setting innerText and Value. I also dispatched multiple events like keydown, change and whatnot. Only way it detected those fields as filled was by AHK sending keys directly to that window (something about TrustedSource or something like that). But sending keys to Chrome is extremely unreliable and also needs Chrome to be active window.

Has anyone run into a similar problem or have a way for this page to recognize the values entered via Chrome.ahk?

I got Selenium to work and it's easy but not as portable, and I actually prefer using this library if possible.

songdg
Posts: 561
Joined: 04 Oct 2017, 20:04

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

Post by songdg » 01 Jul 2021, 21:18

What's the syntax of right click an element throught classname.

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

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

Post by malcev » 02 Jul 2021, 20:13

If You need trusted events then can use: Input.dispatchMouseEvent, Input.dispatchKeyEvent in Chrome DevTools Protocol.
blue83 wrote:
12 May 2021, 03:38
Do you know how to get JSESSIONID which is Httponly and secure?
Network.getCookies or Network.getAllCookies.

Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

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

Post by Tre4shunter » 06 Jul 2021, 10:36

You're absolutely right @kczx3 - i'm just a duntz and forgot to even add the favicon files to my offline version of the file.

So, long story short is that all you need to to is add the correct favicon file and add --app=yoururl.com in order to open in 'app Mode' with a custom icon.

Thanks!

erohtar
Posts: 20
Joined: 24 Jun 2021, 10:53

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

Post by erohtar » 06 Jul 2021, 17:33

Is there any way to have an ahk function called when something on the webpage in Chrome is interacted with? Maybe via some fancy eventListener?
Something like how when using Neutron library, a button click on the web app would call a function in the ahk script.
Edit: Forgot to mention that the page that'd be open in Chrome is not a website, but a local HTML document that I can modify.

Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

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

Post by Tre4shunter » 07 Jul 2021, 07:22

@erohtar -

You can do this - but afaik it has to be with console messages. Not ideal, but seems to work perfectly fine for most applications.

Example:

html

Code: Select all

<script>
function myFunc()
{
	console.log('AHK:Function');
}
</script>
AHK:

Code: Select all

;Create Chrome Inst
ChromeInst := New Chrome(...)
BoundCallback := Func("Callback").Bind()	

;Connect to PAge
PageInst := ChromeInst.GetPage(,,BoundCallBack)

;Enable Console Messages
PageInst.Call("Console.Enable")
return

Callback(event){
	GLOBAL
	;Listen for messages with your specific message and do an action
	if (Event.Method = "Console.messageAdded" && InStr(Event.params.message.text, "AHK:Function) == 1)
	{
		msgbox, Hello - i was triggered from a javascript function!
	}else if (Event.Method = "Inspector.Detached"){
		ChromeInst.Kill()
		exitapp
	}
}
**Code snippets not complete - just for reference

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

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

Post by malcev » 07 Jul 2021, 07:52

You can use native messaging or custom protocol handler.

erohtar
Posts: 20
Joined: 24 Jun 2021, 10:53

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

Post by erohtar » 07 Jul 2021, 11:54

@Tre4shunter
Thank you, I tried the code and I got partial success. After creating my chromeInstance, when I call that custom javascript function from ahk, the callback function gets triggered, but none of the event data it checks for is populated. They're all blank. Only thing that I could find was event.id which has the count of the number of times this data was written to console.

Code: Select all

	pageInst := chromeInst.GetPage(,,Func("Callback").Bind())
	
	;Enable Console Messages
	chromeInst.Call("Console.Enable")
	sleep, 500
	pageInst.Evaluate("myFunc()")
return

Callback(event){
	GLOBAL
	for key, val in event
		msgbox %key% = %val%
	msgbox % Event.params.message.text
	msgbox % Event.Method
	
	;Listen for messages with your specific message and do an action
	if (Event.Method = "Console.messageAdded" && InStr(Event.params.message.text, "AHK:Function") == 1)
	{
		msgbox, Hello - i was triggered from a javascript function!
	}else if (Event.Method = "Inspector.Detached"){
		ChromeInst.Kill()
		exitapp
	}
}
I'm probably missing something silly, but could you pls look into it?

Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

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

Post by Tre4shunter » 07 Jul 2021, 13:30

change your

ChromeInst.Call("Console.Enable") to -> PageInst.Call("Console.Enable")

Your not enabling the console on the main Chrome object, but the individual 'tabs' or pages you have connected to. That might fix your problem. to be honest, not sure why it works at all currently - idk what the ChromeInst.Call() is even doing.

-Tre4

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

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

Post by malcev » 07 Jul 2021, 13:57

Console Domain

This domain is deprecated - use Runtime or Log instead.
Deprecated
https://chromedevtools.github.io/devtools-protocol/tot/Console/

Post Reply

Return to “Scripts and Functions (v1)”