Hack #18. Rewrite the Web

Use the power of Perl to rewrite the web.

The Greasemonkey extension for Mozilla Firefox and related browsers is a powerful way to modify web pages to your liking. In fact, the Mozilla family projects are customizable in many ways—as long as you like writing C++, JavaScript, or XUL.

If your network doesn't run only Firefox, or if you just prefer to customize the Web with Perl instead of any other language, HTTP::Proxy can help.

The Hack

For whatever reason (registrar greed, mostly), plenty of useful sites such as Perl Monks have .com and .org domain names. One visitor might use http://www.perlmonks.com/, while the truly blessed saints prefer http://perlmonks.org/. That's all well and good except for the cases where you have logged in to the site through one domain name but not the others. Your HTTP cookie uses the specific domain name for identification.

Thus you may follow a link from somewhere that leads to the correct site with the incorrect domain name. How annoying!

Fixing this with HTTP::Proxy is easy though:

use strict; use warnings; use HTTP::Proxy ':log'; use HTTP::Proxy::HeaderFilter::simple; # start the proxy with the given command-line parameters my $proxy = HTTP::Proxy->new( @ARGV ); for my $redirect (<DATA>) { chomp $redirect; my ($pattern, $destination) = split( /\\|/, $redirect ); my $filter = get_filter( $destination ); $proxy->push_filter( host => $pattern, request => $filter ); } $proxy->start( ); my %filters; sub get_filter { my $site ...

Get Perl Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.