[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!

18 Jul 2021, 17:15

malcev wrote:
18 Jul 2021, 16:24
No, this will not modify evaluate function, but increase speed of chrome response decoding
GeekDude uses Coco json parser which is very slow.
@malcev Just did multiple tests, and it is indeed even faster than the clipboard trick to return values
:bravo: @teadrinker

Are there any disadvantages?

:think: Maybe time to try to update the github page with teadrinker`s Json and Jackie Sztuk improvements to make it work with edge .
geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

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

20 Jul 2021, 11:42

I tend to stay away from JavaScript COM for JSON parsing, as a lot of implementations are vulnerable to code injection and could lead to broken behavior, or at worst compromise. The object it kicks out is a COM object hosted by Internet Explorers' JavaScript engine, and so can't be used in all the same contexts as a native AutoHotkey object. For similar reasons, the trick doesn't work as well in reverse--you can't easily push AHK objects to the JavaScript engine for serialization.

I'm also wanting to move as much away as possible from embedded Trident/IE given its deprecation. The parts of it Chrome.ahk uses shouldn't go away even when IE gets "removed" in the upcoming months, but building new stuff on old outdated platforms is not great.

I'm currently developing a new JSON library with better performance characteristics than Coco's while still using real AHK objects. Decoding can be much faster depending on the object structure (more performance gains yet to come) and encoding is easily in the hundreds-times-faster range. Once I get this library up to release, and get my new upcoming websockets library working (I'm not sure yet if the new approach will even work), I'll merge them into Chrome.ahk, make a release, and start working on the next major Chrome.ahk release (and Positron UI library, which uses the Edge-based WebView2 runtime for rendering).

The JSON library can be found here, as a pre-release: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=92320
gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

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

20 Jul 2021, 11:49

GeekDude wrote:
20 Jul 2021, 11:42
Great to here. Already noticed the new json approach you posted.
I am looking forward to a new Chrome.ahk release. Thank you for working on it!
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

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

20 Jul 2021, 11:55

Disadvantage of Your json parser is that it works only with 64bit.
You can parse json without eval with JSON.parse, like this:

Code: Select all

JsonToAHK_parse(json, rec := false) {
   static doc := ComObjCreate("htmlfile")
         , __ := doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
         , JS := doc.parentWindow
   if !rec
      obj := %A_ThisFunc%(JS.JSON.parse(json), true)
   else if !IsObject(json)
      obj := json
   else if JS.Object.prototype.toString.call(json) == "[object Array]" {
      obj := []
      Loop % json.length
         obj.Push( %A_ThisFunc%(json[A_Index - 1], true) )
   }
   else {
      obj := {}
      keys := JS.Object.keys(json)
      Loop % keys.length {
         k := keys[A_Index - 1]
         obj[k] := %A_ThisFunc%(json[k], true)
      }
   }
   Return obj
}
Also it is possible to use JSMN, that should be fastest and works on both bitness.
Just need to convert from AutoIt.
https://www.autoitscript.com/forum/topic/148114-a-non-strict-json-udf-jsmn/
geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

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

20 Jul 2021, 15:42

Yes, you can call to the JavaScript like that to avoid code injection. It's a good implementation, and it looks like it also recursively reconstructs the JS object into an AHK object. That's good for some reasons, but also can be very slow. When I'm ready for the 1.0 release of cJson I suspect it may be faster than that snippet. And, as mentioned, simple JS implementations only parse--a serializer will take additional code and will be slow compared to the cJson implementation. Then there's the problems relating to nulls and booleans losing their type information.

Working independently, I've built a library that addresses some of those problems, available here, but it's slower than cJson will be and still depends on Internet Explorer.

JSMN cannot be compiled for AHK using any of the existing MCode implementations. You're welcome to try. If you manage it I'll grovel for your secrets haha. I may be able to steal the compiled code from the AutoIt script there, but I'd caution against borrowing random machine code. Supposing I did (ignoring copyright violations), I'd also be unable to effectively modify it to add support for nonstandard extensions like inline comments (planned for cJson).

We're building an entirely new MCode stack for AHK, of which cJson is the first project utilizing it. This new MCode stack actually could compile JSMN for use by AHK, but only in 64-bit right now. JSMN doesn't generate native objects, limiting the usefulness of the library, and any code to convert its output to AHK objects will very likely have more overhead than cJson doing it natively (at the 1.0 release).

Eventually, we want to provide a custom COM object, implemented by embedded MCode, that will provide real key hashing, no "magic" key values that can break AHK like _NewEnum, and integration with cJson that will provide near-instant parsing and serializing. This is not planned for the 1.0 release, but when it comes it will make all other implementations look like snails in comparison while still addressing problems like type hinting and unwieldy foreign interfaces.

No 32-bit support, which may still eventually happen, is a small enough price to pay for most use cases especially now. Windows 11 will no longer support 32-bit processors (though will keep the 32 bit compatibility layer). Very few legacy AHK libraries have not been ported to 64 bit. I may be mistaken, but as I recall even 32-bit MS Office provides 64-bit COM objects. I think time spent trying to maintain 32-bit compatibility in new software may be just as well spent updating old code for 64-bit compatibility. If it's a deal breaker for you, you can always swap out the JSON module with something else.
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

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

20 Jul 2021, 16:12

I see. I think it would be very good if script detects ahk version and depends on it chooses JSON module.
geek
Posts: 1052
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

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

20 Jul 2021, 18:31

teadrinker wrote:
20 Jul 2021, 17:51
By the way, Windows 10 supports JSON natively.
That's very interesting, though there's something to be said about a more native implementation with a custom fit to the language. I'm also not so sure about relying on the UWP platform as far as both interoperability and compatibility with third party winapi solutions like Wine.

Good to see it coming in as a first class citizen in any case.
william_ahk
Posts: 481
Joined: 03 Dec 2018, 20:02

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

20 Jul 2021, 21:35

Is it possible to suppress the websocket error popup?
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

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

21 Jul 2021, 03:17

william_ahk wrote:
20 Jul 2021, 21:35
Is it possible to suppress the websocket error popup?
Not always the best practice, but you can put any code in a try statement. Catch can afterwards be used to run if an error occured. If you put an Exit command in the Catch, the current thread will be destroyed, but the script will still run. You could also edit Chrome.ahk and put an Exit command just before the line "throw Exception("Websocket Error!")".

Code: Select all

Try, {
	; put in the code that can result in an error
	throw " This line gives an error"
}
Catch e {
	if (e="Websocket Error!"){
		; continue script?
	}
	else{
		TrayTip, Error, %e%
		Exit
	}
}
william_ahk
Posts: 481
Joined: 03 Dec 2018, 20:02

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

21 Jul 2021, 05:01

@AHK_user Thanks! Sometimes when I close the browser outside the flow the websocket error would popup because the page was still connected.
eekhelpspike
Posts: 20
Joined: 05 Jul 2015, 17:51

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

05 Aug 2021, 08:48

Can someone give me an example of how to use the page.captureScreenshot? The "Capture full size screenshot" command from DevTools console works well-- I was hoping it behaves like that?
User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

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

05 Aug 2021, 10:31

eekhelpspike wrote:
05 Aug 2021, 08:48
Can someone give me an example of how to use the page.captureScreenshot? The "Capture full size screenshot" command from DevTools console works well-- I was hoping it behaves like that?
Try this:

Code: Select all

captureSnapshot(PageInst, filename) {    ; EXPERIMENTAL
    data := PageInst.Call("Page.captureSnapshot", {"format": "mhtml"}).data
    F := FileOpen(filename, "w"), F.Write(data), F.Close()
}
eekhelpspike
Posts: 20
Joined: 05 Jul 2015, 17:51

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

05 Aug 2021, 11:07

Xtra wrote:
05 Aug 2021, 10:31
Try this:

Code: Select all

captureSnapshot(PageInst, filename) {    ; EXPERIMENTAL
    data := PageInst.Call("Page.captureSnapshot", {"format": "mhtml"}).data
    F := FileOpen(filename, "w"), F.Write(data), F.Close()
}
Works perfectly! You're the best, thank you. I've been messing with it, trying to save as .png but I've no luck so far. It's only 88 web pages so mhtml will suffice.
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

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

05 Aug 2021, 11:44

eekhelpspike, You can look at examples how to export to pdf and then try to call captureScreenshot.
eekhelpspike
Posts: 20
Joined: 05 Jul 2015, 17:51

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

05 Aug 2021, 12:34

malcev wrote:
05 Aug 2021, 11:44
eekhelpspike, You can look at examples how to export to pdf and then try to call captureScreenshot.
Thanks so much, Xtra got me there.

My initial excuse was I wanted something that I could record my Vudu purchase history with. Fixing to combine my Vudu/FandangoNow accounts and I'm sure I'll have to fight with them about something. There's a great extension for it that puts it in CSV, but I wanted something that looked more official.

I'm sure I'll use it many times in the future for other things. Thanks guys!
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

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

06 Aug 2021, 14:54

@eekhelpspike, I believe @malcev meant something like this, which exports it to a PNG:
Spoiler
Regards,
burque505

P.S. Also working headless for me.
eekhelpspike
Posts: 20
Joined: 05 Jul 2015, 17:51

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

06 Aug 2021, 14:59

burque505 wrote:
06 Aug 2021, 14:54
@eekhelpspike, I believe @malcev meant something like this, which exports it to a PNG P.S. Also working headless for me.
Gotcha. I'll check it out, thanks so much!
Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

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

07 Aug 2021, 16:54

I've been trying to find a reliable way to activate a specific Chrome tab with AutoHotkey and I'm surprised how difficult it seems to be. Preferrably, I would like to not rely on Ctrl + Tab cycles to find the tab and it should account for the fact that I may have more than one Chrome instance (window) open.

I came across this library, but it seems to be too advanced for my limited knowledge of AutoHotkey. Maybe you guys could help me: Is there any way to set a hotkey that would look for all the existing Chrome tabs across multiple Chrome windows and activate that tab (if it doesn't exist, it would open that tab). Is such a thing possible with this library?
Tre4shunter
Posts: 139
Joined: 26 Jan 2016, 16:05

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

11 Aug 2021, 07:43

I've been using the tdChrome version of Chrome.ahk and noticed something about how the LightJson class interprets numbers/numbers as strings.

Code: Select all

jsonobj := {"Key 1":044505,"Key 2":"00540"}
jsonstring := LightJson.Stringify(jsonobj)
msgbox % jsonstring
You'll notice that the code above results in...Key1 -> 44505 and Key2 -> completely wrong number

Using the Jxon_Dump function (Originally included with Chrome.ahk the results are:
Key1 -> 44505 and Key2 -> "00540"

My desired output would be that Key1 -> 044505 and Key2 -> "00540"...it seems that both functions interpret the values differently. Is this an AHK issue, JSON issue...or an issue of my understanding how the AHK object is being translated to the JSON string?

Thanks!

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: MiM and 119 guests