1#!/usr/bin/perl -w
2use HTTP::Proxy;
3use HTML::Parser;
4use HTTP::Proxy::BodyFilter::htmlparser;
5
6# define the filter (the most difficult part)
7# filters not using HTML::Parser are much simpler :-)
8
9my $parser = HTML::Parser->new( api_version => 3 );
10$parser->handler(
11    start => sub {
12        my ( $self, $tag, $text ) = @_;
13        $self->{output} .= $text;
14        $self->{output} .= "YOUR JAVASCRIPT HERE" if $tag eq 'body';
15    },
16    "self,tagname,text"
17);
18$parser->handler(
19    default => sub {
20        my ($self, $text) = @_;
21        $self->{output} .= $text;
22    },
23    "self,text"
24);
25
26# this is a read-write filter (rw => 1)
27# that is the reason why we had to copy everything into $self->{output}
28my $filter = HTTP::Proxy::BodyFilter::htmlparser->new( $parser, rw => 1 );
29
30# create and launch the proxy
31my $proxy = HTTP::Proxy->new(@ARGV);
32$proxy->push_filter( response => $filter, mime => 'text/html' );
33$proxy->start();
34
35