33

I have a list of URLs in a text file, for example,

http://url1
http://url2
http://url3

I wonder how to open them each in one tab in Firefox (or SeaMonkey), without the hassle of creating a new tab, copying into address bar and hitting return for each URL?

My OS is Ubuntu 10.10. Both command line and GUI solutions are welcome.

slhck
  • 223,558
  • 70
  • 607
  • 592
Tim
  • 16,963
  • 69
  • 181
  • 260

6 Answers6

36

You can save the following to a HTML file:

<!doctype html>
<html>
<head>
<title>Open Windows</title>
<script>
function openWindow(){
    var x = document.getElementById('a').value.split('\n');
    for (var i = 0; i < x.length; i++)
        if (x[i].indexOf('.') > 0)
            if (x[i].indexOf('://') < 0)
                window.open('http://'+x[i]);
            else
                window.open(x[i]);
}
</script>
<style>
html, body
{
    height : 99%;
    width  : 99%;
}

textarea
{
    height : 80%;
    width  : 90%;
}
</style>
</head>
<body>
<textarea id="a"></textarea>
<br>
<input type="button" value="Open Windows" onClick="openWindow()">
<input type="button" value="Clear" onClick="document.getElementById('a').value=''">
</body>
</html>

Now load the file in Firefox, copy the list of URLs in the textarea and click Open Windows.

Dennis
  • 48,917
  • 12
  • 130
  • 149
  • 1
    Hah... I haven't thought 'bout that one! I usually do it with `firefox \`cat file.txt\`` (as WakiMiko wrote). Anyway using your way will work on all OS. :D – tftd Feb 02 '12 at 16:39
  • 1
    Now this is what I call "for the win." +1 for platform independence. Would definitely accept this answer. Supported: SeaMonkey, FireFox, IE, Chrome, Safari, etc...Ubuntu, Windows, Mac, etc. – Matt Borja Jun 06 '12 at 22:39
  • Technically chrome blocking as pop-ups. But since I need source and I'm running fiddler, you made my day. Thanks a done. – Jones Apr 08 '13 at 15:12
  • This is perfect. Is there anyway to add a slight delay, lets say 5 seconds between opening each tab? – DomainsFeatured Nov 30 '16 at 17:16
  • @DomainsFeatured Sure. You could use `setInterval` to iterate over the domains and cancel it once all of them have been processed. – Dennis Nov 30 '16 at 17:29
  • Hey Dennis, could you post the code here please? http://stackoverflow.com/questions/40894481/how-to-add-delay-to-html-javascript-function I'm not sure how to do that. Would be so grateful! – DomainsFeatured Nov 30 '16 at 17:29
  • Awesome answer, I made a JSFiddle for anyone as lazy as I am - https://jsfiddle.net/y1dgezuL/ – Neil P Oct 21 '18 at 16:12
  • Made a functional and modernized version here -> https://jsfiddle.net/L06mkcvd/ – Erlend Aug 19 '21 at 22:11
32

A simple

firefox $(cat file.txt)

should suffice. It will pass each link as an argument to the firefox command, as long as every link is separated by whitespace.

slhck
  • 223,558
  • 70
  • 607
  • 592
jhenninger
  • 1,651
  • 12
  • 11
  • 1
    +1. Thanks! That works! I wonder if you happen to know how to do that in SeaMonkey? I tried `seamonkey $(cat urls)`, but only the url in the first line is opened. – Tim Feb 02 '12 at 17:03
  • 1
    if you're doing this over SSH to something running Xorg, you need to set the display to open firefox window in: `env DISPLAY=:0 firefox $(cat file.txt)` – zfogg May 31 '20 at 21:17
  • `cat` is not needed, `firefox $(< example-domains.txt)` is more streamlined — see https://stackoverflow.com/questions/11710552/useless-use-of-cat and https://unix.meta.stackexchange.com/questions/1261/remove-useless-uses-of-cat-or-not – user198350 Jun 16 '23 at 08:35
  • If the `--new-tab` [flag](https://wiki.mozilla.org/Firefox/CommandLineOptions) is added — `firefox --new-tab $(< example-domains.txt)` — Firefox only appears to open the first tab in the current window. See [my answer](https://superuser.com/a/1789351/598527) for a potential solution (but works on my machine). – user198350 Jun 16 '23 at 08:38
11

On windows you can create a batch file (named say, multiurl.bat):

@echo off    
for /F "eol=c tokens=1" %%i in (%1) do "C:\Program Files (x86)\Mozilla Firefox\firefox.exe" %%i

and then run multiurl.bat urls.txt from the command line and it will load the URLS in new tabs if FireFox is already open, or it will run it and then load the URLS.

tfitzgerald
  • 231
  • 1
  • 3
  • You aren't required to create a .bat file to use the `for` command. – Oliver Salzburg Feb 02 '12 at 16:25
  • 2
    This is not relevant - the users asks for a solution that will work on Linux machines! – tftd Feb 02 '12 at 16:37
  • 1
    @TheDevil Come on, this answer took longer than three minutes to test and type. I suggest tfitzgerald keeps this answer up, there might be others interested. I sure won't delete my answer for OS X. – Daniel Beck Feb 02 '12 at 17:20
  • @TheDevil Yes, he edited his question as I was writing my answer. – tfitzgerald Feb 03 '12 at 01:47
  • 1
    @OliverSalzburg That is correct. But I don't think I said you are required to...but why would you want to type that whole line each time you wanted to do this? That is what scripts are for! – tfitzgerald Feb 03 '12 at 01:48
  • @fitzgerald The user didn't specify how often he plans on using the functionality. If only a single use would be intended, creating a dedicated script is unnecessary. Your answer might leave the impression that a script is required for this to work. – Oliver Salzburg Feb 03 '12 at 10:29
  • Yes, the words, "you can" obviously translate to "you must" over the Internet. Thanks for reminding me though, if you were to do this just in a command prompt you would want to change the `%%i` to just `%i`. The double percent signs are only for when using the `for` command in a batch file. – tfitzgerald Feb 04 '12 at 21:51
8

On Mac OS X, save the following script as openurls.sh, run chmod +x openurls.sh in Terminal, and then type ./openurls.sh from the same directory.

#!/usr/bin/env bash

while read line ; do
    open -a Firefox "$line"
done < "/path/to/file-with-urls.txt"
Daniel Beck
  • 109,300
  • 14
  • 287
  • 334
2

Open your text file in firefox as

file:///C:/URLTextFile.txt
  1. Select the whole link
  2. Right click on it
  3. Click on "Open Link in new tab"
Siva Charan
  • 4,865
  • 2
  • 25
  • 29
0

To expand on an existing Linux answer, personally I strongly prefer opening all pages in the same (existing, open) browser window. Unfortunately Firefox (command-line flags) appears not to accept all the input at once and I had to implement delay (sleep) between actions with xargs (source):

<example-domains.txt xargs -I "%" sh -c '{ firefox --new-tab "%"; sleep 0.25; }'

You may be able to use a significantly shorter delay than 0.25 seconds. You can try out the method with a file containing (about the reserved domains):

https://example.com/
https://example.net/
https://example.org/
https://example.edu/
user198350
  • 3,769
  • 15
  • 50
  • 89
  • xargs 4.9.0, Mozilla Firefox 114.0 (documenting if the method turns out to be not entirely universal and compatible) – user198350 Jun 16 '23 at 08:30