Can I simplify function? Concatenating parameter? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
bumbo
Posts: 12
Joined: 31 Jul 2020, 11:39

Can I simplify function? Concatenating parameter?

Post by bumbo » 23 Jul 2021, 11:07

Hello,

I have a function that is retrieving data on a page. And I have a setTimer calling a function object referencing the function. It's all working but I'd like to simplify the function object if possible (I'm trying to simplify so I can diagnose/isolate some other issues in my script :crazy: )

Code: Select all

url1 := "https://www.timeanddate.com/worldclock/usa/new-york-state"
ChromeInstance := new Chrome(profile, [url1], "")
PageInstance1 := ChromeInstance.GetPageByURL(url1)

FunctionGet(page,xpath)
{
	%page%.Call("Page.bringToFront").WaitForLoad
	myVar := xpath
	
	tooltip, %myVar%
}

fn_nyc := Func("FunctionGet").bind(PageInstance1,PageInstance1.evaluate("document.evaluate('//*[@id=""ct""]', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.innerHTML").value)
SetTimer, %fn_nyc%, 2000
So the code becomes something like this where the function object is easier to read/passing parameters is easier.
I'm sorry it's kind of a syntax hell...

Code: Select all

FunctionGet(page,xpath)
{
	%page%.Call("Page.bringToFront").WaitForLoad
	myVar := %page%.evaluate("document.evaluate('%xpath%', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.innerHTML").value
	
	tooltip, %myVar%
}

fn_nyc := Func("FunctionGet").bind(PageInstance1,//*[@id=""ct""])
SetTimer, %fn_nyc%, 2000
bumbo
Posts: 12
Joined: 31 Jul 2020, 11:39

Re: Can I simplify function? Concatenating parameter?  Topic is solved

Post by bumbo » 23 Jul 2021, 11:17

Code: Select all

FunctionGet(page,xpath)
{
	%page%.Call("Page.bringToFront").WaitForLoad
	myVar := %page%.evaluate("document.evaluate('" xpath "', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.innerHTML").value
	tooltip, %myVar%
}

fn_nyc := Func("FunctionGet").bind("PageInstance1","//*[@id=""ct""]")
SetTimer, %fn_nyc%, 2000
OHHHH!!! The above is working now! Except I have to manually pass the parameter into the function :crazy:
My question is alot easier now I think. How can I just use the parameter xpath instead of //*[@id=""ct""] in the function?

Oops I got it. I was stuck on this for days before :crazy: Code is updated so any future people can see it!
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Can I simplify function? Concatenating parameter?

Post by Xtra » 23 Jul 2021, 12:35

Code: Select all

FunctionGet(page,xpath)
{
	page.Call("Page.bringToFront")
	myVar := page.evaluate("document.evaluate('" xpath "', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.innerHTML").value
	tooltip, %myVar%
}

fn_nyc := Func("FunctionGet").bind(PageInstance1,"//*[@id=""ct""]")
SetTimer, %fn_nyc%, 2000
% not needed if you bind the var PageInstance1 instead of the string "PageInstance1"
Not sure where you got .WaitForLoad ?

HTH
Post Reply

Return to “Ask for Help (v1)”