Home > bash, firefox, python > Open URLs in browser tabs simultaneously

Open URLs in browser tabs simultaneously

Problem

From Python, you can easily open URLs in Firefox tabs:

#!/usr/bin/env python

# open_in_tabs.py

import webbrowser
import sys

firefox = webbrowser.get('firefox')

for url in sys.stdin.readlines():
    url = url.rstrip("\n")
    firefox.open_new_tab(url)

This script reads URLs from the standard input and opens them in separated tabs. Usage:

cat url_list.txt | ./open_in_tabs.py

Where url_list.txt contains one URL per line.

It’s all very nice, but… The script will open the URLs one by one, which can be very slow (especially if you want to open several URLs). Would it be possible to open the URLs in tabs at the same time? That is, tell Firefox to assign a thread to each tab and process them simultaneously.

Solution

I found the solution in this thread. I won’t provide a source code, just sketch the idea:

  • If Firefox is not running yet, open the first URL with “firefox -new-window URL“. Do it with an external call.
  • For the rest of the URLs, open them with “firefox -new-tab URL“.

Demonstration:
Here I suppose you have a running Firefox instance.

cat url_list.txt | while read i; do firefox -new-tab $i; done

Useful links

Related posts

In the post “Extract all links from a web page“, I show an interesting usage of the script open_in_tabs.py. Links of a web page are extracted with another script, and the extracted links are opened in Firefox tabs with open_in_tabs.py. All that is done in one line using pipes. Example:

./get_links.py http://www.beach-hotties.com/ | grep -i jpg | ./open_in_tabs.py

Update (20110507)

The script is updated, now it can open tabs simultaneously too. Also, it can open links in a new window. See the source here.

About these ads
Categories: bash, firefox, python Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.
Follow

Get every new post delivered to your Inbox.

Join 61 other followers

%d bloggers like this: