| HTTP-Proxy-GreaseMonkey documentation | Contained in the HTTP-Proxy-GreaseMonkey distribution. |
HTTP::Proxy::GreaseMonkey::Script - A GreaseMonkey script.
This document describes HTTP::Proxy::GreaseMonkey::Script version 0.05
use HTTP::Proxy::GreaseMonkey::Script;
Represents a single GreaseMonkey user script.
newmatch_uriscriptThe Javascript source of this script.
supportThe Javascript support code for this script
fileThe filename of this script.
statGet the cached stat array for this script.
nameThe descriptive name of this script
namespaceThe namespace of this script.
descriptionThe description of this script.
HTTP::Proxy::GreaseMonkey::Script requires no configuration files or environment variables.
None.
None reported.
No bugs have been reported.
Please report any bugs or feature requests to
bug-http-proxy-greasemonkey@rt.cpan.org, or through the web interface at
http://rt.cpan.org.
Andy Armstrong <andy@hexten.net>
Copyright (c) 2007, Andy Armstrong <andy@hexten.net>.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.
| HTTP-Proxy-GreaseMonkey documentation | Contained in the HTTP-Proxy-GreaseMonkey distribution. |
package HTTP::Proxy::GreaseMonkey::Script; use strict; use warnings; use Carp; use HTML::Tiny;
our $VERSION = '0.05';
sub new { my ( $class, $script_file ) = @_; my @stat = stat $script_file or croak "Can't stat $script_file ($!)"; open my $sh, '<', $script_file or croak "Can't read $script_file ($!)"; my $script = do { local $/; <$sh> }; close $sh; my %meta = (); if ( $script =~ m{^ \s* // \s+ ==UserScript== \s+ (.*?) ^ \s* // \s+==/UserScript== \s+ }xmsi ) { my $header = $1; while ( $header =~ m{ ^ \s* // \s+ \@(\w+)\s+(.+)$ }xmg ) { if ( $1 eq 'include' || $1 eq 'exclude' ) { push @{ $meta{$1} }, _gm_wildcard( $2 ); } else { $meta{$1} = $2; } } } # Special case - if include is empty make it match anything $meta{include} = [qr{}] unless $meta{include}; return bless { file => $script_file, meta => \%meta, stat => \@stat, script => $script, }, $class; }
sub match_uri { my ( $self, $uri ) = @_; for my $exc ( @{ $self->{meta}->{exclude} || [] } ) { return if $uri =~ $exc; } for my $inc ( @{ $self->{meta}->{include} || [] } ) { return 1 if $uri =~ $inc; } return; }
sub script { shift->{script} }
sub support { my $self = shift; my $h = $self->{_html} ||= HTML::Tiny->new; my @args = map { $h->json_encode( $_ ) } ( $self->namespace, $self->name ); return join "\n", map { "function GM_$_() { return GM__proxyFunction(" . join( ', ', $h->json_encode( $_ ), @args ) . ", arguments) }" } qw( setValue getValue log ); }
sub file { shift->{file} }
sub stat { @{ shift->{stat} } }
sub name { shift->{meta}->{name} }
sub namespace { shift->{meta}->{namespace} }
sub description { shift->{meta}->{description} } sub _gm_wildcard { my $wc = shift; my $pattern = join '', map { $_ eq '*' ? '.*' : $_ eq '?' ? '.' : quotemeta( $_ ) } split /([*?])/, $wc; return qr{^$pattern$}i; } 1; __END__