Convert imacro code to javascript

Discussions and Tech Support specific to the iMacros Firefox Add-on.
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
DDon
Posts: 20
Joined: Sat Aug 06, 2016 1:41 pm

Convert imacro code to javascript

Post by DDon » Fri Sep 29, 2017 5:51 am

Firefox ESR 52.4 64 bit / Windows 10 64 bit / Imacro 8.9.7

I can't make the macro code run in javascript (.js file as there are many quotations in the code). I appreciate if there is a solution. Many Thanks

Code: Select all

TAG XPATH="/*/Summary[1]/Payer}
	
SET a1  EVAL("'{{!EXTRACT}}'.replace(/(EXTRACT|#EANF#)/g, '');")
chivracq
Posts: 8997
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Convert imacro code to javascript

Post by chivracq » Fri Sep 29, 2017 2:18 pm

DDon wrote:Firefox ESR 52.4 64 bit / Windows 10 64 bit / Imacro 8.9.7

I can't make the macro code run in javascript (.js file as there are many quotations in the code). I appreciate if there is a solution. Many Thanks

Code: Select all

TAG XPATH="/*/Summary[1]/Payer}
	
SET a1  EVAL("'{{!EXTRACT}}'.replace(/(EXTRACT|#EANF#)/g, '');")
Yep, you need to escape them...
What sometimes helps is to alternate Single Quotes and Double Quotes (like you already did in your 'EVAL()' Statement), and therefore to use Single Quotes in your '.js' Script for your on-the-fly Macro-String Construction.

But mini-Remark, the (main) "Purpose" of 'EVAL()' is to be able to use JavaScript in an '.iim' Script... => Using 'EVAL()' in an embedded on-the-fly Macro in a '.js' Script always sounds a bit cumbersome to me as you are already in a JS Environment...! You can split your on-the-fly Macro and handle the 'EVAL()' part directly in the '.js' Script.
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
DDon
Posts: 20
Joined: Sat Aug 06, 2016 1:41 pm

Re: Convert imacro code to javascript

Post by DDon » Fri Sep 29, 2017 3:11 pm

Hi Chivracq,

I get your point. I'm not really a programmer and I learn iMacro and java bit by bit when I'm trying to code my own iMacro. The codes have been working well in iim format, but they are getting more than a thousand line as I keep copy and paste without the code without using the loop function. Due to my work nature (speed priority), my imacro need to click and run rather than open the imacro and put the number of loop then click play. This push me to the limit upgrade/convert my current iim code to .js.

If you know the solution to conver these two line codes in to javascript for imacro, I think that will be the key that help me finish the rest of the looping code. I'm totally stuck at the moment :( Thank you!

Code: Select all

TAG XPATH="/*/Summary[1]/Payer" EXTRACT=TXT
   
SET a1  EVAL("'{{!EXTRACT}}'.replace(/(EXTRACT|#EANF#)/g, '');")
chivracq
Posts: 8997
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Convert imacro code to javascript

Post by chivracq » Fri Sep 29, 2017 4:30 pm

DDon wrote:Hi Chivracq,

I get your point. I'm not really a programmer and I learn iMacro and java bit by bit when I'm trying to code my own iMacro. The codes have been working well in iim format, but they are getting more than a thousand line as I keep copy and paste without the code without using the loop function. Due to my work nature (speed priority), my imacro need to click and run rather than open the imacro and put the number of loop then click play. This push me to the limit upgrade/convert my current iim code to .js.

If you know the solution to conver these two line codes in to javascript for imacro, I think that will be the key that help me finish the rest of the looping code. I'm totally stuck at the moment :( Thank you!

Code: Select all

TAG XPATH="/*/Summary[1]/Payer" EXTRACT=TXT
   
SET a1  EVAL("'{{!EXTRACT}}'.replace(/(EXTRACT|#EANF#)/g, '');")
Well, I don't know really as I don't use '.js' Scripts (I deliberately do all my Scripts in pure '.iim' to avoid JS that I don't know either, except for some "Basic" Data Manipulation on Numbers/Strings/Arrays in 'EVAL()') and I don't use 'XPATH' either, so you are already more "Advanced" than me on those 2 "Fields", ah-ah...! :wink:
Hum, and I don't use 'REGEX' either like in your 'replace()', so that makes 3 Fields, oops...! :oops:

Everything I know about '.js' Scripts is from other Users posting their Scripts on the Forum, then I can "discern" if stg is correct or not, but you didn't post (the rest of) your '.js' Script, or what you've tried, then I can't tell you what is wrong, and I can't/wouldn't be able to test...

But normally, you simply need to escape Double Quotes, => _"_ would become _\"_, and if you have several Levels of embedded Double Quotes, you then need to double or triple(...?, I never know...) the Backslashes, or like I say, alternate Single and Double Quotes which often works and is then easier... and makes your Statement as well visually easier to understand instead of having to count the Double Quotes...

And for your 'XPATH', you can try (already in '.iim') if removing the Double Quotes still works (as your 'XPATH' doesn't contain any Space(s)), that would already be one easy Solution, it seems to work, from a quick Test, for the 'SELECTOR' Parameter in the 'EVENT' Mode which according to the Wiki also expects Double Quotes like the 'XPATH' Parameter... (Tested (in '.iim') on iMacros for FF v8.8.2, Pale Moon v26.3.3 (=FF47), Win10_x64.)
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
DDon
Posts: 20
Joined: Sat Aug 06, 2016 1:41 pm

Re: Convert imacro code to javascript

Post by DDon » Sun Oct 01, 2017 12:15 am

Thanks, remove the __"__ in the first line of code work in .iim but it doesn't work in javascript :(, I feel like the javascript code doesn't get the part 'EXTRACT=TXT'
iim:

Code: Select all

TAG XPATH="/*/Summary[1]/Payer" EXTRACT=TXT
Javascript

Code: Select all

var macro;
macro =  "CODE:";


macro += "TAG XPATH=/*/Summary[1]/Payer EXTRACT=TXT" +"/n";
 

iimPlay(macro);
Error : BadParameter: expected EXTRACT=TXT|TXTALL|HTM|TITLE|ALT|HREF|CHECKED as parameter 5, line 1 (Error code: -911)
chivracq
Posts: 8997
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Convert imacro code to javascript

Post by chivracq » Sun Oct 01, 2017 6:16 am

DDon wrote:Thanks, remove the __"__ in the first line of code work in .iim but it doesn't work in javascript :(, I feel like the javascript code doesn't get the part 'EXTRACT=TXT'
iim:

Code: Select all

TAG XPATH="/*/Summary[1]/Payer" EXTRACT=TXT
Javascript

Code: Select all

var macro;
macro =  "CODE:";

macro += "TAG XPATH=/*/Summary[1]/Payer EXTRACT=TXT" +"/n";

iimPlay(macro);
Error : BadParameter: expected EXTRACT=TXT|TXTALL|HTM|TITLE|ALT|HREF|CHECKED as parameter 5, line 1 (Error code: -911)
OK, so 'XPATH' works as well without Double Quotes, at least in '.iim', good to know...

But for the rest, I can't really help you without an URL and a Site to test on...!

Did you try with keeping the Double Quotes and escaping them otherwise...?

Is the 'EVAL()' part now solved...?
=> Post the Solution then...!

Sorry, but not my "Role" to keep asking the same Qt's or the most logical Follow-up Qt's, you need to be a bit proactive with your Info as well... :roll:
No Script + No URL, I can only give "general/generic" Advice, and I don't use XPATH+'.js'_Scripts(+REGEX) myself, so I need a Script applied to some precise Site to be able to do any Testing..., that you probably should be able to do yourself as I will be a complete Newbie for those Functionalities that I've never used for myself... :roll:
You have some 'XPATH' Demo, use it as a Testcase, then I can do some Testing if you don't want to post your "real" URL...

Even if hum, starting to get a bit "suspicious" about your "Goal" because of the "Secrecy", can you post the Name/URL of the Site...? And explain a bit your Purpose...?
(I don't want to help Users for Games/Votes/Social Media/Like/Follow/Share/Comment/Spam/Hacking/DDos/Financial+BitCoin/Captcha...)
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
DDon
Posts: 20
Joined: Sat Aug 06, 2016 1:41 pm

Re: Convert imacro code to javascript

Post by DDon » Sun Oct 01, 2017 12:37 pm

I'm work in Tax field so the real URL is from Tax department database, due to sensitive information, I can't post the information in the general public, I hope you understand that. However, I found alternative xml site for testing purposes, pretty much the same thing but different content. The main goal is to extract data for the accounting calculation :wink: .

Testing URL: https://www.w3schools.com/xml/simple.xml

iim working code:

Code: Select all

TAG XPATH=/breakfast_menu/food[1]/description EXTRACT=TXT
js code that still doesn't work:

Code: Select all

var macro;
macro =  "CODE:";
var a =  "TAG XPATH=/breakfast_menu/food[1]/description EXTRACT=TXT";

macro +=  a +"/n"; 


iimPlay(macro);

Eval code has not been solved yet as I still can't get the EXTRACT value :(.

I think that if we can get this XPATH EXTRACT code, I can manipulate the EXTRACT directly in the .js file rather than using EVAL. :o
chivracq
Posts: 8997
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Convert imacro code to javascript

Post by chivracq » Mon Oct 02, 2017 1:50 am

DDon wrote:I'm work in Tax field so the real URL is from Tax department database, due to sensitive information, I can't post the information in the general public, I hope you understand that. However, I found alternative xml site for testing purposes, pretty much the same thing but different content. The main goal is to extract data for the accounting calculation :wink: .

Testing URL: https://www.w3schools.com/xml/simple.xml

iim working code:

Code: Select all

TAG XPATH=/breakfast_menu/food[1]/description EXTRACT=TXT
js code that still doesn't work:

Code: Select all

var macro;
macro =  "CODE:";
var a =  "TAG XPATH=/breakfast_menu/food[1]/description EXTRACT=TXT";

macro +=  a +"/n"; 

iimPlay(macro);
Eval code has not been solved yet as I still can't get the EXTRACT value :(.

I think that if we can get this XPATH EXTRACT code, I can manipulate the EXTRACT directly in the .js file rather than using EVAL. :o
OK, I finally have a Site I can test on, sorry but I get a bit "tired" if I have to ask several times, and it's usually completely "approximate" to try to help sbd if Script and URL are not posted, especially if I don't know the Functionality if I don't use it myself... But that's good now... :D

But hum..., you didn't say anything about XML, hum-hum...!
I get the following Runtime Error as soon as I try to run your '.iim' working mini-Script with the 'XPATH':

Code: Select all

TypeError: element.style is undefined, line 7 (Error code: -1001)
(Tested on iMacros for FF v8.8.2, Pale Moon v26.3.3 (=FF47), Win10_x64.)

Same Result without the 'EXTRACT'.
Same Result as well on iMacros for FF 8.9.7, FF v55.0.3, Win10_x64, even if the Page is displayed differently on FF with only the Data in one Block while I could see the Document Tree in PM.

If I try to record myself tagging your 'Description' Element, I am able to record "stg"..., on PM:

Code: Select all

TAG POS=1 TYPE=BREAKFAST_MENU ATTR=TXT:Belgian<SP>Waffles<SP>$5.95<SP>Two<SP>of<SP>our<SP>famous*
... and on FF:

Code: Select all

TAG POS=1 TYPE=DESCRIPTION ATTR=TXT:Two<SP>of<SP>our<SP>famous<SP>Belgian<SP>Waffles<SP>with*
... but both Env.'s still produce the same RuntimeError if I try to replay them. (And '!ERRORIGNORE' doesn't help...)
Same thing as well using the 'EVENT' Mode:

Code: Select all

EVENT TYPE=CLICK SELECTOR="breakfast_menu>food>description" BUTTON=0
>

I still find 2 Workarounds to access the Data that you want on the Source of the Page without using 'XPATH', and both using the 'TAG' or the 'EVENT' Mode, but both need an 'EVAL()' Statement to isolate the Data that you want to keep, so that could be a Workaround for the 'XPATH' part, but you'll still have to get the 'EVAL()' to work in the '.js' Script or to do that 'EVAL()' Content in the '.js':

Code: Select all

VERSION BUILD=8970419 RECORDER=FX
SET !ERRORIGNORE YES
SET !EXTRACT_TEST_POPUP NO
TAB T=1
URL GOTO=https://www.w3schools.com/xml/simple.xml

'TAG XPATH=/breakfast_menu/food[1]/description EXTRACT=TXT

'Open Page Source:
EVENTS TYPE=KEYPRESS SELECTOR=* CHARS="u" MODIFIERS="ctrl"
WAIT SECONDS=1
TAB T=2

'Using 'TAG' Mode:
TAG POS=1 TYPE=* ATTR=* EXTRACT=TXT
SET Description_TAG_Mode EVAL("var s='{{!EXTRACT}}'; var x,y,z; x=s.split('<description>'); y=x[1].split('<'); z=y[0]; z;")

'Using 'EVENT' Mode:
EVENTS TYPE=KEYPRESS SELECTOR=* CHARS="ac" MODIFIERS="ctrl"
SET Description_EVENT_Mode EVAL("var s='{{!CLIPBOARD}}'; var x,y,z; x=s.split('<description>'); y=x[1].split('<'); z=y[0]; z;")

PROMPT Description_TAG_Mode:<BR>_{{Description_TAG_Mode}}_<BR><BR>Description_EVENT_Mode:<BR>_{{Description_TAG_Mode}}_
(Tested on v8.8.2 + PM v26.3.3 and v8.9.7 + FF v55.0.3.)

=> Result in the 'PROMPT':

Code: Select all

Description_TAG_Mode:
_Two of our famous Belgian Waffles with plenty of real maple syrup_

Description_EVENT_Mode:
_Two of our famous Belgian Waffles with plenty of real maple syrup_
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE').
- I don't even read the Qt if that (required) Info is not mentioned...!
- Script & URL help a lot for more "educated" Help...
techimac
Posts: 311
Joined: Fri Feb 20, 2015 9:27 pm

Re: Convert imacro code to javascript

Post by techimac » Tue Oct 03, 2017 8:54 pm

Javascript

Code: Select all

var macro;
macro =  "CODE:";


macro += "TAG XPATH=/*/Summary[1]/Payer EXTRACT=TXT" +"/n";
 

iimPlay(macro);
Error : BadParameter: expected EXTRACT=TXT|TXTALL|HTM|TITLE|ALT|HREF|CHECKED as parameter 5, line 1 (Error code: -911)[/quote]

no error for me with this code

Code: Select all

var macro;
macro =  "CODE:";


macro += "TAG XPATH=\/*\/Summary[1]\/Payer EXTRACT=TXT" +"/n";
 

iimPlay(macro);
Post Reply