A Simple Web Browser Launcher

Kirsle
kirsle
Posted by Kirsle on Sunday, Jul 22 2012 @ 5:11 PM
I like to keep my Facebook separate from my other web browsing, so I use Google Chrome for Facebook and Firefox for everything else. This way, Facebook will have a harder time tracking me across the web through those "like" buttons if they don't have a logged-in cookie for me, since web browsers don't share cookies.

But this becomes annoying when somebody sends me a Facebook link on Pidgin, because it would open in Firefox when I click it and I'd have to copy/paste the link to Chrome instead. So, I wrote a little wrapper script to make this all automatic for me.

I set this script to be my default web browser, so anytime a program wants to open a link, it calls my script instead of Firefox. Then my script looks at the domain name from the URL, and opens it in Chrome if it's a facebook link, or Firefox otherwise.

Here's the script for others to use. You can also download it here.

#!/usr/bin/perl

# browser-wrap: Set this as your default browser to open certain links with
# certain browsers.
#
# To get `xdg-open` to use this, put this in your ~/.profile (update the path
# to match where you installed the script to):
#
# if [ -n "$DISPLAY" ]; then
#     BROWSER=/home/kirsle/bin/browser-wrap
# fi
#
# --Kirsle
# http://sh.kirsle.net/

use 5.14.0;
use strict;
use warnings;

#------------------------------------------------------------------------------#
# Configuration Section                                                        #
#------------------------------------------------------------------------------#

# Define your browser rules here.
my $rules = {
    # These are domain names to match. Use a regular expression.
    qr/(facebook|fbcdn)\.(com|net)/ => "google-chrome",
};

# Default browser for links that don't have rules that match.
my $default = "firefox";

#------------------------------------------------------------------------------#
# End Configuration Section                                                    #
#------------------------------------------------------------------------------#

# Get the URL passed in.
my $url = shift(@ARGV);
my $browser = $default;

# Looks okay?
if ($url =~ /^https?:\/\/([^\/]+)\/?/i) {
    print "Domain: $1\n";
    my $domain = $1;

    # Look for the best rule.
    my @sorted = sort { length($b) <=> length($a) } keys %{$rules};
    foreach my $rule (@sorted) {
        if ($domain =~ /$rule/i) {
            # Matched!
            $browser = $rules->{$rule};
            last;
        }
    }
}

# Launch the browser.
my ($app, @args) = split(/\s+/, $browser);
exec($app, @args, $url, @ARGV);

Categories: Linux

[ Kirsle's Blog ]

Comments

There are 0 comments on this page.

Leave a Comment

Login with Facebook
Your name:
Your Email: (optional)
Message:
You can use emoticons in your comment (opens in new window).

If you can see this, don't touch the following fields.

Kirsle
» Homepage (RSS)
» About Me
» Photo Albums
» Guestbook
» Contact Me
Channels
» Linux (47)
» General (43)
» Perl (34)
» Rant (20)
» Software (15)
» RiveScript (9)
» Gnome 3 (8)
» HowTo (8)
» Windows (8)
» HTML (7)
» Android (6)
» Design (6)
» Siikir (6)
» Tk (6)
» Curiosity (5)
» Blackhat (4)
» Gay (4)
» Java (4)
» Reviews (4)
» VirtualBox (4)
» DOS (3)
» KAGE (3)
» Licensing (3)
» Photos (3)
» Xfce (3)
» ttf2eot (3)
Creativity
» 3D Renderings
» Flash Animation
» JavaScript
» Fonts
» Metacity
» Tutorials
Software
» RiveScript
» Error Generator
» Tk Calculator
» Terminal Apps
» CyanChat Client
Web Tools
» TTF to EOT
» Text Fader
» Favicons
» Distance Calc
» Azulian Encoder
» XBM Masks
Subdomains
» Shell Scripts
» Linux RPMs
» Kirsle::Nano
» Minecraft Server
Miscellany
¤ Pokemon Fuchsia City
¤ DOS and Windows
¤ Raspberry Pi
Links
¤ Google+
¤ Facebook
¤ New MySpace
¤ Twitter
¤ Github
¤ CPAN
Fan Club
» Log In
» Sign Up

Stats
-= Today =-
> Total hits: 1003
> Unique: 589
-= All Time =-
> Total hits: 1339296
> Unique: 124203
» Traffic History
» Referrers