| 1 | package HTTP::Proxy::GreaseMonkey::Script; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use HTML::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | HTTP::Proxy::GreaseMonkey::Script - A GreaseMonkey script. | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 VERSION | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | This document describes HTTP::Proxy::GreaseMonkey::Script version 0.05 | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =cut | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 5 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | use HTTP::Proxy::GreaseMonkey::Script; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | Represents a single GreaseMonkey user script. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 INTERFACE | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head2 C<< new >> | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 6 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 6 | my ( $class, $script_file ) = @_; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 6 | ▶ | my @stat = stat $script_file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | or croak "Can't stat $script_file ($!)"; | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 6 | ▶ | open my $sh, '<', $script_file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | or croak "Can't read $script_file ($!)"; | ||||||||||||||||||||||||||||
| 42 | 6 | my $script = do { local $/; <$sh> }; | |||||||||||||||||||||||||||
| 43 | 6 | close $sh; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 6 | my %meta = (); | |||||||||||||||||||||||||||
| 46 | 6 | ▶ | if ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | $script =~ m{^ \s* // \s+ ==UserScript== \s+ | ||||||||||||||||||||||||||||
| 48 | (.*?) ^ \s* // \s+==/UserScript== \s+ }xmsi | ||||||||||||||||||||||||||||
| 49 | ) { | ||||||||||||||||||||||||||||
| 50 | 6 | my $header = $1; | |||||||||||||||||||||||||||
| 51 | 6 | ▶ | while ( $header =~ m{ ^ \s* // \s+ \@(\w+)\s+(.+)$ }xmg ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 30 | ▶ | if ( $1 eq 'include' || $1 eq 'exclude' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$1 eq "include" or $1 eq "exclude"
MC/DC100%2/2
$1 eq "include" or $1 eq "exclude"
$1 eq "include"
$1 eq "exclude"
Truth table100%3/3
A or B
A= $1 eq "include"B= $1 eq "exclude"
| |||||||||||||||||||||||||||||
| 53 | 12 | push @{ $meta{$1} }, _gm_wildcard( $2 ); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | else { | ||||||||||||||||||||||||||||
| 56 | 18 | $meta{$1} = $2; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # Special case - if include is empty make it match anything | ||||||||||||||||||||||||||||
| 62 | 6 | ▶ | $meta{include} = [qr{}] unless $meta{include}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 6 | return bless { | |||||||||||||||||||||||||||
| 65 | file => $script_file, | ||||||||||||||||||||||||||||
| 66 | meta => \%meta, | ||||||||||||||||||||||||||||
| 67 | stat => \@stat, | ||||||||||||||||||||||||||||
| 68 | script => $script, | ||||||||||||||||||||||||||||
| 69 | }, | ||||||||||||||||||||||||||||
| 70 | $class; | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =head2 C<< match_uri >> | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =cut | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 14 | ▶ | sub match_uri { | ||||||||||||||||||||||||||
|
Subroutine
match_uri: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 14 | my ( $self, $uri ) = @_; | |||||||||||||||||||||||||||
| 79 | 14 | ▶ | for my $exc ( @{ $self->{meta}->{exclude} || [] } ) { | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'meta'}{'exclude'} || []
MC/DC100%1/1
$self->{'meta'}{'exclude'} || []
$self->{'meta'}{'exclude'}
Truth table100%2/2
A
A= $self->{'meta'}{'exclude'}
| |||||||||||||||||||||||||||||
| 80 | 13 | ▶ | return if $uri =~ $exc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 12 | ▶ | for my $inc ( @{ $self->{meta}->{include} || [] } ) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'meta'}{'include'} || []
MC/DC0%0/1
$self->{'meta'}{'include'} || []
$self->{'meta'}{'include'}
Truth table50%1/2
A
A= $self->{'meta'}{'include'}
| |||||||||||||||||||||||||||||
| 83 | 15 | ▶ | return 1 if $uri =~ $inc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 1 | return; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head2 C<< script >> | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | The Javascript source of this script. | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =cut | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 2 | ▶ | sub script { shift->{script} } | ||||||||||||||||||||||||||
|
Subroutine
script: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head2 C<< support >> | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | The Javascript support code for this script | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =cut | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 2 | ▶ | sub support { | ||||||||||||||||||||||||||
|
Subroutine
support: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 104 | 2 | ▶ | my $h = $self->{_html} ||= HTML::Tiny->new; | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"_html"} ||= "HTML::Tiny"->new
MC/DC0%0/2
$self->{"_html"} ||= "HTML::Tiny"->new
$self->{"_html"}
"HTML::Tiny"->new
Truth table33%1/3
A ||= B
A= $self->{"_html"}B= "HTML::Tiny"->new
| |||||||||||||||||||||||||||||
| 105 | my @args | ||||||||||||||||||||||||||||
| 106 | 2 | = map { $h->json_encode( $_ ) } ( $self->namespace, $self->name ); | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | return join "\n", map { | ||||||||||||||||||||||||||||
| 109 | 2 | "function GM_$_() { return GM__proxyFunction(" | |||||||||||||||||||||||||||
| 110 | . join( ', ', $h->json_encode( $_ ), @args ) | ||||||||||||||||||||||||||||
| 111 | . ", arguments) }" | ||||||||||||||||||||||||||||
| 112 | } qw( setValue getValue log ); | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =head2 C<< file >> | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | The filename of this script. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =cut | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 4 | ▶ | sub file { shift->{file} } | ||||||||||||||||||||||||||
|
Subroutine
file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head2 C<< stat >> | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Get the cached C<stat> array for this script. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =cut | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub stat { @{ shift->{stat} } } | ||||||||||||||||||||||||||
|
Subroutine
stat: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =head2 C<< name >> | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | The descriptive name of this script | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =cut | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 4 | ▶ | sub name { shift->{meta}->{name} } | ||||||||||||||||||||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head2 C<< namespace >> | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | The namespace of this script. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =cut | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 4 | ▶ | sub namespace { shift->{meta}->{namespace} } | ||||||||||||||||||||||||||
|
Subroutine
namespace: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head2 C<< description >> | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | The description of this script. | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =cut | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 2 | ▶ | sub description { shift->{meta}->{description} } | ||||||||||||||||||||||||||
|
Subroutine
description: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 12 | ▶ | sub _gm_wildcard { | ||||||||||||||||||||||||||
|
Subroutine
_gm_wildcard: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 156 | 12 | my $wc = shift; | |||||||||||||||||||||||||||
| 157 | my $pattern = join '', | ||||||||||||||||||||||||||||
| 158 | 12 | ▶ | map { $_ eq '*' ? '.*' : $_ eq '?' ? '.' : quotemeta( $_ ) } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 159 | split /([*?])/, $wc; | ||||||||||||||||||||||||||||
| 160 | 12 | return qr{^$pattern$}i; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 5 | 1; | |||||||||||||||||||||||||||
| 164 | __END__ | ||||||||||||||||||||||||||||