Chrome.ahk script that stores and compares class.innerText Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
rakibkhan7496
Posts: 8
Joined: 11 May 2021, 06:53
Contact:

Chrome.ahk script that stores and compares class.innerText

25 Jul 2021, 13:11

Hi, :wave:
How to pass JavaScript variable, array, objects to/from Auto Hotkey in Chrome.ahk

Code: Select all

#NoEnv
SetBatchLines, -1
#Include <Chrome> 
#SingleInstance, force
FileCreateDir, ChromeProfile
chrome := new Chrome("ChromeProfile")
page := chrome.GetPage()
if !(page := chrome.GetPage())
{
	MsgBox, Could not retrieve page!
	chrome.Kill()
}
else
{
	page.Call("Page.navigate", {"url": "https://www.daraz.com.bd/smartphones/"})
	JS =
	(;Javascript Code
		var cart = [];
		var card = document.querySelectorAll(".c16H9d");
		for (var i=0; i<card.length; i++){
				cart.push(card[i].innerText);
		}

	)
	page.WaitForLoad()
	page.Evaluate(JS)
	MsgBox, % page.Evaluate("JSON.stringify(cart)").value
}
ExitApp
return
In this example code, I can't pass cart array to ahk, I tried
  • 1. cart := page.Evaluate(JS).value
    2. cart := page.Evaluate(JS).key
    3. cart := page.Evaluate(JS).data
    4. cart := page.Evaluate(JS)
    5. adding function to js
    6. searching answer everywhere possible
    7. eventcallback included with Chrome.ahk
I want to check certain websites for product with class loop. Than check every products if they already owned by me with the help of json or similar technique, if not owned I want to purchase theme. Add new purchased products to database.
I just need a way to pass variable to ahk or write json from javascript.
Every pointer is appropriated
Thank you for reading.
Last edited by rakibkhan7496 on 25 Jul 2021, 23:04, edited 2 times in total.
teadrinker
Posts: 4330
Joined: 29 Mar 2015, 09:41
Contact:

Re: Chrome.ahk script that stores and compares class.innerText

25 Jul 2021, 14:40

rakibkhan7496 wrote:

Code: Select all

    JS =
    (;Javascript Code
        var cart = [];
        var card = document.querySelectorAll(".card");
        for (var i=0; i<card.length; i++){
                cart.push(card[i].innerText);
        }return cart
    )
First of all this is invalid JavaScript code, try running it in Chrome console.
rakibkhan7496
Posts: 8
Joined: 11 May 2021, 06:53
Contact:

Re: Chrome.ahk script that stores and compares class.innerText

25 Jul 2021, 23:12

teadrinker wrote:
25 Jul 2021, 14:40
First of all this is invalid JavaScript code, try running it in Chrome console.
Well it was an actual javascript which works and run in console, for privacy reason I didn't give you website address,
I updated my code with actually working code.
Code above returns cart value as string but I want to pass variable, constant, array to autohotkey, just show me how to pass var x = 2 or var arr = ["1","2","3"]
User avatar
Xeo786
Posts: 760
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: Chrome.ahk script that stores and compares class.innerText  Topic is solved

26 Jul 2021, 02:34

For Pass array/object from chrome to ahk, you need to change parameters of Runtime.evaluate specifically parameter returnByValue , you need to change it to true like this "returnByValue": Chrome.Jxon_True() in chrome.ahk, therefore I made some changes into chrome.ahk by just creating eval_obj()

read my following post, there I passed a function to read a table and save it as array and using eval_obj() to extract it
https://www.autohotkey.com/boards/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

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: OrangeCat and 297 guests