Extracting part of current URL and adding it as variable into script

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
Chris3460
Posts: 8
Joined: Mon Mar 11, 2019 3:00 pm

Extracting part of current URL and adding it as variable into script

Post by Chris3460 » Mon Mar 11, 2019 3:10 pm

Hello,

I'm fairly new to iMacros and trying to create a script to automatically create and complete a ticket in SalesForce. I'm able to get the actual ticket created, and I'm able to get that ticket closed, but I'm also required to have a support activity on the ticket. This requires clicking a button "New Support Activity." That when I click on the button, the result in iMacros includes part of the URL for the ticket. For example, if the ticket is at:

http://salesforce.com/500e0001X0cO?srPos=0&srKp=500

Clicking the button in iMacros results in:

TAG POS=1 TYPE=INPUT:BUTTON FORM=NAME:massActionForm_500e0001X0cO_00N320002ChTK ATTR=NAME:new_support_activity

Every time I try to do this, the result is exactly the same, except for that 500e0001X0cO portion. I know I can use !URLCURRENT to extract the current URL, but is there a way to parse that URL to grab everything between "http://salesforce.com/" and "?" and turn that into a variable to change my line to read something like:

TAG POS=1 TYPE=INPUT:BUTTON FORM=NAME:massActionForm_VARIABLE1_00N320002ChTK ATTR=NAME:new_support_activity

If I can get this part figured out, my life will be much easier!
chivracq
Posts: 8406
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Extracting part of current URL and adding it as variable into script

Post by chivracq » Mon Mar 11, 2019 3:21 pm

Chris3460 wrote:
Mon Mar 11, 2019 3:10 pm
Hello,

I'm fairly new to iMacros and trying to create a script to automatically create and complete a ticket in SalesForce. I'm able to get the actual ticket created, and I'm able to get that ticket closed, but I'm also required to have a support activity on the ticket. This requires clicking a button "New Support Activity." That when I click on the button, the result in iMacros includes part of the URL for the ticket. For example, if the ticket is at:

http://salesforce.com/500e0001X0cO?srPos=0&srKp=500

Clicking the button in iMacros results in:

Code: Select all

TAG POS=1 TYPE=INPUT:BUTTON FORM=NAME:massActionForm_500e0001X0cO_00N320002ChTK ATTR=NAME:new_support_activity
Every time I try to do this, the result is exactly the same, except for that 500e0001X0cO portion. I know I can use !URLCURRENT to extract the current URL, but is there a way to parse that URL to grab everything between "http://salesforce.com/" and "?" and turn that into a variable to change my line to read something like:

Code: Select all

TAG POS=1 TYPE=INPUT:BUTTON FORM=NAME:massActionForm_VARIABLE1_00N320002ChTK ATTR=NAME:new_support_activity
If I can get this part figured out, my life will be much easier!
CIM...! :mrgreen: (... For me to have a look..., read my Sig...)

Wondering what your Thread has "specific" with 'iMacros for FF' if you don't even mention your FCI...? :?
Try to select the/a "correct" Sub-Forum next time you'll open a Thread... (Would have been the 'General' or 'Data Extraction' Sub-Forum(s) for this one..., but no need to open a Duplicate now...)
- (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...
Chris3460
Posts: 8
Joined: Mon Mar 11, 2019 3:00 pm

Re: Extracting part of current URL and adding it as variable into script

Post by Chris3460 » Mon Mar 11, 2019 3:40 pm

chivracq wrote:
Mon Mar 11, 2019 3:21 pm
Chris3460 wrote:
Mon Mar 11, 2019 3:10 pm
Hello,

I'm fairly new to iMacros and trying to create a script to automatically create and complete a ticket in SalesForce. I'm able to get the actual ticket created, and I'm able to get that ticket closed, but I'm also required to have a support activity on the ticket. This requires clicking a button "New Support Activity." That when I click on the button, the result in iMacros includes part of the URL for the ticket. For example, if the ticket is at:

http://salesforce.com/500e0001X0cO?srPos=0&srKp=500

Clicking the button in iMacros results in:

Code: Select all

TAG POS=1 TYPE=INPUT:BUTTON FORM=NAME:massActionForm_500e0001X0cO_00N320002ChTK ATTR=NAME:new_support_activity
Every time I try to do this, the result is exactly the same, except for that 500e0001X0cO portion. I know I can use !URLCURRENT to extract the current URL, but is there a way to parse that URL to grab everything between "http://salesforce.com/" and "?" and turn that into a variable to change my line to read something like:

Code: Select all

TAG POS=1 TYPE=INPUT:BUTTON FORM=NAME:massActionForm_VARIABLE1_00N320002ChTK ATTR=NAME:new_support_activity
If I can get this part figured out, my life will be much easier!
CIM...! :mrgreen: (... For me to have a look..., read my Sig...)

Wondering what your Thread has "specific" with 'iMacros for FF' if you don't even mention your FCI...? :?
Try to select the/a "correct" Sub-Forum next time you'll open a Thread... (Would have been the 'General' or 'Data Extraction' Sub-Forum(s) for this one..., but no need to open a Duplicate now...)

Yeah, sorry. Literally my first post here, so pardon my not knowing what I'm doing. I posted in the FF forum because that's the version I'm using.

iMacros 10.0.2.1450

Firefox 65.0.2

Windows 7 Pro (Version 6.1 Build 7601; SP1)
chivracq
Posts: 8406
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Extracting part of current URL and adding it as variable into script

Post by chivracq » Mon Mar 11, 2019 4:01 pm

Chris3460 wrote:
Mon Mar 11, 2019 3:40 pm
Yeah, sorry. Literally my first post here, so pardon my not knowing what I'm doing. I posted in the FF forum because that's the version I'm using.

Code: Select all

iMacros 10.0.2.1450

Firefox 65.0.2

Windows 7 Pro (Version 6.1 Build 7601; SP1)
OK, FCI mentioned, good...! :D
Hum, 'Free' or 'PE' is still missing about your iMacros for FF Version...? (Would play a role for the Var Name...)

(And when posting/replying, you don't need to quote yourself again, that doesn't really help for "Readability" of the Thread...)

Then OK, to answer your Qt, yep, fairly easy to achieve, by using 'EVAL()' and "some" JavaScript String Manipulation Method(s), several ways possible, like 'replace()' + 'split()', for example, or 'split()' x2... :idea:

I would go for 'split()' x2 myself which I would find more "reliable" and more flexible than the 'replace()', as I'm a bit "surprised" btw by the "http" (and no "https") in the URL, and a 'split()' already on the "first part" would handle both Cases together ("http" + "https").

I let you "sweat" a little bit to try to find out the/a Solution by yourself and I'll post 1 or 2 Solution(s) a bit later on if you don't come out by yourself... :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...
Chris3460
Posts: 8
Joined: Mon Mar 11, 2019 3:00 pm

Re: Extracting part of current URL and adding it as variable into script

Post by Chris3460 » Mon Mar 11, 2019 4:08 pm

chivracq wrote:
Mon Mar 11, 2019 4:01 pm

OK, FCI mentioned, good...! :D
Hum, 'Free' or 'PE' is still missing about your iMacros for FF Version...? (Would play a role for the Var Name...)

(And when posting/replying, you don't need to quote yourself again, that doesn't really help for "Readability" of the Thread...)

Then OK, to answer your Qt, yep, fairly easy to achieve, by using 'EVAL()' and "some" JavaScript String Manipulation Method(s), several ways possible, like 'replace()' + 'split()', for example, or 'split()' x2... :idea:

I would go for 'split()' x2 myself which I would find more "reliable" and more flexible than the 'replace()', as I'm a bit "surprised" btw by the "http" (and no "https") in the URL, and a 'split()' already on the "first part" would handle both Cases together ("http" + "https").

I let you "sweat" a little bit to try to find out the/a Solution by yourself and I'll post 1 or 2 Solution(s) a bit later on if you don't come out by yourself... :wink:
More apologies. Just clicked "quote" to quote you. It's the free version. And the URL isn't really the URL, so don't be too surprised by the http vs https part. And I thank you for your assistance.
chivracq
Posts: 8406
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Extracting part of current URL and adding it as variable into script

Post by chivracq » Mon Mar 11, 2019 4:28 pm

Chris3460 wrote:
Mon Mar 11, 2019 4:08 pm
More apologies. Just clicked "quote" to quote you. It's the free version. And the URL isn't really the URL, so don't be too surprised by the http vs https part. And I thank you for your assistance.
Yeah, don't worry... Now, do your "Homework" and post your Solution... :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...
Chris3460
Posts: 8
Joined: Mon Mar 11, 2019 3:00 pm

Re: Extracting part of current URL and adding it as variable into script

Post by Chris3460 » Mon Mar 11, 2019 4:58 pm

From the look of it, I can't do this with the free version. For reference, I started playing with another post of yours that I found. Not sure if I even formatted this remotely correctly, but I threw this in:

Code: Select all

SET !VAR1 {{!URLCURRENT}}
SET TICKET EVAL ("var s=\"((VAR1))\";s.split(\"/\", 4);")
When I ran it, I get:

Code: Select all

FreewareLimit: Freeware version limit exceeded: user defined variables not allowed. Maximum number of variables is 3, line: 2
So yeah, it looks like this is a no go from the free version.
chivracq
Posts: 8406
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Extracting part of current URL and adding it as variable into script

Post by chivracq » Mon Mar 11, 2019 6:21 pm

[quote=Chris3460 post_id=81785 time=1552323490 user_id=34075]
From the look of it, I can't do this with the free version. For reference, I started playing with another post of yours that I found. Not sure if I even formatted this remotely correctly, but I threw this in:

[code]SET !VAR1 {{!URLCURRENT}}
SET TICKET EVAL ("var s=\"((VAR1))\";s.split(\"/\", 4);")[/code]

When I ran it, I get:

[code]FreewareLimit: Freeware version limit exceeded: user defined variables not allowed. Maximum number of variables is 3, line: 2[/code]

So yeah, it looks like this is a no go from the free version.
[/quote]

Okay..., that's a "start", and you are making several "Mistakes"... :wink:

But yep you can "do it" with the 'Free' Version... :D
1- The Limitation with the 'Free' Version for Vars is that you can only use the 3x '!VAR[1-3]' as a Name for your Vars, you cannot use 'User defined Vars', and your "TICKET" would be a 'User defined Var'.

2- The Name of '!VAR1' is "!VAR1" and not "VAR1", the '!' belongs to that Name...

3- Referencing Vars is done with Double Curly Brackets => "{{" + "}}" and not with Brackets => "((" + "))"...

4- The Syntax you are using for 'split()' with "s.split(\"Some_String\", n)" will return the n first Splits, => in your "Case" with "n=4", "s.split(\"/\", 4)" will return "http:,,salesforce.com,500e0001X0cO?srPos=0&srKp=500", which is not what you want...!

The Syntax you need to use is "s.split(\"Some_String\")[i]", where 'i' is the Index Number for each 'split()' Element, starting at 'i=0' for the first Element (which would give the same Result like "s.split(\"Some_String\", 1)" then)...

5- Hum, and be "careful" with the Space in "EVAL (...", I think iMacros for FF doesn't mind, but there is/was one Version that "didn't like" that extra Space...

OK, back to "Homework", ah-ah...! :wink:

Hum, "sorry" for the "[quote]" and "[code]" parts, I had to disable BBCode in this Post because of the "[i]" that gets interpreted and displayed as /Italic/... :oops:
- (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: 8406
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Extracting part of current URL and adding it as variable into script

Post by chivracq » Mon Mar 11, 2019 6:52 pm

Hum, and I don't know which "other post of mine" you were using as Example, but it was not "the best one" and must be from several years ago, 3-4-5 I think, that's a "very old" Syntax I was using indeed when I had started using 'EVAL()' + 'split()', and which indeed mostly only worked when you only need to keep the first part of the 'split()'...

If you search my Posts on "eval+double+split", you should find much "better" Examples with nearly the exact Solution you need in your Case, I must have posted dozens of Examples already... :idea:
- (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...
Chris3460
Posts: 8
Joined: Mon Mar 11, 2019 3:00 pm

Re: Extracting part of current URL and adding it as variable into script

Post by Chris3460 » Tue Mar 12, 2019 5:50 pm

Alright, so I'm getting somewhere!!! First, I was wrong in my initial post. My initial URL format is:

http://salesforce.com/500e0001X0cO

All I need to remove is the "http://salesforce.com/" portion of the URL. I was able to accomplish that with:

Code: Select all

SET !VAR1 EVAL("var parts = \"{{!URLCURRENT}}\".split(\"/\"); parts[parts.length-1];")
PROMPT {{!VAR1}}
With that, the prompt displayed

Code: Select all

500e0001X0cO
Now I just need to figure out how to change that to get that to display in the next line. I'm not sure how to format the line here, or if I just need to create a new variable, so any guidance would be great. I tried:

Code: Select all

TAG POS=1 TYPE=INPUT:BUTTON FORM=NAME:massActionForm_!VAR1_00N320002ChTK ATTR=NAME:new_support_activity
Which you'll probably look at and think "HUH?" but I really don't have a clue here.
Chris3460
Posts: 8
Joined: Mon Mar 11, 2019 3:00 pm

Re: Extracting part of current URL and adding it as variable into script

Post by Chris3460 » Tue Mar 12, 2019 5:57 pm

And I figured it out!

Code: Select all

TAG POS=1 TYPE=INPUT:BUTTON FORM=NAME:massActionForm_{{!VAR1}}_00N320002ChTK ATTR=NAME:new_support_activity
chivracq
Posts: 8406
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Extracting part of current URL and adding it as variable into script

Post by chivracq » Tue Mar 12, 2019 7:00 pm

Chris3460 wrote:
Tue Mar 12, 2019 5:50 pm
Alright, so I'm getting somewhere!!! First, I was wrong in my initial post. My initial URL format is:

http://salesforce.com/500e0001X0cO

All I need to remove is the "http://salesforce.com/" portion of the URL. I was able to accomplish that with:

Code: Select all

SET !VAR1 EVAL("var parts = \"{{!URLCURRENT}}\".split(\"/\"); parts[parts.length-1];")
PROMPT {{!VAR1}}
With that, the prompt displayed

Code: Select all

500e0001X0cO
Now I just need to figure out how to change that to get that to display in the next line. I'm not sure how to format the line here, or if I just need to create a new variable, so any guidance would be great. I tried:

Code: Select all

TAG POS=1 TYPE=INPUT:BUTTON FORM=NAME:massActionForm_!VAR1_00N320002ChTK ATTR=NAME:new_support_activity
Which you'll probably look at and think "HUH?" but I really don't have a clue here.
Chris3460 wrote:
Tue Mar 12, 2019 5:57 pm
And I figured it out!

Code: Select all

TAG POS=1 TYPE=INPUT:BUTTON FORM=NAME:massActionForm_{{!VAR1}}_00N320002ChTK ATTR=NAME:new_support_activity
Yep-yep-yep...!, very good...! :D
And you get a 9/10 for your Homework...!! :wink:

And I had already explained about '!VAR1' for the 'set' Functionality and '{{!VAR1}}' for the 'get' Functionality...

OK, now, your 'EVAL()' works but it can be "improved"/simplified a bit, and made more "robust" as I am "afraid" you might still get "Cases" where the part with the '?srPos=0&srKp=500' Parameters in the URL might still be present, and your 'EVAL()' won't work then, so you might still need the 2nd 'split()', "just to make sure"...

First I would adapt your:

Code: Select all

SET !VAR1 EVAL("var parts = \"{{!URLCURRENT}}\".split(\"/\"); parts[parts.length-1];")
PROMPT {{!VAR1}}
... into:

Code: Select all

SET !VAR1 EVAL("var u='{{!URLCURRENT}}'; var x,y,z; x=u.split('/'); y=x.length; z=x[y-1]; z;")
PROMPT URL:<SP>_{{!URLCURRENT}}_<BR>VAR1:<SP>_{{!VAR1}}_
Using several mini-Temp Vars inside the 'EVAL()' (=> x,y,z) makes it easier to debug by replacing the final 'z' for the Return with 'x' or 'y' to be able to check in the 'PROMPT' if those Vars already return the expected intermediary Values.

Using the 'length' Property of the 'split()' Array to dynamically compute the Index is clever and can be useful in some Cases but a bit "Overkill" in your Case I think, and I think I would myself have hard-coded the Index. But as your 'split()' is on the '/' and there are always several '/' in an URL, I would have also chosen to lower the Index by stg like:

Code: Select all

SET !VAR1 EVAL("var u='{{!URLCURRENT}}'; var x,y,z; x=u.split('.com/'); z=x[1]; z;")
PROMPT URL:<SP>_{{!URLCURRENT}}_<BR>VAR1:<SP>_{{!VAR1}}_
And to include a 2nd 'split()' in case the URL might still contain the '?srPos=0&srKp=500' part, that would give stg like:

Code: Select all

SET !VAR1 EVAL("var u='{{!URLCURRENT}}'; var x,y,z; x=u.split('.com/'); y=x[1].split('?'); z=y[0]; z;")
PROMPT URL:<SP>_{{!URLCURRENT}}_<BR>VAR1:<SP>_{{!VAR1}}_
And like you can see, the "x,y,z" Syntax I use can easily be modified and extended (and reused) for different/extra Functionality with minimal Editing...

(I didn't test any of those Statements, so I hope I didn't make any Typo... :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...
Post Reply