EVAL JS Function For Imacros

Discussions and Tech Support related to the iMacros Firefox Add-on, including the built-in Javascript scripting interface for playing .js files.
Forum rules
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
mccarthy995
Posts: 8
Joined: Fri Aug 03, 2018 4:27 pm

EVAL JS Function For Imacros

Post by mccarthy995 » Thu Aug 30, 2018 7:02 pm

What version of iMacros are you using?
VERSION BUILD=9030808

What operating system are you using?
ENG UK WIN 7

Which browser are you using?
FF 52.0.1 (32-bit)

--------------------------------------------------------

Hi there, I am semi new to Imacros and JavaScript, I have spent the last 2 hours trying to work this out with no avail. I understand my issue I just cant work it out.

\\This code extracts the text I want, cuts the first 9 letters off, then takes the first four.
eg. HelloWorld12345 to> d12345 to> d123

TAG POS=1 TYPE=SPAN ATTR=TXT:Account:* EXTRACT=TXT
SET !VAR6 {{!EXTRACT}}
SET !VAR6 EVAL("var s=\"{{!VAR6}}\"; var index=s.indexOf(\"=\");var index2=s.indexOf(\"&\");var r=s.slice(index+9,index-18);r;")
SET !VAR6 EVAL("var x=\"{{!VAR6}}\"; x=x.match(/^.{4}/).join(''); x;")
SET !EXTRACT NULL

I have converted it to a .js Document and this is how It looks now.
macro += "TAG POS=1 TYPE=SPAN ATTR=TXT:Account:* EXTRACT=TXT" + "\n";
macro += "SET !VAR6 {{!EXTRACT}} " + "\n";
macro += "SET !VAR6 EVAL("var s=\"{{!VAR6}}\"; var index=s.indexOf(\"=\");var index2=s.indexOf(\"&\");var r=s.slice(index+9,index-18);r;")" + "\n";
macro += "SET !VAR6 EVAL("var x=\"{{!VAR6}}\"; x=x.match(/^.{4}/).join(''); x;")" + "\n";
macro += "SET !EXTRACT NULL" + "\n";

I know that the EVAL lines aren't working because its identifying it as a string and not a command, however due to my lack of knowledge with JavaScript, I cannot work out what should be a " and what should be a '. Can you help me please? :shock:
chivracq
Posts: 8217
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: EVAL JS Function For Imacros

Post by chivracq » Thu Aug 30, 2018 11:04 pm

mccarthy995 wrote:

Code: Select all

What version of iMacros are you using?
VERSION BUILD=9030808

What operating system are you using?
ENG UK WIN 7

Which browser are you using?
FF 52.0.1 (32-bit)
--------------------------------------------------------

Hi there, I am semi new to Imacros and JavaScript, I have spent the last 2 hours trying to work this out with no avail. I understand my issue I just cant work it out.

\\This code extracts the text I want, cuts the first 9 letters off, then takes the first four.
eg. HelloWorld12345 to> d12345 to> d123

Code: Select all

TAG POS=1 TYPE=SPAN ATTR=TXT:Account:* EXTRACT=TXT
SET !VAR6 {{!EXTRACT}} 
SET !VAR6 EVAL("var s=\"{{!VAR6}}\"; var index=s.indexOf(\"=\");var index2=s.indexOf(\"&\");var r=s.slice(index+9,index-18);r;")
SET !VAR6 EVAL("var x=\"{{!VAR6}}\"; x=x.match(/^.{4}/).join(''); x;")
SET !EXTRACT NULL
I have converted it to a .js Document and this is how It looks now.

Code: Select all

macro += "TAG POS=1 TYPE=SPAN ATTR=TXT:Account:* EXTRACT=TXT" + "\n";
macro += "SET !VAR6 {{!EXTRACT}} " + "\n";
macro += "SET !VAR6 EVAL("var s=\"{{!VAR6}}\"; var index=s.indexOf(\"=\");var index2=s.indexOf(\"&\");var r=s.slice(index+9,index-18);r;")" + "\n";
macro += "SET !VAR6 EVAL("var x=\"{{!VAR6}}\"; x=x.match(/^.{4}/).join(''); x;")" + "\n";
macro += "SET !EXTRACT NULL" + "\n";
I know that the EVAL lines aren't working because its identifying it as a string and not a command, however due to my lack of knowledge with JavaScript, I cannot work out what should be a " and what should be a '. Can you help me please? :shock:
Hum..., your 2 'EVAL()' Statements in pure '.iim' already look a bit cumbersome to me, even if you don't use your 'index2', I would probably find you a much simpler Solution if you posted a "real" Example with the Content of the 'EXTRACT' and mentioning exactly what you want to keep as a Result in the end...

Then..., pfff..., even more cumbersome to use 'EVAL()' in some on-the-fly Macro in a '.js' Script...! The "Purpose" of 'EVAL()' is to be able to use JS in an '.iim' Macro, but you are already in a JS Environment in a '.js' Script...! => Better Practice is to do your JS Manipulation directly in the '.js' Script instead of embedding JS inside an '.iim' embedded in JS... :shock:

But anyway, the Consequence from that embedded+embedded JS is that you need for each embedded Level to escape (again and again) all Special Chars, oops...! => You need to double or triple all Escape Chars (\) inside the 'EVAL()' now..., or to try to play with alternating Single and Double Quotes...

And hum..., from the Script you've posted, I don't really see the Need for a '.js' Script, all can be much simpler done in pure '.iim'..., as if you ever want to update your FF52 to FF57(+) (current FF Version is now FF62), you'll need to update iMacros as well to v10.0.2(+) where '.js' Scripts are not supported anymore, and you'll have to reconvert your Scripts back to pure '.iim'... :?
- (F)CIM = (Full) Config Info Missing: iMacros + Browser + OS with all 3 Versions...
- I usually don't even read the Question if that (required) Info is not mentioned...
- Script & URL usually help a lot for a more "educated" Help...
mccarthy995
Posts: 8
Joined: Fri Aug 03, 2018 4:27 pm

Re: EVAL JS Function For Imacros

Post by mccarthy995 » Fri Aug 31, 2018 11:48 am

Hi Chivracq,

A few things to clear up, I am using a work computer and for some reason they don't allow the newer version of FF on the pc's we use :x , and with that in mind, I cannot show you the full script without breaking some data protection rule, But I will try my best to help provide the info you need.

'Full Name
TAG POS=1 TYPE=DT ATTR=TXT:Name:
TAG POS=R1 TYPE=SPAN ATTR=TXT:* EXTRACT=TXT
SET !VAR1 {{!EXTRACT}}
SET !EXTRACT NULL

'ID
TAG POS=1 TYPE=DT ATTR=TXT:Service<sp>ID:
TAG POS=R1 TYPE=SPAN ATTR=TXT:* EXTRACT=TXT
SET !VAR2 {{!EXTRACT}}
SET !EXTRACT NULL

'Postal Code
TAG POS=1 TYPE=DT ATTR=TXT:Address:
TAG POS=R5 TYPE=SPAN ATTR=TXT:* EXTRACT=TXT
SET !VAR4 {{!EXTRACT}}
SET !EXTRACT NULL

'Phone number
TAG POS=1 TYPE=DT ATTR=TXT:CLI*
TAG POS=R1 TYPE=SPAN ATTR=TXT:* EXTRACT=TXT
SET !VAR5 {{!EXTRACT}}
SET !EXTRACT NULL

'Account Name
TAG POS=1 TYPE=SPAN ATTR=TXT:Account:* EXTRACT=TXT
SET !VAR6 {{!EXTRACT}}
SET !VAR6 EVAL("var s=\"{{!VAR6}}\"; var index=s.indexOf(\"=\");var index2=s.indexOf(\"&\");var r=s.slice(index+9,index-18);r;")
SET !VAR6 EVAL("var x=\"{{!VAR6}}\"; x=x.match(/^.{4}/).join(''); x;")
SET !EXTRACT NULL

'The above text extracts the data first and then assigns it to !var#. I use this information to place orders with our Clients automatically.

Below is what the EVAL function slices up. I need the first 4 letters just after Account:

Account: thisaccount.net | queued-activate | WLR
Account: thataccount.net | queued-activate | WLR

Hope this helps, I want this a .js script so I can use IF fuctions later on, but if you know of a better way I would love your input.

Thanks!
:D
chivracq
Posts: 8217
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: EVAL JS Function For Imacros

Post by chivracq » Fri Aug 31, 2018 4:36 pm

mccarthy995 wrote:Hi Chivracq,

A few things to clear up, I am using a work computer and for some reason they don't allow the newer version of FF on the pc's we use :x , and with that in mind, I cannot show you the full script without breaking some data protection rule, But I will try my best to help provide the info you need.

Code: Select all

'Full Name
TAG POS=1 TYPE=DT ATTR=TXT:Name: 
TAG POS=R1 TYPE=SPAN ATTR=TXT:* EXTRACT=TXT
SET !VAR1 {{!EXTRACT}} 
SET !EXTRACT NULL

'ID
TAG POS=1 TYPE=DT ATTR=TXT:Service<sp>ID:
TAG POS=R1 TYPE=SPAN ATTR=TXT:* EXTRACT=TXT
SET !VAR2 {{!EXTRACT}} 
SET !EXTRACT NULL

'Postal Code
TAG POS=1 TYPE=DT ATTR=TXT:Address:
TAG POS=R5 TYPE=SPAN ATTR=TXT:* EXTRACT=TXT
SET !VAR4 {{!EXTRACT}} 
SET !EXTRACT NULL

'Phone number
TAG POS=1 TYPE=DT ATTR=TXT:CLI*
TAG POS=R1 TYPE=SPAN ATTR=TXT:* EXTRACT=TXT
SET !VAR5 {{!EXTRACT}} 
SET !EXTRACT NULL

'Account Name
TAG POS=1 TYPE=SPAN ATTR=TXT:Account:* EXTRACT=TXT
SET !VAR6 {{!EXTRACT}} 
SET !VAR6 EVAL("var s=\"{{!VAR6}}\"; var index=s.indexOf(\"=\");var index2=s.indexOf(\"&\");var r=s.slice(index+9,index-18);r;")
SET !VAR6 EVAL("var x=\"{{!VAR6}}\"; x=x.match(/^.{4}/).join(''); x;")
SET !EXTRACT NULL
'The above text extracts the data first and then assigns it to !var#. I use this information to place orders with our Clients automatically.

Below is what the EVAL function slices up. I need the first 4 letters just after Account:

Account: thisaccount.net | queued-activate | WLR
Account: thataccount.net | queued-activate | WLR

Hope this helps, I want this a .js script so I can use IF fuctions later on, but if you know of a better way I would love your input.

Thanks!
:D
You can use the ]CODE[ Meta-Tags to format your Posts to "ease" a bit the Readability of the Thread... :idea:

Well, don't be "sad" about your IT-Dept keeping you at FF52, this is actually a "lucky" Situation for you that allows you to still be able to use iMacros for FF v9.0.3 or v8.9.7 (that both work until FF56(-)); in Business Environments with an IT-Dept, it's usually the "other way round", with the IT-Dept forcing Users to update their Browser(s) to some "later/latest" Version(s) for Security Reason...

But I guess one day, they will update your FF to some later Version after FF57+, and you'll be forced to use iMacros for FF v10.0.2+, which is very limited in Functionality compared to v9.0.3 or v8.9.7 with for example like I already mentioned, '.js' Scripts not supported anymore in v10.0.2.

Hum, OK for your "Account: this/thataccount.net | queued-activate | WLR" but that's what you get after already your first cumbersome 'EVAL()', not what you get in the '!EXTRACT', I reckon your 2 'EVAL()' Statements can both be combined in just one single simpler 'EVAL()'...

But as an Example, if you were getting "Account: thataccount.net | queued-activate | WLR" in the 'EXTRACT', then I would use:

Code: Select all

SET Acc_Name EVAL("var s='{{!EXTRACT}}'; var x,y,z; x=s.split(' '); z=x[1].substr(0,4); z;")
... or actually even simpler:

Code: Select all

SET Acc_Name EVAL("var s='{{!EXTRACT}}'; var z=s.substr(9,4); z;")
And any of those 2 'EVAL()' Statements can easily be converted to '.js' by using Single Quotes for your "macro" String in the '.js' to alternate Single and Double Quotes to avoid having to single/double/triple escape Special Chars:

Code: Select all

macro += 'SET Acc_Name EVAL("var s='{{!EXTRACT}}'; var x,y,z; x=s.split(' '); z=x[1].substr(0,4); z;")' + "\n";

Code: Select all

macro += 'SET Acc_Name EVAL("var s='{{!EXTRACT}}'; var z=s.substr(9,4); z;")' + "\n";
"... so I can use IF fu[n]ctions later on..."
=> Yeah well, it all depends a bit on your IF-IF-IF, but everything can be done in pure '.iim', I don't use any '.js' Scripts myself, I do all my Coding in pure '.iim'... (And I do more complex "Things" than anything I've ever seen (in '.js) on the Forum, ah-ah...! :twisted: )
I've explained a few of the Techniques I use in the following Thread:
- Workarounds for Conditional Logic in Macro Code

OK, good luck anyway... :wink:
- (F)CIM = (Full) Config Info Missing: iMacros + Browser + OS with all 3 Versions...
- I usually don't even read the Question if that (required) Info is not mentioned...
- Script & URL usually help a lot for a more "educated" Help...
mccarthy995
Posts: 8
Joined: Fri Aug 03, 2018 4:27 pm

Re: EVAL JS Function For Imacros

Post by mccarthy995 » Wed Oct 03, 2018 1:15 pm

Hi Chivracq,

Sorry for the late reply, I couldn't use your formula without thanking you,

My Script doesn't look like a mess anymore with the Complicated Eval Statements everywhere.

Much appreciated!
Tom
chivracq
Posts: 8217
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: EVAL JS Function For Imacros

Post by chivracq » Wed Oct 03, 2018 8:03 pm

mccarthy995 wrote:Hi Chivracq,

Sorry for the late reply, I couldn't use your formula without thanking you,

My Script doesn't look like a mess anymore with the Complicated Eval Statements everywhere.

Much appreciated!
Tom
Ah...!, never too late indeed...! :wink:
But OK..., good to hear and glad I could help... :D
- (F)CIM = (Full) Config Info Missing: iMacros + Browser + OS with all 3 Versions...
- I usually don't even read the Question if that (required) Info is not mentioned...
- Script & URL usually help a lot for a more "educated" Help...
Post Reply