Possible for ChromeExecute to pass var back to MS?

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
ocnuybear
Pro Scripter
Posts: 100
Joined: Sun Jul 15, 2018 5:14 pm

Possible for ChromeExecute to pass var back to MS?

Post by ocnuybear » Tue Mar 29, 2022 5:30 am

Need to collect info using javascript, but cannot find a way to pass this info back to MS please?

User avatar
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Possible for ChromeExecute to pass var back to MS?

Post by Grovkillen » Tue Mar 29, 2022 6:37 am

Put the data in a div or similar.
Let>ME=%Script%

Running: 15.0.27
version history

ocnuybear
Pro Scripter
Posts: 100
Joined: Sun Jul 15, 2018 5:14 pm

Re: Possible for ChromeExecute to pass var back to MS?

Post by ocnuybear » Tue Mar 29, 2022 7:47 am

Yes I did something similar, you just have to first read in the custom created div afterwards, which is not ideal, but should work or maybe put it in the clipboard, but that also has it downsides.

For now that should work,
Thank you Grovkillen

User avatar
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Possible for ChromeExecute to pass var back to MS?

Post by Grovkillen » Tue Mar 29, 2022 11:08 am

Why isn't it ideal? I do this and it's very straight forward.
Let>ME=%Script%

Running: 15.0.27
version history

ocnuybear
Pro Scripter
Posts: 100
Joined: Sun Jul 15, 2018 5:14 pm

Re: Possible for ChromeExecute to pass var back to MS?

Post by ocnuybear » Tue Mar 29, 2022 2:25 pm

I'm only a beginner in javascript, but have since using MS had to learn a lot how html & javascript works, trying to build a recorder that records web actions with MS so first thing I want to do is to get the element under mouse hover with this java code:

Code: Select all

elm = document.querySelectorAll(":hover");
box = elm[elm.length-1].getBoundingClientRect();
x = box.x
y = box.y
element_under_mouse =document.elementFromPoint(x, y)
Which seems to work good, still need to figure out how to convert the element back to something usefull and "element_under_mouse.id" at least give an ID if one is there of course, tested this on a button element.
Next thing would be to capture the mouse clicks and when mouse down it will run the javascript above (it is far from finished):

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  Left = 340
  Top = 128
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  BorderStyle = bsDialog
  Caption = 'CustomDialog'
  ClientHeight = 1
  ClientWidth = 200
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -14
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  FormStyle = fsStayOnTop
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = True
  PixelsPerInch = 120
  TextHeight = 16
end
EndDialog>Dialog1
Show>Dialog1
OnEvent>KEY_DOWN,VK27,0,Stop
OnEvent>KEY_DOWN,VK01,0,MouseDown
Let>Monitor=1
Let>LM=0
Let>Txt=Mouse Moving...
Let>Txt2=Left Mouse Down
Label>MousePosCheck
    GetCursorPos>nXPos,nYPos,csType
    Let>Coor=%nXPos%,%nYPos%
    IF>{(%Coor_old%=%Coor%) AND (%LM%="0")}
      SetDialogProperty>Dialog1,,Caption,Coor
    ENDIF
    IF>{(%Coor_old%<>%Coor%) AND (%LM%="0")}
      SetDialogProperty>Dialog1,,Caption,Txt
    ENDIF
    Let>Coor_old=Coor
    Wait>0.1
Goto>MousePosCheck
SRT>Stop
    //Let>Monitor=0
    Exit
END>Stop
SRT>MouseDown
  Let>LM=1
  //reset key down handler
  OnEvent>KEY_DOWN,VK01,,
  //set key up handler
  IF>{(%Monitor%="1") AND (%LM%="1")}
    SetDialogProperty>Dialog1,,Caption,Txt2
    OnEvent>KEY_UP,VK01,,MouseUp
  ENDIF
END>MouseDown
SRT>MouseUp
  Let>LM=0
  //space is up - reset up handler
  OnEvent>KEY_UP,VK01,,
  //set key down handler
  OnEvent>KEY_DOWN,VK01,0,MouseDown
END>MouseUp

Now back to your question, every time you click it has to run the javascript above as well as reading the value back from the div with something like this:

Code: Select all

const btn = document.createElement("button");
btn.innerHTML = "Hello Button";
document.body.appendChild(btn);


ChromeExecute>session_id,alert(document.querySelector("body > button").innerText);
ChromeExecute>session_id,document.querySelector("body > button").innerText="testing";
This creates a button at the bottom which needs to be read back into MS with ChromeFindelement, but it seems like a lot of looping which can make it very slow if not done properly.

User avatar
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Possible for ChromeExecute to pass var back to MS?

Post by Grovkillen » Tue Mar 29, 2022 5:10 pm

Or you could have a separate event that looks for this div. You don't need to do that in the main thread.
Let>ME=%Script%

Running: 15.0.27
version history

ocnuybear
Pro Scripter
Posts: 100
Joined: Sun Jul 15, 2018 5:14 pm

Re: Possible for ChromeExecute to pass var back to MS?

Post by ocnuybear » Tue Mar 29, 2022 6:16 pm

So you mean running concurrent scripts like in this thread? https://www.mjtnet.com/forum/viewtopic ... nt#p25384

User avatar
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Possible for ChromeExecute to pass var back to MS?

Post by Grovkillen » Tue Mar 29, 2022 7:00 pm

Custom event is running its own thread yes.
Let>ME=%Script%

Running: 15.0.27
version history

ocnuybear
Pro Scripter
Posts: 100
Joined: Sun Jul 15, 2018 5:14 pm

Re: Possible for ChromeExecute to pass var back to MS?

Post by ocnuybear » Thu Mar 31, 2022 10:59 am

Thank you Grovkillen for the Onevent>CUSTOM hint, below I have a working example :D

It will navigate to www.google.com and then will create a button on the bottom, you may have to scroll down to see it, after that if you click inside where you would type in your google query it will update the button text to the html element, someone might find this quite usefull, although it is far from perfect.

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  Left = 340
  Top = 128
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  BorderStyle = bsDialog
  Caption = 'CustomDialog'
  ClientHeight = 1
  ClientWidth = 200
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -14
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  FormStyle = fsStayOnTop
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = True
  PixelsPerInch = 120
  TextHeight = 16
end
EndDialog>Dialog1
Show>Dialog1
OnEvent>KEY_DOWN,VK27,0,Stop
OnEvent>KEY_DOWN,VK01,0,MouseDown
OnEvent>CUSTOM,TriggerSub,DoIT,DoSub
SetDialogProperty>Dialog1,,Caption,... LOADING ...

Let>CHROMEDRIVER_EXE=c:\chromedriver.exe
ChromeStart>session_id
ChromeNavigate>session_id,url,https://www.google.com
SetFocus>Google - Google Chrome
WaitWindowFocused>Google - Google Chrome
WindowAction>1,Google - Google Chrome

Let>Create_Box=1
Let>Monitor=1
Let>LM=0

LabelToVar>JS_Create_Box,js
ChromeExecute>session_id,js

Let>js=

Let>Txt=Mouse Moving...
Let>Txt2=Left Mouse Down
Label>MousePosCheck
    GetCursorPos>nXPos,nYPos,csType
    Let>Coor=%nXPos%,%nYPos%
    IF>{(%Coor_old%=%Coor%) AND (%LM%="0")}
      SetDialogProperty>Dialog1,,Caption,Coor
    ENDIF
    IF>{(%Coor_old%<>%Coor%) AND (%LM%="0")}
      SetDialogProperty>Dialog1,,Caption,Txt
    ENDIF
    Let>Coor_old=Coor
    Wait>0.1
Goto>MousePosCheck

SRT>Stop
    //Let>Monitor=0
    Exit
END>Stop

SRT>MouseDown
  Let>LM=1
  //reset key down handler
  OnEvent>KEY_DOWN,VK01,,
  //set key up handler
  IF>{(%Monitor%="1") AND (%LM%="1")}
    LabelToVar>JS_Update_Box,js
    ChromeExecute>session_id,js
    SetDialogProperty>Dialog1,,Caption,Txt2
    OnEvent>KEY_UP,VK01,,MouseUp
  ENDIF
END>MouseDown

SRT>MouseUp
  Let>LM=0
  //space is up - reset up handler
  OnEvent>KEY_UP,VK01,,
  //set key down handler
  OnEvent>KEY_DOWN,VK01,0,MouseDown
END>MouseUp

Label>Start
  wait>0.01
Goto>Start

SRT>TriggerSub
    Let>DoIT=TRUE
END>TriggerSub

SRT>DoSub
  //do something
  Let>LM=2
  SetDialogProperty>Dialog1,,Caption,strResult
  Let>DoIT=FALSE
END>DoSub

/*
JS_Create_Box:
var btn = document.createElement("button");
var htm = "strResult";
btn.innerHTML = htm;
document.body.appendChild(btn);
*/

/*
JS_Update_Box:
elm = document.querySelectorAll(":hover");
box = elm[elm.length-1].getBoundingClientRect();
x = box.x
y = box.y
element_under_mouse=document.elementFromPoint(x, y)
document.querySelector("body > button").innerText = element_under_mouse.outerHTML;
*/

User avatar
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: Possible for ChromeExecute to pass var back to MS?

Post by Grovkillen » Thu Mar 31, 2022 11:52 am

Happy to have helped.
Let>ME=%Script%

Running: 15.0.27
version history

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts