Set variable value to javascript result?

Support for iMacros. The iMacros software is the unique solution for automating every activity inside a web browser, for data extraction and web testing.
Forum rules
iMacros EOL - Attention!

The renewal maintenance has officially ended for Progress iMacros effective November 20, 2023 and all versions of iMacros are now considered EOL (End-of-Life). The iMacros products will no longer be supported by Progress (aside from customer license issues), and these forums will also no longer be moderated from the Progress side.

Thank you again for your business and support.

Sincerely,
The Progress Team

Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information: CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
Falsely
Posts: 2
Joined: Fri Oct 25, 2019 12:21 am

Set variable value to javascript result?

Post by Falsely » Fri Oct 25, 2019 12:27 am

iMacros Build 1005
WIndows 10
Google Chrome Version 77.0.3865.120

Pretty self explanatory, I have a script (below) that generates a random string, I want the result of that script to be typed onto a page.

Code: Select all

for(var text="",possible="6SJXEoQ0Htx4WhvKgu2b3fIrle8wBZs1MRpT9PGyDCLVUmYO5AdiankFc7qjzN",i=0;i<15;i++)text+=possible.charAt(Math.floor(Math.random()*possible.length));
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Set variable value to javascript result?

Post by chivracq » Fri Oct 25, 2019 12:43 am

Falsely wrote:
Fri Oct 25, 2019 12:27 am

Code: Select all

iMacros Build 1005
WIndows 10
Google Chrome Version 77.0.3865.120
Pretty self explanatory, I have a script (below) that generates a random string, I want the result of that script to be typed onto a page.

Code: Select all

for(var text="",possible="6SJXEoQ0Htx4WhvKgu2b3fIrle8wBZs1MRpT9PGyDCLVUmYO5AdiankFc7qjzN",i=0;i<15;i++)text+=possible.charAt(Math.floor(Math.random()*possible.length));

Qt has nothing specific to iMacros for CR, "correct" Sub-Forum would have been the 'General' one... :idea:

'Free'/'PE' is missing from your FCI...?

But, yep, you can use 'EVAL()' with your current JS Script to "collect" the Result in a Var. :idea:
(You'll need to escape the Double Quotes or to replace them with Single Quotes... And use 'PROMPT' to debug your Script to "follow" that Var if "it doesn't work directly"...)
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
Falsely
Posts: 2
Joined: Fri Oct 25, 2019 12:21 am

Re: Set variable value to javascript result?

Post by Falsely » Fri Oct 25, 2019 12:50 am

chivracq wrote:
Fri Oct 25, 2019 12:43 am
Qt has nothing specific to iMacros for CR, "correct" Sub-Forum would have been the 'General' one... :idea:

'Free'/'PE' is missing from your FCI...?

But, yep, you can use 'EVAL()' with your current JS Script to "collect" the Result in a Var. :idea:
(You'll need to escape the Double Quotes or to replace them with Single Quotes... And use 'PROMPT' to debug your Script to "follow" that Var if "it doesn't work directly"...)
Any way to move it to general?

Sorry about the missing, Free/PE, I'm using the free version.

Also, how exactly would I do that? Been looking into the docs and forums and haven't been able to figure it out.
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Set variable value to javascript result?

Post by chivracq » Fri Oct 25, 2019 1:35 am

Falsely wrote:
Fri Oct 25, 2019 12:50 am
Any way to move it to general?

Sorry about the missing, Free/PE, I'm using the free version.

Also, how exactly would I do that? Been looking into the docs and forums and haven't been able to figure it out.

Yeah, don't "worry" about the correct Sub-Forum, I'll ask the Forum Admin to move the Thread... (I can't do it myself...) It's "more" for a "next time"... :wink:

OK for your FCI...

"How exactly...?" => you simply C&P your current Statement into 'EVAL()', not very complicated I would think, ah-ah...!: 8)

Code: Select all

VERSION BUILD=8820413 RECORDER=FX
TAB T=1

'for(var text="",possible="6SJXEoQ0Htx4WhvKgu2b3fIrle8wBZs1MRpT9PGyDCLVUmYO5AdiankFc7qjzN",i=0;i<15;i++)text+=possible.charAt(Math.floor(Math.random()*possible.length));")

SET !VAR1 EVAL("for(var text='',possible='6SJXEoQ0Htx4WhvKgu2b3fIrle8wBZs1MRpT9PGyDCLVUmYO5AdiankFc7qjzN',i=0;i<15;i++)text+=possible.charAt(Math.floor(Math.random()*possible.length));")
PROMPT VAR1:<SP>_{{!VAR1}}_
This will return in the 'PROMPT':
VAR1: _9XpDZl7U9L47lKc_
(Tested on iMacros for FF v8.8.2, Pale Moon v26.3.3 (=FF47), Win10_x64.)

>

And if you want to "shorten" the Expression in the 'EVAL()', to make the Script a bit more readable, you can also take your 'possible' Input String outside the 'EVAL()'...:

Code: Select all

SET !VAR1 "6SJXEoQ0Htx4WhvKgu2b3fIrle8wBZs1MRpT9PGyDCLVUmYO5AdiankFc7qjzN"
SET !VAR2 EVAL("var str='{{!VAR1}}', z=''; for(i=0;i<15;i++)z+=str.charAt(Math.floor(Math.random()*str.length));")
PROMPT VAR1:<BR>_{{!VAR1}}_<BR><BR>VAR2:<BR>_{{!VAR2}}_
=> Will produce the following Output in the 'PROMPT':
VAR1:
_6SJXEoQ0Htx4WhvKgu2b3fIrle8wBZs1MRpT9PGyDCLVUmYO5AdiankFc7qjzN_

VAR2:
_WLSzU4eNkcXTpHC_
(Tested in the same FCI...)

(The '<BR>' in the 'PROMPT' is Buggy in v10.0.5 for CR I think, it gets interpreted as a '<SP>' instead of getting a Line-Break, I've reported it many times, never acknowledged by @TechSup, and not one single User ever "complained"... :| )
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
Tom, Tech Support
Posts: 3834
Joined: Mon May 31, 2010 4:59 pm

Re: Set variable value to javascript result?

Post by Tom, Tech Support » Mon Oct 28, 2019 12:23 pm

chivracq wrote:
Fri Oct 25, 2019 1:35 am
(The '<BR>' in the 'PROMPT' is Buggy in v10.0.5 for CR I think, it gets interpreted as a '<SP>' instead of getting a Line-Break, I've reported it many times, never acknowledged by @TechSup, and not one single User ever "complained"... :| )
Duly noted, will add this to the list of items to investigate.
Regards,

Tom, iMacros Support
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Set variable value to javascript result?

Post by chivracq » Mon Oct 28, 2019 5:16 pm

Tom, Tech Support wrote:
Mon Oct 28, 2019 12:23 pm
chivracq wrote:
Fri Oct 25, 2019 1:35 am
(The '<BR>' in the 'PROMPT' is Buggy in v10.0.5 for CR I think, it gets interpreted as a '<SP>' instead of getting a Line-Break, I've reported it many times, never acknowledged by @TechSup, and not one single User ever "complained"... :| )
Duly noted, will add this to the list of items to investigate.

Oh...!, wonderful...!!! :P

I think v10.0.2 for FF was impacted as well, I had noticed once...
I'm not sure if I had noticed/reported it during the Beta-Testing Phase for FF already or "later", as the Script I had "converted" to v10.0.2 and that I used for that Beta-Testing didn't contain any "active" 'PROMPT' I think, and I usually use 'PROMPT' for some Testing for the Forum or for myself when I create new Scripts for myself...

And none of the '\n' | '\r\ | '\r\n\' in any order, with Double/Single Quotes, extra Escape Char(s) would help in any way...
(Hum, I'm not too sure about that anymore, that was several months ago, and I later did some similar Testing with 'MacroError()', so I might be confusing a bit the Results for both... Although '<SP>' + '<BR>' do work in 'MacroError()'..., so I'm not sure anymore which one is which one, ah-ah...! :oops: :oops: I would need to test again... :wink: )

Hum, if you and/or @Dev will be "working" on the 'PROMPT' Command, you may want to have a look at my '-05-a-' + '-05-b-' EnhReqs that I have about "improving"/extending the 'PROMPT' Command/Functionality...! :idea:

>>>

Thanks for moving the Thread to the 'General' Sub-Forum... :D

>>>

Hum, and no Follow-up from @OP anymore... :o
"Funny" to notice time after time, that nearly each time I write the Script/Solution for most 1st-time Users, they never bother to follow up for a mini-Thanks once they have their Solution, grrr...! :roll:

OK, never mind, the 'EVAL()' is interesting for the 'for' Loop with a String as Input. I already knew a Sol with an Array as Input, but I find this one with a String neater and compacter and more usable with iMacros (as Arrays are treated like Strings anyway when first declared outside 'EVAL()'). :D

And @TechSup "finally" acknowledged the Bug about the 'PROMPT' Command, + maybe my EnhReq's, youpidoo...! 8)
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
Tom, Tech Support
Posts: 3834
Joined: Mon May 31, 2010 4:59 pm

Re: Set variable value to javascript result?

Post by Tom, Tech Support » Mon Sep 07, 2020 1:46 pm

Tom, Tech Support wrote:
Mon Oct 28, 2019 12:23 pm
chivracq wrote:
Fri Oct 25, 2019 1:35 am
(The '<BR>' in the 'PROMPT' is Buggy in v10.0.5 for CR I think, it gets interpreted as a '<SP>' instead of getting a Line-Break, I've reported it many times, never acknowledged by @TechSup, and not one single User ever "complained"... :| )
Duly noted, will add this to the list of items to investigate.
This issue is fixed with the release of iMacros for Chrome 10.1.
Regards,

Tom, iMacros Support
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Set variable value to javascript result?

Post by chivracq » Tue Sep 08, 2020 3:54 pm

Tom, Tech Support wrote:
Mon Sep 07, 2020 1:46 pm
Tom, Tech Support wrote:
Mon Oct 28, 2019 12:23 pm
chivracq wrote:
Fri Oct 25, 2019 1:35 am
(The '<BR>' in the 'PROMPT' is Buggy in v10.0.5 for CR I think, it gets interpreted as a '<SP>' instead of getting a Line-Break, I've reported it many times, never acknowledged by @TechSup, and not one single User ever "complained"... :| )
Duly noted, will add this to the list of items to investigate.
This issue is fixed with the release of iMacros for Chrome 10.1.

Alright, good-good, works now fine indeed in this new v10.1 for CR Version. :D

And I'm "nearly" impressed, the '<BR>' also works in 'MacroError()', ah-ah...! Very good...!! :D

For ''MacroError()' and all Runtime Errors, the Error Code though is never mentioned... (I had already reported it 2 or 3 years ago...)
And the 'Edit macro' Button never jumps to the "faulty" Line in the Macro Editor... (Also already reported...)

Mini-Script I used to test...:

Code: Select all

VERSION BUILD=8820413 RECORDER=FX
SET !EXTRACT_TEST_POPUP NO
TAB T=1

URL GOTO=https://forum.imacros.net/viewtopic.php?p=86103#p86103

'TAG POS=1 TYPE=H3 ATTR=TXT:Set<SP>variable<SP>value<SP>to<SP>javascript<SP>result?
TAG POS=1 TYPE=H3 ATTR=TXT:* EXTRACT=TXT

SET !VAR1 LOOP:<SP>_{{!LOOP}}_<BR><BR>EXTRACT:<SP>_{{!EXTRACT}}_
PROMPT {{!VAR1}}

SET !ERRORIGNORE NO
ADD !VAR1 <BR><BR>
SET Save_Top10 EVAL("var n='{{!LOOP}}'; if(n==1){MacroError('{{!VAR1}}');}")
SET !ERRORIGNORE YES
(Tested in iMacros for CR v10.1 'Free', CR76, Win10_Pro_x64.)
(Yep..., I'm still at CR76, that's not a Typo...)


>>>

I've had an EnhReq btw about the 'PROMPT' Command for 5 years now :shock: , to "enhance" that Command to make it much more useful and more powerful/functional, see Items '05-a' + '05-b' about 'PROMPT' + 'ONPROMPT' in my Thread about EnhReq's... :idea:
It's a bit of "a pity" that only mini-Bugs get solved in some new Release and that rarely new Functionality gets added... :(
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
Post Reply