WWW/Mechanize/Chrome/URLBlacklist.pm

filter: Statementstmt 34.6% 17 / 49 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 33.3% 3 / 9 Podpod 33.3% 1 / 3 totaltotal 29.5% 21 / 71 CC 6 SCAR 29.4
CC
6
Cov
29%
on_requestPaused
42.0
__ANON__
2.0
__ANON__
2.0
CRAP 18.9
« Chrome/DevToolsProtocol.pm ↑ Summary Chrome/DevToolsProtocol/Transport/Pipe.pm »
1 package WWW::Mechanize::Chrome::URLBlacklist;
2 1 ▶use Moo 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.80';
7  
8 =head1 NAME
9  
10 WWW::Mechanize::Chrome::URLBlacklist - blacklist URLs from fetching
11  
12 =head1 SYNOPSIS
13  
14 use WWW::Mechanize::Chrome;
15 use WWW::Mechanize::Chrome::URLBlacklist;
16  
17 my $mech = WWW::Mechanize::Chrome->new();
18 my $bl = WWW::Mechanize::Chrome::URLBlacklist->new(
19 blacklist => [
20 qr!\bgoogleadservices\b!,
21 ],
22 whitelist => [
23 qr!\bcorion\.net\b!,
24 ],
25  
26 # fail all unknown URLs
27 default => 'failRequest',
28 # allow all unknown URLs
29 # default => 'continueRequest',
30  
31 on_default => sub {
32 warn "Ignored URL $_[0] (action was '$_[1]')",
33 },
34 );
35 $bl->enable($mech);
36  
37 =head1 DESCRIPTION
38  
39 This module allows an easy approach to whitelisting/blacklisting URLs
40 so that Chrome does not make requests to the blacklisted URLs.
41  
42 =head1 ATTRIBUTES
43  
44 =head2 C<< whitelist >>
45  
46 Arrayref containing regular expressions of URLs to always allow fetching.
47  
48 =cut
49  
50 has 'whitelist' => (
51 is => 'lazy',
52 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
53 1 );
54  
55 =head2 C<< blacklist >>
56  
57 Arrayref containing regular expressions of URLs to always deny fetching unless
58 they are matched by something in the C<whitelist>.
59  
60 =cut
61  
62 has 'blacklist' => (
63 is => 'lazy',
64 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 1 );
66  
67 =head2 C<< default >>
68  
69 default => 'continueRequest'
70  
71 The action to take if an URL appears neither in the C<whitelist> nor
72 in the C<blacklist>. The default is C<continueRequest>. If you want to block
73 all unknown URLs, use C<failRequest>
74  
75 =cut
76  
77 1 has 'default' => (
78 is => 'rw',
79 default => 'continueRequest',
80 );
81  
82 =head2 C<< on_default >>
83  
84 on_default => sub {
85 my( $url, $action ) = @_;
86 warn "Unknown URL <$url>";
87 };
88  
89 This callback is invoked for every URL that is neither in the whitelist nor
90 in the blacklist. This is useful to see what URLs are still missing a category.
91  
92 =cut
93  
94  
95 1 has 'on_default' => (
96 is => 'rw',
97 );
98  
99 =head2 C<< _mech >>
100  
101 (internal) The WWW::Mechanize::Chrome instance we are connected to
102  
103 =cut
104  
105 1 has '_mech' => (
106 is => 'rw',
107 );
108  
109 =head2 C<< _request_listener >>
110  
111 (internal) The request listener created by WWW::Mechanize::Chrome while listening
112 for URL messages
113  
114 =cut
115  
116 1 has '_request_listener' => (
117 is => 'rw',
118 );
119  
120 =head1 METHODS
121  
122 =head2 C<< ->new >>
123  
124 my $bl = WWW::Mechanize::Chrome::URLBlacklist->new(
125 blacklist => [
126 qr!\bgoogleadservices\b!,
127 qr!\ioam\.de\b!,
128 qr!\burchin\.js$!,
129 qr!.*\.(?:woff|ttf)$!,
130 qr!.*\.css(\?\w+)?$!,
131 qr!.*\.png$!,
132 qr!.*\bfavicon.ico$!,
133 ],
134 );
135 $bl->enable( $mech );
136  
137 Creates a new instance of a blacklist, but does B<not> activate it yet.
138 See C<< ->enable >> for that.
139  
140 =cut
141  
142 0 ▶sub on_requestPaused( $self, $info ) {
Subroutine on_requestPaused: not called CC=6 SCAR=37.4
Pod undocumented
143 0 my $id = $info->{params}->{requestId};
144 0 my $request = $info->{params}->{request};
145 0 my $mech = $self->_mech;
146  
147 0 ▶ if( grep { $request->{url} =~ /$_/ } @{ $self->whitelist } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep {$request->{'url'} =~ /$_/;} @{$self->whitelist;}) { } 0 0 0
elsif (grep {$request->{'url'} =~ /$_/;} @{$self->blacklist;}) { } 0 0 0
148 #warn "Whitelisted URL $request->{url}";
149 0 $mech->target->send_message('Fetch.continueRequest', requestId => $id, )->retain;
150  
151 0 } elsif( grep { $request->{url} =~ /$_/ } @{ $self->blacklist }) {
152 #warn "Whitelisted URL $request->{url}";
153 0 $mech->target->send_message('Fetch.failRequest', requestId => $id, errorReason => 'AddressUnreachable' )->retain;
154  
155 } else {
156  
157 0 my $action;
158 0 ▶ if( $self->default eq 'continueRequest' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->default eq 'continueRequest') { } 0 0 0
159 0 $mech->target->send_message('Fetch.continueRequest', requestId => $id, )->retain;
160 0 $action = 'continue';
161 } else {
162 0 $mech->target->send_message('Fetch.failRequest', requestId => $id, errorReason => 'AddressUnreachable' );
163 0 $action = 'fail';
164 };
165 0 ▶ if( my $cb = $self->on_default ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->on_default) 0 0 0
166 0 local $@;
167 0 my $ok = eval {
168 0 $cb->($request->{url}, $action);
169 0 1;
170 };
171 0 ▶ warn $@ if !$ok;
Branch: 1 branch
BranchTrueFalseTotal
if not $ok 0 0 0
172 };
173 };
174 };
175  
176 =head2 C<< ->enable >>
177  
178 $bl->enable( $mech );
179  
180 Attaches the blacklist to a WWW::Mechanize::Chrome object.
181  
182 =cut
183  
184 0 ▶sub enable( $self, $mech ) {
Subroutine enable: not called CC=1 SCAR=6.9
Pod documented
185 0 $self->_mech( $mech );
186 0 $self->_mech->target->send_message('Fetch.enable');
187 0 ▶ my $request_listener = $mech->add_listener('Fetch.requestPaused', sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
188 0 $self->on_requestPaused( @_ );
189 0 });
190 0 $self->_request_listener( $request_listener );
191 };
192  
193 =head2 C<< ->enable >>
194  
195 $bl->disable( $mech );
196  
197 Removes the blacklist to a WWW::Mechanize::Chrome object.
198  
199 =cut
200  
201 0 ▶sub disable( $self ) {
Subroutine disable: not called CC=1 SCAR=6.9
Pod undocumented
202 0 $self->request_listener(undef);
203 0 $self->_mech->target->send_message('Fetch.disable');
204 0 $self->_mech(undef);
205 };
206  
207 1 1;
208  
209 __END__
« Chrome/DevToolsProtocol.pm ↑ Summary Chrome/DevToolsProtocol/Transport/Pipe.pm »