Kirsle.net logo Kirsle.net

A Simple Web Browser Launcher

July 23, 2012 by Noah

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);

Tags:

Comments

There are 0 comments on this page. Add yours.

Add a Comment

Used for your Gravatar and optional thread subscription. Privacy policy.
You may format your message using GitHub Flavored Markdown syntax.