BUG or SYNTAX Error?: EVAL in Macro in Javascript

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
viking
Posts: 214
Joined: Sun Mar 16, 2008 7:22 am

BUG or SYNTAX Error?: EVAL in Macro in Javascript

Post by viking » Mon Dec 31, 2012 1:05 am

The following code works fine in a simple Macro, simpleMacro.iim ( I get a prompt showing "99"):

Code: Select all

SET !VAR1  EVAL("var x=99; x;")
PROMPT {{!VAR1}}
However, I get an Syntax Error when I try to incorporate this in a Macro that is part of a JavaScript, like this:

Code: Select all

var jsLF = "\n";
var MyMacro;

MyMacro = "CODE:";
MyMacro += "SET !VAR1 EVAL("var x=99; x;")"+ jsLF; //Syntax Error here
MyMacro += "PROMPT {{!VAR1}}";

retcode = iimPlay(MyMacro);
if (retcode < 0) {              // an error has occured
	errtext = iimGetLastError();
        alert(errtext);
    }
Is this a Firefox bug, an iMacros limitation or truly a Syntax Error? If Syntax error, anyone knows how it should be changed?


Edit:
NOTE: I do not get an error if I instead execute the external simpleMacro.iim from a javascript:

Code: Select all

retcode = iimPlay("simpleMacro.iim");
if (retcode < 0) {              // an error has occured
	errtext = iimGetLastError();
        alert(errtext);
    }	
viking
Posts: 214
Joined: Sun Mar 16, 2008 7:22 am

Re: BUG or SYNTAX Error?: EVAL in Macro in Javascript

Post by viking » Mon Dec 31, 2012 2:07 am

To answer my own question. I figured out part of the problem by trial-and-error...

Apparently, it is necessary to escape the quotation marks in the EVAL function when making it part of a Macro in a javascript.
Thus, the following works:

Code: Select all

    var jsLF = "\n";
    var MyMacro;

    MyMacro = "CODE:";
    MyMacro += "SET !VAR1 EVAL(\"var x=99; x;\")"+ jsLF; //note the escape backslash before the quotation marks
    MyMacro += "PROMPT {{!VAR1}}";

    retcode = iimPlay(MyMacro);
    if (retcode < 0) {              // an error has occured
       errtext = iimGetLastError();
            alert(errtext);
        }
However, when I try another EVAL function I now get an error "SyntaxError: wrong format of SET command, line: 2"

Code: Select all

var jsLF = "\n";
var MyMacro;

MyMacro = "CODE:";
MyMacro += "SET !EXTRACT \"something\""+ jsLF;
MyMacro += "SET !VAR1 EVAL(\"var x=\"{{!EXTRACT}}\"; x;\")"+ jsLF; // Syntax Error
.
.
.
As previously, it works fine when using the Macro externally instead.
Any ideas..?
viking
Posts: 214
Joined: Sun Mar 16, 2008 7:22 am

Re: BUG or SYNTAX Error?: EVAL in Macro in Javascript

Post by viking » Mon Dec 31, 2012 3:26 am

Well, again, by trial-and-error, I got it work by changing the string quotes to single rather than double quotes (and remove the backslash escape):

Code: Select all

var jsLF = "\n";
var MyMacro;

MyMacro = "CODE:";
MyMacro += "SET !EXTRACT \"something\""+ jsLF;
MyMacro += "SET !VAR1 EVAL(\"var x='{{!EXTRACT}}'; x;\")"+ jsLF; // Replace with single quotes around {{!EXTRACT}}
.
.
Why doesn't double quotes work for strings in this case?
Tom, Tech Support
Posts: 3461
Joined: Mon May 31, 2010 4:59 pm

Re: BUG or SYNTAX Error?: EVAL in Macro in Javascript

Post by Tom, Tech Support » Mon Dec 31, 2012 3:16 pm

Hi viking,

Well, I was waiting for you to once again answer your own question after a little more trial and error, but I see it's been awhile so I'll go ahead and tell you. :wink:

What you have in this situation is a string embedded within a string embedded within a string. The outermost string is the Javascript string that is assigned to the MyMacro variable:

MyMacro += " ... ";

Obviously, these quotes do not need to be escaped because they denote the outermost string.

The first embedded string defines the contents of the EVAL command, and therefore the quotes for the EVAL command need to be escaped as you have already correctly done:

MyMacro += "SET !VAR1 EVAL(\"var x= ... \")"

The second embedded string is contained within the EVAL command string where you assign the value of {{!EXTRACT}} to a Javascript variable. In order to use double-quotes correctly around this string, you need an additional level of nested escape characters:

MyMacro += "SET !VAR1 EVAL(\"var x=\\\"{{!EXTRACT}}\\\"; x;\")"+ jsLF;

Observe in this case that we're escaping the escape character itself, so that the end result that will end up being sent to the macro player is:

SET !VAR1 EVAL("var x=\"{{!EXTRACT}}\"; x;")

Of course using a set of single quotes avoids this extra level redirection and may even look "cleaner," but at least now your curiosity should be satisfied. :D
Regards,

Tom, iMacros Support
viking
Posts: 214
Joined: Sun Mar 16, 2008 7:22 am

Re: BUG or SYNTAX Error?: EVAL in Macro in Javascript

Post by viking » Mon Dec 31, 2012 6:55 pm

Tom, iOpus wrote:Hi viking,

Well, I was waiting for you to once again answer your own question after a little more trial and error, but I see it's been awhile so I'll go ahead and tell you. :wink:

What you have in this situation is a string embedded within a string embedded within a string. The outermost string is the Javascript string that is assigned to the MyMacro variable:

MyMacro += " ... ";

Obviously, these quotes do not need to be escaped because they denote the outermost string.

The first embedded string defines the contents of the EVAL command, and therefore the quotes for the EVAL command need to be escaped as you have already correctly done:

MyMacro += "SET !VAR1 EVAL(\"var x= ... \")"

The second embedded string is contained within the EVAL command string where you assign the value of {{!EXTRACT}} to a Javascript variable. In order to use double-quotes correctly around this string, you need an additional level of nested escape characters:

MyMacro += "SET !VAR1 EVAL(\"var x=\\\"{{!EXTRACT}}\\\"; x;\")"+ jsLF;

Observe in this case that we're escaping the escape character itself, so that the end result that will end up being sent to the macro player is:

SET !VAR1 EVAL("var x=\"{{!EXTRACT}}\"; x;")

Of course using a set of single quotes avoids this extra level redirection and may even look "cleaner," but at least now your curiosity should be satisfied. :D
I see. Thanks!
I actually tried:
MyMacro += "SET !VAR1 EVAL(\"var x=\\"{{!EXTRACT}}\\"; x;\")"+ jsLF;
thinking that I needed to "escape again". I didn't realize that I needed to escape the escape character.... It may help others to write this up somewhere (maybe in connection with the EVAL function?).
Tom, Tech Support
Posts: 3461
Joined: Mon May 31, 2010 4:59 pm

Re: BUG or SYNTAX Error?: EVAL in Macro in Javascript

Post by Tom, Tech Support » Wed Jan 02, 2013 12:43 pm

viking wrote:It may help others to write this up somewhere (maybe in connection with the EVAL function?).
I already linked this post to the Wiki page for EVAL!
Regards,

Tom, iMacros Support
kingmak
Posts: 3
Joined: Mon Jun 10, 2013 4:10 pm

Re: BUG or SYNTAX Error?: EVAL in Macro in Javascript

Post by kingmak » Tue Jun 11, 2013 1:26 pm

Extremely Sorry for bumping this post but i am curious to know that how would I write the manipulated text to a file?
Eric-xbd
Posts: 10
Joined: Sun May 11, 2014 2:10 am

Re: BUG or SYNTAX Error?: EVAL in Macro in Javascript

Post by Eric-xbd » Tue Jul 29, 2014 4:26 pm

Hello please could you help me?
I want extract string caracters without cut words.
I have this error in firefox: wrong format of SET command, line 31 (Error code: -910)
you can see directly to line 31:

SEARCH SOURCE=REGEXP:"description"\scontent="(.*)">" EXTRACT="$1"
SET !VAR8 EVAL("var extr2=\"{{!EXTRACT}}\"; extr2.replace(/\\s-\\sInformatique(.*)$/, \"\");")
SET !VAR7 EVAL("\"{{!VAR8}}\".substr(0,130);")

This is line 31:
SET !VAR6 EVAL("var extr3=\"{{!VAR7}}\"; var random=extr3.substr(0, Math.min(extr3.length, extr3.lastIndexOf(","))); random;")

Please help me and come drink a beer in Brest France
chivracq
Posts: 8217
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: BUG or SYNTAX Error?: EVAL in Macro in Javascript

Post by chivracq » Tue Jul 29, 2014 10:48 pm

Eric-xbd wrote:Hello please could you help me?
I want extract string caracters without cut words.
I have this error in firefox: wrong format of SET command, line 31 (Error code: -910)
you can see directly to line 31:

SEARCH SOURCE=REGEXP:"description"\scontent="(.*)">" EXTRACT="$1"
SET !VAR8 EVAL("var extr2=\"{{!EXTRACT}}\"; extr2.replace(/\\s-\\sInformatique(.*)$/, \"\");")
SET !VAR7 EVAL("\"{{!VAR8}}\".substr(0,130);")

This is line 31:
SET !VAR6 EVAL("var extr3=\"{{!VAR7}}\"; var random=extr3.substr(0, Math.min(extr3.length, extr3.lastIndexOf(","))); random;")

Please help me and come drink a beer in Brest France
CIM...! :mrgreen:

My 2ct as I'm definitely not a JavaScript Guru..., but I've encountered the Problem as well when trying to execute 2 "embedded" or consecutive JavaScript Functions and I've solved it by splitting the original EVAL Statement into 2 EVAL Statements with 1 Function per EVAL Statement (=> one for the 'min()' first and then one for the 'substr()').
I've seen Threads otherwise where 'exec()' is used...

And maybe you even need a 3rd Level for the 'lastIndexOf()'...
But your Double Quotes in 'lastIndexOf(",")' should be escaped as well, I think... (or replaced by Single Quotes).
Last edited by chivracq on Tue Jul 29, 2014 11:06 pm, edited 1 time in total.
- (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...
chivracq
Posts: 8217
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: BUG or SYNTAX Error?: EVAL in Macro in Javascript

Post by chivracq » Tue Jul 29, 2014 10:58 pm

kingmak wrote:Extremely Sorry for bumping this post but i am curious to know that how would I write the manipulated text to a file?
Late Reply...!

CIM first...!

Simply by using SAVEAS TYPE=EXTRACT and putting before the Computed String into !EXTRACT...
- (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...
Eric-xbd
Posts: 10
Joined: Sun May 11, 2014 2:10 am

Re: BUG or SYNTAX Error?: EVAL in Macro in Javascript

Post by Eric-xbd » Wed Jul 30, 2014 10:21 pm

Ok I don't want to be a (f) CIM posting man!!!!

Thanks for your answer and sorry for my english like baby

-windows seven SP1
-firefox 31
-imacro 8.8.2

SEARCH SOURCE=REGEXP:"description"\scontent="(.*)">" EXTRACT="$1" >>> sniffing in source code :)
SET !VAR8 EVAL("var extr2=\"{{!EXTRACT}}\"; extr2.replace(/\\s-\\sInformatique(.*)$/, \"\");") >>> delete some text
SET !VAR7 EVAL("\"{{!VAR8}}\".substr(0,130);") >>> keep only 130 first caracters

I've tried splitting the original EVAL Statement into 2 EVAL But i've the same error for !VAR5 or !VAR6 line (before or after splitting)

SET !VAR6 EVAL("var extr3=\"{{!VAR7}}\"; var random=Math.min(extr3.length, extr3.lastIndexOf(" ")) random;") >>> replace part of following !VAR6 line
SET !VAR5 EVAL("var extr4=\"{{!VAR6}}\"; "var extr3=\"{{!VAR7}}\"; var random=extr3.substr(0,extr4); random;") >>> replace part of following !VAR6 line

SET !VAR6 EVAL("var extr3=\"{{!VAR7}}\"; var random=extr3.substr(0, Math.min(extr3.length, extr3.lastIndexOf(" "))); random;")


note: !VAR7= the 130 first caracters >>
maybe this is the problem for making !VAR6? maybe !VAR6 must be a number?

Thanks for your help
from Brest in France
chivracq
Posts: 8217
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: BUG or SYNTAX Error?: EVAL in Macro in Javascript

Post by chivracq » Thu Jul 31, 2014 9:26 am

Eric-xbd wrote:Ok I don't want to be a (f) CIM posting man!!!!

Thanks for your answer and sorry for my english like baby

-windows seven SP1
-firefox 31
-imacro 8.8.2

SEARCH SOURCE=REGEXP:"description"\scontent="(.*)">" EXTRACT="$1" >>> sniffing in source code :)
SET !VAR8 EVAL("var extr2=\"{{!EXTRACT}}\"; extr2.replace(/\\s-\\sInformatique(.*)$/, \"\");") >>> delete some text
SET !VAR7 EVAL("\"{{!VAR8}}\".substr(0,130);") >>> keep only 130 first caracters

I've tried splitting the original EVAL Statement into 2 EVAL But i've the same error for !VAR5 or !VAR6 line (before or after splitting)

SET !VAR6 EVAL("var extr3=\"{{!VAR7}}\"; var random=Math.min(extr3.length, extr3.lastIndexOf(" ")) random;") >>> replace part of following !VAR6 line
SET !VAR5 EVAL("var extr4=\"{{!VAR6}}\"; "var extr3=\"{{!VAR7}}\"; var random=extr3.substr(0,extr4); random;") >>> replace part of following !VAR6 line

SET !VAR6 EVAL("var extr3=\"{{!VAR7}}\"; var random=extr3.substr(0, Math.min(extr3.length, extr3.lastIndexOf(" "))); random;")


note: !VAR7= the 130 first caracters >>
maybe this is the problem for making !VAR6? maybe !VAR6 must be a number?

Thanks for your help
from Brest in France
OK, I would need some time to dig into it, but again, you don't escape the Double Quotes in 'indexOf()', that's the first thing to try...
And use PROMPT-PROMPT-PROMPT to debug your Variables...
- (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...
Eric-xbd
Posts: 10
Joined: Sun May 11, 2014 2:10 am

Re: BUG or SYNTAX Error?: EVAL in Macro in Javascript

Post by Eric-xbd » Thu Jul 31, 2014 9:36 am

I will try your suggest this evening Thanks
chivracq
Posts: 8217
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: BUG or SYNTAX Error?: EVAL in Macro in Javascript

Post by chivracq » Thu Jul 31, 2014 10:47 am

Have a look at this Thread for a working Example that I once made of 'EVAL()' with 'indexOf()'...:
- Re: Extract separately parts of text from a webpage
- (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...
Eric-xbd
Posts: 10
Joined: Sun May 11, 2014 2:10 am

Re: BUG or SYNTAX Error?: EVAL in Macro in Javascript

Post by Eric-xbd » Thu Jul 31, 2014 7:55 pm

Work perfect

i used your method and answer

using prompt to understand what matter
using \"
using imacro variables instead of javascript variables
using more EVAL line:


SET !VAR8 EVAL("var extr2=\"{{!EXTRACT}}\"; extr2.replace(/\\s-\\sInformatique(.*)$/, \"\");")
SET !VAR7 EVAL("\"{{!VAR8}}\".substr(0,130);")
SET !VAR6 EVAL("var extr3=\"{{!VAR7}}\"; Math.min(extr3.length, extr3.lastIndexOf(\" \"));")
SET !VAR5 EVAL("\"{{!VAR7}}\".substr(0,"{{!VAR6}}\");")

MANY THANKS chivracq
Last edited by Eric-xbd on Sat Aug 02, 2014 9:48 am, edited 1 time in total.
Post Reply