Le jeudi 29 janvier 2004 à 07:22, Ovid écrivait:
> --- Tony Bowden <[EMAIL PROTECTED]> wrote:
> > On Tue, Jan 27, 2004 at 10:37:48AM -0500, Potozniak, Andrew wrote:
> > > To make a long story short I can not get access to the source of
> > the bottom
> > > frame through JavaScript because of an access denied error.
> > 
> > This is a security feature in most browsers -
> 
> Andrew,
> 
> Hate to say it, but Tony's right.  I've run into this before and the
> problem is not insurmountable, but it means that you have to have your
> app running on a server.

Or that you need a proxy that'll modify the page on the fly (by adding
the javascript you need).

My pet module HTTP::Proxy (available on CPAN) can help you do this. :-)

I suppose you mostly need a filter that'll add the necessary code to
load the javascript somewhere near the opening <body> tag of each and
every text/html response.

The code of such a proxy is as simple as:

    use HTTP::Proxy;
    use HTML::Parser;
    use HTTP::Proxy::BodyFilter::htmlparser;

    # define the filter (the most difficult part)
    # filters not using HTML::Parser are much simpler
    my $parser = HTML::Parser->new( api_version => 3 );
    $parser->handler(
        start => sub {
            my ( $self, $tag, $text ) = @_;
            $self->{output} .= $text;
            $self->{output} .= "YOUR JAVASCRIPT HERE" if $tag eq 'body';
        },
        "self,tagname,text"
    );
    $parser->handler(
        default => sub {
            my ($self, $text) = @_;
            $self->{output} .= $text;
        },
        "self,text"
    );

    # this is a read-write filter (rw => 1)
    # that is the reason why we had to copy everything into $self->{output}
    my $filter = HTTP::Proxy::BodyFilter::htmlparser->new( $parser, rw => 1 );

    # create and launch the proxy
    my $proxy = HTTP::Proxy->new();
    $proxy->logmask( 1 ); # terse logs
    $proxy->push_filter( response => $filter, mime => 'text/html',
                         host => 'www.example.com' );
    $proxy->start();

And now you have all the javascript you need added to the HTML pages
you want. There is also a 'path' parameter to the push_filter() method,
you you want to add the javascript only to parts of the web site.

Note: I'm not very proud of the way I plugged HTML::Parser objects
into HTTP::Proxy. But HTML::Parser uses callbacks, just as HTTP::Proxy
(LWP::UA, actually) does. If anybody has better ideas, I all ears.

-- 
 Philippe "BooK" Bruhat

 Your reputation is what you make of it... and what you choose to take with
 you.                               (Moral from Groo The Wanderer #48 (Epic))

Reply via email to