Extracting and parsing URL

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
Posts: 2
Joined: Thu May 14, 2009 2:49 pm

Extracting and parsing URL

Post by srawcliffe » Thu May 14, 2009 3:20 pm

A particular site I visit regularly has the same content in different languages. Unfortunately, it doesn't have a button to take you from, say, the English version of a page to the French version of the same page. However, if you know the URL of the English page you can derive the URL of the French page:

English page: http://www.mysite.com/eng/siteeng/something.htm
French page: http://www.mysite.com/fre/sitefre/something.htm

Manually, you can go into the address bar and change both occurrences of eng to fre, hit enter and voilà, you're on the same page but in French!

So I want to create a macro that will read the current URL, replace occurrences of eng by fre and then take me to the URL it has generated.

All I need to do is get the URL and then manipulate it as a string.

In iMacros, I can get the URL of the current page using !URLCURRENT. But as far as I know, there are no string functions with which to replace eng by fre.

In javascript, I can use replace to replace all occurrences of eng in a string by fre. But how to get the current URL in the first place? When used within iMacros, location.href returns not the url of the page shown in the browser, but some useless url starting with chrome://.

Any suggestions?
Posts: 18
Joined: Fri May 08, 2009 2:32 pm

Re: Extracting and parsing URL

Post by Puss » Thu May 14, 2009 4:54 pm

How about ...

Use a javascript iMacro that runs some of the iimPlay(), iimSet() etc functions.
See: http://wiki.imacros.net/Command_Reference

This js iMacro:

- uses iimPlay() to run a normal iMacro that saves the !CURRENTURL into !EXTRACT
- gets the URL into js using iimGetLastExtract (http://wiki.imacros.net/iimGetLastExtract%28%29)
- changes it from eng to fre or whatever you need
- uses iimSet to put that changed URL value back into iMacros
- uses iimPlay to reload the page using the altered URL

This assumes you are using Firefox so that you can use the built in js interface to run js iMacros.

Any good?
Posts: 190
Joined: Wed Aug 06, 2008 2:38 am

Re: Extracting and parsing URL

Post by josephconlin » Thu May 14, 2009 5:02 pm

I think you will find the concepts you need at these two links.



The general idea is to use a macro to SET !EXTRACT {{!URLCURRENT}} so that you can then use iimGetLastExtract() to get the value of the URL into Javascript. Once there, you can manipulate it and use it as needed.

Hope this helps. Looks like Puss gave you the same information, too. :)
Posts: 2
Joined: Thu May 14, 2009 2:49 pm

Re: Extracting and parsing URL

Post by srawcliffe » Fri May 15, 2009 2:39 pm


Thanks to both Puss and josephconlin. I now have a neat little set of three macros that does the trick nicely.

Here they are, just in case anyone else is interested ...


Code: Select all

oldurl = iimGetLastExtract();
newurl = oldurl.replace(/eng/ig, "fre");
iimSet("finalurl", newurl);

Code: Select all


Code: Select all

url goto ={{finalurl}}
Posts: 7
Joined: Wed Oct 03, 2012 12:31 am

Re: Extracting and parsing URL

Post by Jackson5280 » Mon Feb 25, 2013 11:57 pm

This helped me solve my issue. Thanks for posting your solution!
Post Reply