Thank you, but I don't understand. Can you show me with an example?rename Evaluate() -> Eval_obj()
and rename Eval_obj() -> Evaluate()
but it has other problems, such as the process not exiting on exit.
Class WebSocket2 v1.1 (32bit+64bit) without IE support
Re: Class WebSocket2 v1.1 (32bit+64bit) without IE support
Re: Class WebSocket2 v1.1 (32bit+64bit) without IE support
I don't understand what you guys are up to but here the full story of Eval_obj()
viewtopic.php?f=6&t=42890&start=360#p382858
viewtopic.php?f=6&t=42890&start=360#p382858
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
Re: Class WebSocket2 v1.1 (32bit+64bit) without IE support
Code: Select all
#SingleInstance Force
#include JSON.ahk
#include WS2.ahk ; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=94895
#Include WebSocketAsio.ahk ; https://github.com/pdckxd/WebSocketAsioAHK/tree/master/bin
; 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
PageInstance := ChromeInst.GetPage()
PageInstance.Call("Page.navigate", {"url": "https://autohotkey.com/"})
PageInstance.WaitForLoad()
; Execute some JavaScript
PageInstance.Evaluate("alert('Hello World!');")
; Close the browser (note: this closes *all* pages/tabs)
PageInstance.Call("Browser.close")
PageInstance.Disconnect()
ExitApp
return
; Chrome.ahk v1.2
; Copyright GeekDude 2018
; https://github.com/G33kDude/Chrome.ahk
class Chrome
{
static DebugPort := 9222
/*
Escape a string in a manner suitable for command line parameters
*/
CliEscape(Param)
{
return """" RegExReplace(Param, "(\\*)""", "$1$1\""") """"
}
/*
Finds instances of chrome in debug mode and the ports they're running
on. If no instances are found, returns a false value. If one or more
instances are found, returns an associative array where the keys are
the ports, and the values are the full command line texts used to start
the processes.
One example of how this may be used would be to open chrome on a
different port if an instance of chrome is already open on the port
you wanted to used.
```
; If the wanted port is taken, use the largest taken port plus one
DebugPort := 9222
if (Chromes := Chrome.FindInstances()).HasKey(DebugPort)
DebugPort := Chromes.MaxIndex() + 1
ChromeInst := new Chrome(ProfilePath,,,, DebugPort)
```
Another use would be to scan for running instances and attach to one
instead of starting a new instance.
```
if (Chromes := Chrome.FindInstances())
ChromeInst := {"base": Chrome, "DebugPort": Chromes.MinIndex()}
else
ChromeInst := new Chrome(ProfilePath)
```
*/
FindInstances()
{
static Needle := "--remote-debugging-port=(\d+)"
Out := {}
for Item in ComObjGet("winmgmts:")
.ExecQuery("SELECT CommandLine FROM Win32_Process"
. " WHERE Name = 'chrome.exe'")
if RegExMatch(Item.CommandLine, Needle, Match)
Out[Match1] := Item.CommandLine
return Out.MaxIndex() ? Out : False
}
/*
ProfilePath - Path to the user profile directory to use. Will use the standard if left blank.
URLs - The page or array of pages for Chrome to load when it opens
Flags - Additional flags for chrome when launching
ChromePath - Path to chrome.exe, will detect from start menu when left blank
DebugPort - What port should Chrome's remote debugging server run on
*/
__New(ProfilePath:="", URLs:="about:blank", Flags:="", ChromePath:="", DebugPort:="")
{
if (ProfilePath == "")
throw Exception("Need a profile directory")
; Verify ProfilePath
if (!InStr(FileExist(ProfilePath), "D"))
{
FileCreateDir, %ProfilePath%
if (ErrorLevel = 1)
throw Exception("Failed to create the profile directory")
}
this.ProfilePath := ProfilePath
; Verify ChromePath
if (ChromePath == "")
; By using winmgmts to get the path of a shortcut file we fix an edge case where the path is retreived incorrectly
; if using the ahk executable with a different architecture than the OS (using 32bit AHK on a 64bit OS for example)
try ChromePath := ComObjGet("winmgmts:").ExecQuery("Select * from Win32_ShortcutFile where Name=""" StrReplace(A_StartMenuCommon "\Programs\Google Chrome.lnk", "\", "\\") """").ItemIndex(0).Target
; FileGetShortcut, %A_StartMenuCommon%\Programs\Google Chrome.lnk, ChromePath
if (ChromePath == "")
RegRead, ChromePath, HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe
if !FileExist(ChromePath)
throw Exception("Chrome could not be found")
this.ChromePath := ChromePath
; Verify DebugPort
if (DebugPort != "")
{
if DebugPort is not integer
throw Exception("DebugPort must be a positive integer")
else if (DebugPort <= 0)
throw Exception("DebugPort must be a positive integer")
this.DebugPort := DebugPort
}
; Escape the URL(s)
for Index, URL in IsObject(URLs) ? URLs : [URLs]
URLString .= " " this.CliEscape(URL)
Run, % this.CliEscape(ChromePath)
. " --remote-debugging-port=" this.DebugPort
. (ProfilePath ? " --user-data-dir=" this.CliEscape(ProfilePath) : "")
. (Flags ? " " Flags : "")
. URLString
,,, OutputVarPID
this.PID := OutputVarPID
}
/*
End Chrome by terminating the process.
*/
Kill()
{
Process, Close, % this.PID
}
/*
Queries chrome for a list of pages that expose a debug interface.
In addition to standard tabs, these include pages such as extension
configuration pages.
*/
GetPageList()
{
http := ComObjCreate("WinHttp.WinHttpRequest.5.1")
StartTime := A_TickCount
while (A_TickCount-StartTime < 10*1000)
{
; It is easy to fail here because "new chrome()" takes a long time to execute.
; Therefore, it will be tried again and again within 10 seconds until it succeeds or timeout.
try
{
http.Open("GET", "http://127.0.0.1:" this.DebugPort "/json", true)
http.Send()
http.WaitForResponse(-1)
if (http.Status = 200)
break
}
Sleep, 50
}
return this.Jxon_Load(http.responseText)
}
/*
Returns a connection to the debug interface of a page that matches the
provided criteria. When multiple pages match the criteria, they appear
ordered by how recently the pages were opened.
Key - The key from the page list to search for, such as "url" or "title"
Value - The value to search for in the provided key
MatchMode - What kind of search to use, such as "exact", "contains", "startswith", or "regex"
Index - If multiple pages match the given criteria, which one of them to return
fnCallback - A function to be called whenever message is received from the page
*/
GetPageBy(Key, Value, MatchMode:="exact", Index:=1, fnCallback:="")
{
;msgbox, % PageData[Key] "`n" MatchMode "`n" Value
Count := 0
for n, PageData in this.GetPageList()
{
if (((MatchMode = "exact" && PageData[Key] = Value) ; Case insensitive
|| (MatchMode = "contains" && InStr(PageData[Key], Value))
|| (MatchMode = "startswith" && InStr(PageData[Key], Value) == 1)
|| (MatchMode = "regex" && PageData[Key] ~= Value))
&& ++Count == Index)
;msgbox, PageData[Key] "`n" MatchMode "`n" Value
return new this.Page(PageData.webSocketDebuggerUrl, fnCallback)
}
}
/*
Shorthand for GetPageBy("url", Value, "startswith")
*/
GetPageByURL(Value, MatchMode:="startswith", Index:=1, fnCallback:="")
{
return this.GetPageBy("url", Value, MatchMode, Index, fnCallback)
}
/*
Shorthand for GetPageBy("title", Value, "startswith")
*/
GetPageByTitle(Value, MatchMode:="startswith", Index:=1, fnCallback:="")
{
return this.GetPageBy("title", Value, MatchMode, Index, fnCallback)
}
/*
Shorthand for GetPageBy("type", Type, "exact")
The default type to search for is "page", which is the visible area of
a normal Chrome tab.
*/
GetPage(Index:=1, Type:="page", fnCallback:="")
{
return this.GetPageBy("type", Type, "exact", Index, fnCallback)
}
/*
Connects to the debug interface of a page given its WebSocket URL.
*/
class Page
{
Connected := False
ID := 0
Responses := []
/*
wsurl - The desired page's WebSocket URL
fnCallback - A function to be called whenever message is received
*/
__New(wsurl, fnCallback:="")
{
this.fnCallback := fnCallback
this.BoundKeepAlive := this.Call.Bind(this, "Browser.getVersion",, False)
this.tabid := tabid
; TODO: Throw exception on invalid objects
if IsObject(wsurl)
wsurl := wsurl.webSocketDebuggerUrl
ws := new WS2
ws.Connect(wsurl)
if ws.isconnected() ; checking WS connection before unloading
while( this.Connection = 1) ; confirming ws connection terminated
sleep, 10
this.ws := ws
;wsurl := StrReplace(wsurl, "localhost", "127.0.0.1")
;this.ws := {"base": this.WebSocket, "_Event": this.Event, "Parent": this}
;this.ws.__New(wsurl)
;while !this.Connected
; Sleep, 50
Sleep, 50
}
/*
Calls the specified endpoint and provides it with the given
parameters.
DomainAndMethod - The endpoint domain and method name for the
endpoint you would like to call. For example:
PageInst.Call("Browser.close")
PageInst.Call("Schema.getDomains")
Params - An associative array of parameters to be provided to the
endpoint. For example:
PageInst.Call("Page.printToPDF", {"scale": 0.5 ; Numeric Value
, "landscape": Chrome.Jxon_True() ; Boolean Value
, "pageRanges: "1-5, 8, 11-13"}) ; String value
PageInst.Call("Page.navigate", {"url": "https://autohotkey.com/"})
WaitForResponse - Whether to block until a response is received from
Chrome, which is necessary to receive a return value, or whether
to continue on with the script without waiting for a response.
*/
/*
Document()
{
New Document
}
*/
Call(DomainAndMethod, Params:="", WaitForResponse:=True)
{
ws := this.ws
if !ws.isconnected()
return
ID := this.ID += 1
msg := Chrome.Jxon_Dump({"id": ID
, "params": Params ? Params : {}
, "method": DomainAndMethod})
if ws.send(msg, strLen(msg), false)
while !ws.response
sleep, 10
response := Chrome.Jxon_Load(ws.response)
return response.result
}
/*
Run some JavaScript on the page. For example:
PageInst.Evaluate("alert(""I can't believe it's not IE!"");")
PageInst.Evaluate("document.getElementsByTagName('button')[0].click();")
*/
Eval_obj(JS)
{
response := this.Call("Runtime.evaluate",
( LTrim Join
{
"expression": JS,
"objectGroup": "console",
"includeCommandLineAPI": Chrome.Jxon_True(),
"silent": Chrome.Jxon_False(),
"returnByValue": Chrome.Jxon_False(),
"userGesture": Chrome.Jxon_True(),
"awaitPromise": Chrome.Jxon_False()
}
))
return response
}
Evaluate(JS)
{
response := this.Call("Runtime.evaluate",
( LTrim Join
{
"expression": JS,
"objectGroup": "console",
"includeCommandLineAPI": Chrome.Jxon_True(),
"silent": Chrome.Jxon_False(),
"returnByValue": Chrome.Jxon_True(),
"userGesture": Chrome.Jxon_True(),
"awaitPromise": Chrome.Jxon_False()
}
))
if (response.exceptionDetails)
{
throw Exception(Chrome.Jxon_Dump(response.result.description)) ; Chrome.Jxon_Dump(response.exceptionDetails)
}
return response.result
}
/*
Waits for the page's readyState to match the DesiredState.
DesiredState - The state to wait for the page's ReadyState to match
Interval - How often it should check whether the state matches
*/
WaitForLoad(DesiredState:="complete", Interval:=100)
{
while this.Evaluate("document.readyState").value != DesiredState
Sleep, Interval
}
/*
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.
*/
Disconnect()
{
ws := this.ws
if !ws.isConnected()
return
ws.disconnect()
ws := ""
this.ws := ""
}
}
Jxon_Load(ByRef src, args*)
{
static q := Chr(34)
key := "", is_key := false
stack := [ tree := [] ]
is_arr := { (tree): 1 }
next := q . "{[01234567890-tfn"
pos := 0
while ( (ch := SubStr(src, ++pos, 1)) != "" )
{
if InStr(" `t`n`r", ch)
continue
if !InStr(next, ch, true)
{
ln := ObjLength(StrSplit(SubStr(src, 1, pos), "`n"))
col := pos - InStr(src, "`n",, -(StrLen(src)-pos+1))
msg := Format("{}: line {} col {} (char {})"
, (next == "") ? ["Extra data", ch := SubStr(src, pos)][1]
: (next == "'") ? "Unterminated string starting at"
: (next == "\") ? "Invalid \escape"
: (next == ":") ? "Expecting ':' delimiter"
: (next == q) ? "Expecting object key enclosed in double quotes"
: (next == q . "}") ? "Expecting object key enclosed in double quotes or object closing '}'"
: (next == ",}") ? "Expecting ',' delimiter or object closing '}'"
: (next == ",]") ? "Expecting ',' delimiter or array closing ']'"
: [ "Expecting JSON value(string, number, [true, false, null], object or array)"
, ch := SubStr(src, pos, (SubStr(src, pos)~="[\]\},\s]|$")-1) ][1]
, ln, col, pos)
throw Exception(msg, -1, ch)
}
is_array := is_arr[obj := stack[1]]
if i := InStr("{[", ch)
{
val := (proto := args[i]) ? new proto : {}
is_array? ObjPush(obj, val) : obj[key] := val
ObjInsertAt(stack, 1, val)
is_arr[val] := !(is_key := ch == "{")
next := q . (is_key ? "}" : "{[]0123456789-tfn")
}
else if InStr("}]", ch)
{
ObjRemoveAt(stack, 1)
next := stack[1]==tree ? "" : is_arr[stack[1]] ? ",]" : ",}"
}
else if InStr(",:", ch)
{
is_key := (!is_array && ch == ",")
next := is_key ? q : q . "{[0123456789-tfn"
}
else ; string | number | true | false | null
{
if (ch == q) ; string
{
i := pos
while i := InStr(src, q,, i+1)
{
val := StrReplace(SubStr(src, pos+1, i-pos-1), "\\", "\u005C")
static end := A_AhkVersion<"2" ? 0 : -1
if (SubStr(val, end) != "\")
break
}
if !i ? (pos--, next := "'") : 0
continue
pos := i ; update pos
val := StrReplace(val, "\/", "/")
, val := StrReplace(val, "\" . q, q)
, val := StrReplace(val, "\b", "`b")
, val := StrReplace(val, "\f", "`f")
, val := StrReplace(val, "\n", "`n")
, val := StrReplace(val, "\r", "`r")
, val := StrReplace(val, "\t", "`t")
i := 0
while i := InStr(val, "\",, i+1)
{
if (SubStr(val, i+1, 1) != "u") ? (pos -= StrLen(SubStr(val, i)), next := "\") : 0
continue 2
; \uXXXX - JSON unicode escape sequence
xxxx := Abs("0x" . SubStr(val, i+2, 4))
if (A_IsUnicode || xxxx < 0x100)
val := SubStr(val, 1, i-1) . Chr(xxxx) . SubStr(val, i+6)
}
if is_key
{
key := val, next := ":"
continue
}
}
else ; number | true | false | null
{
val := SubStr(src, pos, i := RegExMatch(src, "[\]\},\s]|$",, pos)-pos)
; For numerical values, numerify integers and keep floats as is.
; I'm not yet sure if I should numerify floats in v2.0-a ...
static number := "number", integer := "integer"
if val is %number%
{
if val is %integer%
val += 0
}
; in v1.1, true,false,A_PtrSize,A_IsUnicode,A_Index,A_EventInfo,
; SOMETIMES return strings due to certain optimizations. Since it
; is just 'SOMETIMES', numerify to be consistent w/ v2.0-a
else if (val == "true" || val == "false")
val := %value% + 0
; AHK_H has built-in null, can't do 'val := %value%' where value == "null"
; as it would raise an exception in AHK_H(overriding built-in var)
else if (val == "null")
val := ""
; any other values are invalid, continue to trigger error
else if (pos--, next := "#")
continue
pos += i-1
}
is_array? ObjPush(obj, val) : obj[key] := val
next := obj==tree ? "" : is_array ? ",]" : ",}"
}
}
return tree[1]
}
Jxon_Dump(obj, indent:="", lvl:=1)
{
static q := Chr(34)
if IsObject(obj)
{
static Type := Func("Type")
if Type ? (Type.Call(obj) != "Object") : (ObjGetCapacity(obj) == "")
throw Exception("Object type not supported.", -1, Format("<Object at 0x{:p}>", &obj))
prefix := SubStr(A_ThisFunc, 1, InStr(A_ThisFunc, ".",, 0))
fn_t := prefix "Jxon_True", obj_t := this ? %fn_t%(this) : %fn_t%()
fn_f := prefix "Jxon_False", obj_f := this ? %fn_f%(this) : %fn_f%()
if (&obj == &obj_t)
return "true"
else if (&obj == &obj_f)
return "false"
is_array := 0
for k in obj
is_array := k == A_Index
until !is_array
static integer := "integer"
if indent is %integer%
{
if (indent < 0)
throw Exception("Indent parameter must be a postive integer.", -1, indent)
spaces := indent, indent := ""
Loop % spaces
indent .= " "
}
indt := ""
Loop, % indent ? lvl : 0
indt .= indent
this_fn := this ? Func(A_ThisFunc).Bind(this) : A_ThisFunc
lvl += 1, out := "" ; Make #Warn happy
for k, v in obj
{
if IsObject(k) || (k == "")
throw Exception("Invalid object key.", -1, k ? Format("<Object at 0x{:p}>", &obj) : "<blank>")
if !is_array
out .= ( ObjGetCapacity([k], 1) ? %this_fn%(k) : q . k . q ) ;// key
. ( indent ? ": " : ":" ) ; token + padding
out .= %this_fn%(v, indent, lvl) ; value
. ( indent ? ",`n" . indt : "," ) ; token + indent
}
if (out != "")
{
out := Trim(out, ",`n" . indent)
if (indent != "")
out := "`n" . indt . out . "`n" . SubStr(indt, StrLen(indent)+1)
}
return is_array ? "[" . out . "]" : "{" . out . "}"
}
; Number
else if (ObjGetCapacity([obj], 1) == "")
return obj
; String (null -> not supported by AHK)
if (obj != "")
{
obj := StrReplace(obj, "\", "\\")
, obj := StrReplace(obj, "/", "\/")
, obj := StrReplace(obj, q, "\" . q)
, obj := StrReplace(obj, "`b", "\b")
, obj := StrReplace(obj, "`f", "\f")
, obj := StrReplace(obj, "`n", "\n")
, obj := StrReplace(obj, "`r", "\r")
, obj := StrReplace(obj, "`t", "\t")
static needle := (A_AhkVersion<"2" ? "O)" : "") . "[^\x20-\x7e]"
while RegExMatch(obj, needle, m)
obj := StrReplace(obj, m[0], Format("\u{:04X}", Ord(m[0])))
}
return q . obj . q
}
Jxon_True()
{
static obj := {}
return obj
}
Jxon_False()
{
static obj := {}
return obj
}
}
Re: Class WebSocket2 v1.1 (32bit+64bit) without IE support
You are including WebSocketAsio.ahk and WS2.ahk you need one of them. and convert Chrome.AHK accordingly,
WebSocketAsio.dlls does/can not support Multiple Pages at once,
WS2 dllcalls get some how effected when used in AHK class, btw I wrote this class to support multiple pages which these dll are not capable
One can Write Websocket cleint Mcode like CJson.Ahk but Websocket are not that simple. There is 100% AHK websocket client that does not support Fragmented messages and I receive wrong opcode when connect it to chrome.ahk. here is the link https://github.com/gustavooavila/Autohotkey-Websocket-Client
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
Re: Class WebSocket2 v1.1 (32bit+64bit) without IE support
Since WS2 do not work as intended, I tried to implement to gustavooavila's websocket client into Chrome.ahk it work just fine with short Websocket responses and unable to parse long responses due to fragmentation,
actually websocket client receiving wrong opcode while dealing with CDP Chrome Devtools Protocol I am uploading my test
Hope someone Fix that Opcode issue in WSClient.ahk, I tried my best to resolve it.
OPcode issue:
actually websocket client receiving wrong opcode while dealing with CDP Chrome Devtools Protocol I am uploading my test
Hope someone Fix that Opcode issue in WSClient.ahk, I tried my best to resolve it.
OPcode issue:
Spoiler
attachment: you can connect to multiple pages- Attachments
-
- Chrome Websocket test.zip
- (131.8 KiB) Downloaded 69 times
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
Re: Class WebSocket2 v1.1 (32bit+64bit) without IE support
Thanks for the reply, I am using ExitApp() for the time being to solve the problem of the process not being able to exit.Xeo786 wrote: ↑25 Mar 2022, 03:22You are including WebSocketAsio.ahk and WS2.ahk you need one of them. and convert Chrome.AHK accordingly,
WebSocketAsio.dlls does/can not support Multiple Pages at once,
WS2 dllcalls get some how effected when used in AHK class, btw I wrote this class to support multiple pages which these dll are not capable
One can Write Websocket cleint Mcode like CJson.Ahk but Websocket are not that simple. There is 100% AHK websocket client that does not support Fragmented messages and I receive wrong opcode when connect it to chrome.ahk. here is the link https://github.com/gustavooavila/Autohotkey-Websocket-Client
Code: Select all
ExitApp() {
Menu, Tray, NoIcon
Process, Close, % DllCall("GetCurrentProcessId")
}
Return to “Scripts and Functions (v1)”
Who is online
Users browsing this forum: yxldh and 91 guests