Open links in new tabs?

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
nma1919
Posts: 28
Joined: Fri Apr 17, 2009 1:56 pm

Open links in new tabs?

Post by nma1919 » Tue Jul 28, 2009 2:21 am

Hello,

I'm trying to find links on a website and have them open into another tab without switching to the new tab.

Is this possible?
nma1919
Posts: 28
Joined: Fri Apr 17, 2009 1:56 pm

Re: Open links in new tabs?

Post by nma1919 » Tue Jul 28, 2009 2:44 am

This is what I have right now.

Code: Select all

VERSION BUILD=6240709 RECORDER=FX
TAG POS={{!LOOP}} TYPE=A ATTR=TXT:Comments*
TAB OPEN NEW
TAB T={{!LOOP}}
The "comments*" is because the comment section is a unique link.

It would be wonderful if I can open everyone of those comment links on the homepage to their own tabs.
nma1919
Posts: 28
Joined: Fri Apr 17, 2009 1:56 pm

Re: Open links in new tabs?

Post by nma1919 » Tue Jul 28, 2009 2:23 pm

I keep testing this thing and I'm making progress.

If only I can get a new link for each new tab. It keeps copying over the same first link.

Here's the code

Code: Select all

VERSION BUILD=300002      
TAB T=1     
TAG POS={{!LOOP}} TYPE=A ATTR=HREF:*#comment* EXTRACT=HREF
TAB OPEN NEW
SET !VAR1 {{!LOOP}}
ADD !VAR1 1
TAB T={{!VAR1}}
URL GOTO={{!EXTRACT}}
TAB T=1
SET !EXTRACT_TEST_POPUP NO
nma1919
Posts: 28
Joined: Fri Apr 17, 2009 1:56 pm

Re: Open links in new tabs?

Post by nma1919 » Tue Jul 28, 2009 3:13 pm

Image
josephconlin
Posts: 190
Joined: Wed Aug 06, 2008 2:38 am

Re: Open links in new tabs?

Post by josephconlin » Tue Jul 28, 2009 8:47 pm

nma1919 wrote:I keep testing this thing and I'm making progress.

If only I can get a new link for each new tab. It keeps copying over the same first link.

Here's the code

Code: Select all

VERSION BUILD=300002      
TAB T=1     
TAG POS={{!LOOP}} TYPE=A ATTR=HREF:*#comment* EXTRACT=HREF
TAB OPEN NEW
SET !VAR1 {{!LOOP}}
ADD !VAR1 1
TAB T={{!VAR1}}
URL GOTO={{!EXTRACT}}
TAB T=1
SET !EXTRACT_TEST_POPUP NO
What do you mean by "it keeps copying over the same first link"? Do you get a new tab for each link, but each new tab opens to the same link, all of them being whatever the first link was? Or do you only get 1 new tab, and you keep running the macro, but every time you run it, it only processes 1 link? Or something else?

When using !LOOP, you have to make sure that you play the macro with the Play (Loop) button toward the bottom of the control, and not the Play button at the top of the control. You have to set the value in the Max: box to the number you want !LOOP to go up to.

I hope this helps. If I haven't addressed your specific question, can you please be more clear about what exactly happens when you run the macro?

Oh, I always use TAB NEW OPEN instead of TAB OPEN NEW, but maybe both work.
josephconlin
Posts: 190
Joined: Wed Aug 06, 2008 2:38 am

Re: Open links in new tabs?

Post by josephconlin » Tue Jul 28, 2009 8:51 pm

nma1919 wrote:Image
If you don't know for sure how many links there might be, it makes it hard to have to count and make sure you set the Max: field above the Play (Loop) button to the right value.

This is why I usually write my macros in Javascript. This allows me to do much more advanced looping and conditional processing. In your example, I would try to extract the HREF at the Javascript equivalent of !LOOP. Then, I would check !EXTRACT to see if it returned #EANF#. If it did, that means that it couldn't find a link that matches the description, so all of them have been processed. If it returns something other than #EANF#, it will be a link HREF that you can use to open in a new tab.

Hope this helps.
nma1919
Posts: 28
Joined: Fri Apr 17, 2009 1:56 pm

Re: Open links in new tabs?

Post by nma1919 » Tue Jul 28, 2009 9:40 pm

josephconlin wrote:
nma1919 wrote:Image
If you don't know for sure how many links there might be, it makes it hard to have to count and make sure you set the Max: field above the Play (Loop) button to the right value.

This is why I usually write my macros in Javascript. This allows me to do much more advanced looping and conditional processing. In your example, I would try to extract the HREF at the Javascript equivalent of !LOOP. Then, I would check !EXTRACT to see if it returned #EANF#. If it did, that means that it couldn't find a link that matches the description, so all of them have been processed. If it returns something other than #EANF#, it will be a link HREF that you can use to open in a new tab.

Hope this helps.
Thanks for your reply, Joseph! As you can probably tell, it's driving me crazy trying to figure this out on my own. lol

You're right, the macro opens multiple links into different tabs but each link is the same as the first one. It circulates to the other links that I want to open but it only opens the first link.

I test it by hitting the loop button of course. So I dont know what I'm doing wrong? How do I do this in Javascript? :oops:
josephconlin
Posts: 190
Joined: Wed Aug 06, 2008 2:38 am

Re: Open links in new tabs?

Post by josephconlin » Wed Jul 29, 2009 5:29 pm

nma1919 wrote:
josephconlin wrote:
nma1919 wrote:Image
If you don't know for sure how many links there might be, it makes it hard to have to count and make sure you set the Max: field above the Play (Loop) button to the right value.

This is why I usually write my macros in Javascript. This allows me to do much more advanced looping and conditional processing. In your example, I would try to extract the HREF at the Javascript equivalent of !LOOP. Then, I would check !EXTRACT to see if it returned #EANF#. If it did, that means that it couldn't find a link that matches the description, so all of them have been processed. If it returns something other than #EANF#, it will be a link HREF that you can use to open in a new tab.

Hope this helps.
Thanks for your reply, Joseph! As you can probably tell, it's driving me crazy trying to figure this out on my own. lol

You're right, the macro opens multiple links into different tabs but each link is the same as the first one. It circulates to the other links that I want to open but it only opens the first link.

I test it by hitting the loop button of course. So I dont know what I'm doing wrong? How do I do this in Javascript? :oops:
I don't normally do this, but I wrote a macro that should do pretty much what you want. Right now I have it set up to open all the links from the list page for an iMacros forum, like the Firefox forum.

To use it, save the code I post below in the same folder as your other macros as test.js (or whatever you want to call it. It just needs to end with .js so iMacros knows it's a Javascript macro). Go to a forum list page, like this one. http://forum.imacros.net/viewforum.php?f=11. Click on your .js macro and click the play button. You will get several notices in the iMacros sidebar that the website timed out, but those are safe to ignore. I told the macro to set the timeout very low and ignore errors so that it won't make you wait for each new tab to completely open before going back and starting to load the next one.

I've had issues where a loop I expect to end with #EANF# never does (because it finds more links than I expected), so I included a check that it doesn't run more than 100 times. You can remove that if you want. I also left in a commented line that contains the extract that you mentioned in your post. You can uncomment that line and comment the line that checks for forum posts, and that should get you most of the way done on your other site, if not all the way.

This macro is not guaranteed in any way, use at your own risk, etc etc. In your original post, you asked if it was possible to open links in new tabs without switching to the new tab. That never got answered in this topic, so the answer is "Not that I know of. The only way I know that it can be done is to open the new tab, switch to the new tab, go to the URL, then go back to the first tab."

As another side note, I think the reason that your original version with !LOOP didn't work is because !EXTRACT was being appended to rather than over written. Thus, the first time it had something like "http://www.1.com", then the next time it had something like "http://www.1.com[EXTRACT]http://www.2.com" and so on, and the URL GOTO just took the first part which never changed. You can fix that in the old version with SET !EXTRACT NULL after you do the URL GOTO={{!EXTRACT}}.

Hope this helps.

test.js

Code: Select all

//imacros-js:showsteps no

//Testing forum post http://forum.imacros.net/viewtopic.php?f=11&t=7537&sid=2103c60e70f9c7051dcda5264874a488
//Idea is to read something in common on the page and open each link in a new tab.
var macro, retcode, url="", pos=1, stop=false;
while(!stop)
{
	macro="CODE:";
	macro+="TAB T=1\n";
	macro+="TAG POS="+pos+" TYPE=A ATTR=CLASS:*topictitle* EXTRACT=HREF\n";
	//macro+="TAG POS="+pos+" TYPE=A ATTR=HREF:*#comment* EXTRACT=HREF\n";
	
	retcode = iimPlay(macro);

	if (retcode < 0)               // an error has occured
	{
		errtext = iimGetLastError();
		alert("1st Error "+retcode+": "+errtext);
		stop=true;
	}
	
	//Stop if extract found nothing or counter is too high as a precaution
	if((url = iimGetLastExtract()) == "#EANF#" || pos > 100)
	{
		stop=true;
		break;
	}
	else
	{
		macro="CODE:";
		macro+="SET !TIMEOUT 1\n";
		macro+="SET !ERRORIGNORE YES\n";
		macro+="TAB T=1\n";
		macro+="TAB NEW OPEN\n";
		macro+="TAB T="+new Number(pos+1)+"\n";
		macro+="URL GOTO="+url+"\n";
		macro+="TAB T=1\n";
		
		retcode = iimPlay(macro);

		if (retcode < 0)               // an error has occured
		{
			errtext = iimGetLastError();
			alert("2nd Error "+retcode+": "+errtext);
			stop=true;
		}
		pos++;
	}
}
nma1919
Posts: 28
Joined: Fri Apr 17, 2009 1:56 pm

Re: Open links in new tabs?

Post by nma1919 » Wed Jul 29, 2009 7:29 pm

Joseph, I'm speechless. This is exactly what I was looking for... HOLY COW!!! You have no idea how awesome this is!

Do you have a paypal account? I feel I owe you something for this.
josephconlin
Posts: 190
Joined: Wed Aug 06, 2008 2:38 am

Re: Open links in new tabs?

Post by josephconlin » Fri Jul 31, 2009 4:24 pm

nma1919 wrote:Joseph, I'm speechless. This is exactly what I was looking for... HOLY COW!!! You have no idea how awesome this is!

Do you have a paypal account? I feel I owe you something for this.
I have thought long and hard about how to respond to this. My normal inclination is to say that I got to where I am by learning from others, and I would encourage everyone to share what they learn so that others can move ahead as well. No payment is expected or requested in exchange for my sharing.

However, I happen to have my oldest child in the hospital for a week now, and the bills for that will be extensive. While I still feel that no payment is requested or required, circumstances suggest that I shouldn't turn down any help freely extended any more than anyone should ignore the help offered on these forums.

In that vein, should you still desire to contribute something, you may contact me at josephconlin at gmail dot com for Paypal information.

Thanks for your kind words and thoughts.
Post Reply