thqby / ahk2_lib

MIT License
215 stars 29 forks source link

Testing Chrome.ahk #3

Closed dmtr99 closed 2 years ago

dmtr99 commented 2 years ago

I tested some v2 Chrome.ahk code, and It seems to be working great, thanks a lot for your amazing and usefull work :D

Currently the JSON is returning a map, but would it not be more logically to return an object? I found it strange to get maps back. I know V1 mixed Maps and Objects. This would make it easier to change some old scripts to v2.

Working example v2:

ChromeInst :=  Chrome()
ChromeTab := ChromeInst.GetPage()
result := ChromeTab.Evaluate("window.location.href")
msgbox(result["value"])

Normally I would think that result.value should be the correct syntax.

thqby commented 2 years ago

Map supports case sensitivity, while object is not case sensitive, although it is rare to have keys with different cases at the same time. And run Map.Prototype.DefineProp('__get', {call: Map.Prototype.Get}), you can use map just like object.

dmtr99 commented 2 years ago

Interesting trick, thanks a lot. Strange how it stays case sensitive, or you need to set the CaseSense Off before filling the object. in some strange cases you would indeed lose data and that is indeed a disadvantage to this approach, and maybe it is better to keep the code cleaner.

oMap := Map()
oMap.CaseSense := "Off"
oMap.Set("Red", "ff0000" , "Green", "00ff00", "Deep Blue", "0000ff")
oMap.DefineProp('__get', { call: oMap.Get })
MsgBox("oMap.red:" oMap.red "`noMap.Red:" oMap.Red)
dmtr99 commented 2 years ago

I however have another question, I want to connect to chrome without opening a new tab, but it seems this version always opens a new tab.

If I remember correctly, I used to connect to Chrome with: PageInst := Chrome.GetPage()

But I now get the error "This value of type ''Class" has no method named "GetPage".

This code seems to work, but always opens a new tab:

ChromeInst :=  Chrome()
ChromeTab := ChromeInst.GetPage()
thqby commented 2 years ago

Use an existing browser that starts in debug mode?

dmtr99 commented 2 years ago

Yes, I have the habit to run my browser always in debug mode. In the V1 version you could connect to the existing debug window with this code.

thqby commented 2 years ago

This problem has been fixed. Chrome.FindInstances

dmtr99 commented 2 years ago

Tested and approved.

Just for clarity, my previous code still does not work.

this seems to work perfectly:

ChromeInst := Chrome.FindInstances()
ChromeTab := ChromeInst.GetPage()
result := ChromeTab.Evaluate("window.location.href")
msgbox(result["value"])