| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Mozilla::Mechanize; |
|
2
|
13
|
|
|
13
|
|
23705
|
use strict; |
|
|
13
|
|
|
|
|
29
|
|
|
|
13
|
|
|
|
|
634
|
|
|
3
|
13
|
|
|
13
|
|
76
|
use warnings; |
|
|
13
|
|
|
|
|
28
|
|
|
|
13
|
|
|
|
|
1004
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
# $Id: Mechanize.pm,v 1.4 2005/10/07 12:17:20 slanning Exp $ |
|
6
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
|
7
|
|
|
|
|
|
|
|
|
8
|
13
|
|
|
13
|
|
47283
|
use Glib qw(FALSE G_PRIORITY_LOW); |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use URI; |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use Mozilla::DOM '0.22'; |
|
12
|
|
|
|
|
|
|
use Mozilla::Mechanize::Browser; |
|
13
|
|
|
|
|
|
|
use Mozilla::Mechanize::Form; |
|
14
|
|
|
|
|
|
|
use Mozilla::Mechanize::Input; |
|
15
|
|
|
|
|
|
|
use Mozilla::Mechanize::Link; |
|
16
|
|
|
|
|
|
|
use Mozilla::Mechanize::Image; |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 NAME |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
Mozilla::Mechanize - Like WWW::Mechanize but using Gtk2::MozEmbed |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
use Mozilla::Mechanize; |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
my $moz = Mozilla::Mechanize->new(); |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
$moz->get( $url ); |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
$moz->follow_link( text => $link_txt ); |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
$moz->form_name( $form_name ); |
|
33
|
|
|
|
|
|
|
$moz->set_fields( |
|
34
|
|
|
|
|
|
|
username => 'yourname', |
|
35
|
|
|
|
|
|
|
password => 'dummy' |
|
36
|
|
|
|
|
|
|
); |
|
37
|
|
|
|
|
|
|
$moz->click( $btn_name ); |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# Or all in one go: |
|
40
|
|
|
|
|
|
|
$moz->submit_form( |
|
41
|
|
|
|
|
|
|
form_name => $form_name, |
|
42
|
|
|
|
|
|
|
fields => { |
|
43
|
|
|
|
|
|
|
username => 'yourname', |
|
44
|
|
|
|
|
|
|
password => 'dummy', |
|
45
|
|
|
|
|
|
|
}, |
|
46
|
|
|
|
|
|
|
button => $btn_name, |
|
47
|
|
|
|
|
|
|
); |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=begin comment |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
XXX: not sure how to add headers |
|
52
|
|
|
|
|
|
|
(necko/nsIHttpAuthenticator.h ?) |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
Now also tries to support Basic-Authentication like LWP::UserAgent |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
use Mozilla::Mechanize; |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
my $moz = Mozilla::Mechanize->new( visible => 1 ); |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
$moz->credentials( 'pause.perl.org:443', 'PAUSE', 'abeltje', '********' ); |
|
61
|
|
|
|
|
|
|
$moz->get( 'https://pause.perl.org/pause/authenquery' ); |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=end comment |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
This module tries to be a sort of drop-in replacement for |
|
68
|
|
|
|
|
|
|
L. It uses Mozilla's Gecko HTML-rendering |
|
69
|
|
|
|
|
|
|
engine via the modules L and |
|
70
|
|
|
|
|
|
|
L. |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Don't expect it to be like L |
|
73
|
|
|
|
|
|
|
in that the class is not derived from the user-agent class (like LWP). |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
B
|
|
76
|
|
|
|
|
|
|
The docs are incomplete and may still refer to the Win32 module. |
|
77
|
|
|
|
|
|
|
Some methods are unimplemented.> |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Comment from Abe, which I echo: Thank you Andy Lester for |
|
80
|
|
|
|
|
|
|
L. I ported a lot of that code |
|
81
|
|
|
|
|
|
|
and nicked most of your documentation! |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
Ditto from me regarding Abe Timmerman's L. |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 CONSTRUCTION AND PROPERTIES |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=cut |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
my %moz_property = ( |
|
91
|
|
|
|
|
|
|
# addressbar => { type => 'b', value => undef }, |
|
92
|
|
|
|
|
|
|
fullscreen => { type => 'b', value => 0 }, |
|
93
|
|
|
|
|
|
|
# resizable => { type => 'b', value => undef }, |
|
94
|
|
|
|
|
|
|
# statusbar => { type => 'b', value => undef }, |
|
95
|
|
|
|
|
|
|
# toolbar => { type => 'b', value => undef }, |
|
96
|
|
|
|
|
|
|
visible => { type => 'b', value => 1 }, |
|
97
|
|
|
|
|
|
|
width => { type => 'n', value => 600 }, |
|
98
|
|
|
|
|
|
|
height => { type => 'n', value => 400 }, |
|
99
|
|
|
|
|
|
|
# left => { type => 'n', value => undef }, |
|
100
|
|
|
|
|
|
|
# top => { type => 'n', value => undef }, |
|
101
|
|
|
|
|
|
|
); |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head2 Mozilla::Mechanize->new( [%options] ) |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
This initializes a new browser window and sets all the |
|
107
|
|
|
|
|
|
|
properties that are passed via the C<%options> hash(ref). |
|
108
|
|
|
|
|
|
|
Currently supported options are: quiet, onwarn, onerror. |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
B. |
|
111
|
|
|
|
|
|
|
Currently supported Browser options are: height, width, visible, |
|
112
|
|
|
|
|
|
|
fullscreen. |
|
113
|
|
|
|
|
|
|
See C>. |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=cut |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub new { |
|
118
|
|
|
|
|
|
|
my $class = shift; |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
my $self = bless { |
|
121
|
|
|
|
|
|
|
_opt => {}, |
|
122
|
|
|
|
|
|
|
onwarn => \&Mozilla::Mechanize::__warn, |
|
123
|
|
|
|
|
|
|
onerror => \&Mozilla::Mechanize::__die, |
|
124
|
|
|
|
|
|
|
quiet => 0, |
|
125
|
|
|
|
|
|
|
debug => 0, |
|
126
|
|
|
|
|
|
|
}, $class; |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
my %opt = @_ ? UNIVERSAL::isa( $_[0], 'HASH' ) ? %{ $_[0] } : @_ : (); |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# $self->{_opt} = { map { |
|
131
|
|
|
|
|
|
|
# ( $_ => __prop_value( $_, $opt{ $_ } ) ) |
|
132
|
|
|
|
|
|
|
# } grep exists $moz_property{ lc $_ } => keys %opt }; |
|
133
|
|
|
|
|
|
|
# Sets all default values (before it only set ones passed in) |
|
134
|
|
|
|
|
|
|
$self->{_opt} = { map { |
|
135
|
|
|
|
|
|
|
my $prop = lc $_; |
|
136
|
|
|
|
|
|
|
($prop => __prop_value($prop, $opt{$prop})) |
|
137
|
|
|
|
|
|
|
} keys %moz_property }; |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
# some more options not for Browser |
|
140
|
|
|
|
|
|
|
$self->{$_} = exists $opt{$_} ? $opt{$_} : undef |
|
141
|
|
|
|
|
|
|
for qw(quiet debug onwarn onerror); |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
$self->open(); |
|
144
|
|
|
|
|
|
|
} |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head2 $moz->set_property( %opt ) |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
B
|
|
149
|
|
|
|
|
|
|
certain options through `new' (which see).> |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Allows you to set these supported properties: |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=over 4 |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=item B |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Set the visibility of the Browser window. Setting this to 0 |
|
158
|
|
|
|
|
|
|
iconifies the window, while setting it to 1 deiconifies it. |
|
159
|
|
|
|
|
|
|
I'd be very happy if it wasn't necessary to even create a window, |
|
160
|
|
|
|
|
|
|
but I don't believe it's possible because of the way Mozilla |
|
161
|
|
|
|
|
|
|
is implemented (layout and DOM are apparently tightly coupled). |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=item B |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Set the height of the Browser window. |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=item B |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
Set the width of the Browser window. |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=item B (NOT SUPPORTED) |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
Set the visibility of the addressbar |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=item B |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
Set the Browser window to fullscreen. Setting it false unfullscreens. |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=item B (NOT SUPPORTED) |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
Set the resize-ability |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=item B (NOT SUPPORTED) |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
Set the visibility of the statusbar |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=item B (NOT SUPPORTED) |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
Set the visibility of the toolbar |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=item B (NOT SUPPORTED) |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
Set the left coordinate of the Browser window |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=item B (NOT SUPPORTED) |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
Set the top-coordinate of the Browser window |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=back |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=cut |
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
sub set_property { |
|
204
|
|
|
|
|
|
|
my $self = shift; |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
my %raw = @_ ? UNIVERSAL::isa( $_[0], 'HASH' ) ? %{ $_[0] } : @_ : (); |
|
207
|
|
|
|
|
|
|
my %opt = map { |
|
208
|
|
|
|
|
|
|
( $_ => __prop_value( $_, $raw{ $_ } ) ) |
|
209
|
|
|
|
|
|
|
} grep exists $moz_property{ lc $_ } => keys %raw; |
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
foreach my $prop ( keys %opt ) { |
|
212
|
|
|
|
|
|
|
defined $opt{ $prop } and |
|
213
|
|
|
|
|
|
|
$self->agent->{ $prop } = $opt{ $prop }; |
|
214
|
|
|
|
|
|
|
} |
|
215
|
|
|
|
|
|
|
} |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=head2 $moz->close |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
Close the Browser. |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=cut |
|
222
|
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
sub close { |
|
224
|
|
|
|
|
|
|
my $self = shift; |
|
225
|
|
|
|
|
|
|
$self->agent->quit(); |
|
226
|
|
|
|
|
|
|
$self->{agent} = undef; |
|
227
|
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
# XXX: do we need to run the GUI here? |
|
229
|
|
|
|
|
|
|
} |
|
230
|
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
sub open { |
|
232
|
|
|
|
|
|
|
my $self = shift; |
|
233
|
|
|
|
|
|
|
defined $self->{agent} and return; |
|
234
|
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
my $browser_opts = $self->{_opt}; |
|
236
|
|
|
|
|
|
|
$browser_opts->{debug} = $self->{debug}; |
|
237
|
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
$self->{agent} = Mozilla::Mechanize::Browser->new($browser_opts) |
|
239
|
|
|
|
|
|
|
or $self->die("Cannot create a new Browser"); |
|
240
|
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
# foreach my $prop ( keys %{ $self->{_opt} } ) { |
|
242
|
|
|
|
|
|
|
# defined $self->{_opt}{ $prop } and |
|
243
|
|
|
|
|
|
|
# $self->{agent}->{ $prop } = $self->{_opt}{ $prop }; |
|
244
|
|
|
|
|
|
|
# } |
|
245
|
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
return $self; |
|
247
|
|
|
|
|
|
|
} |
|
248
|
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=head2 $moz->agent |
|
250
|
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
Return a reference to the Browser object. |
|
252
|
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=cut |
|
254
|
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
sub agent { $_[0]->{agent} } |
|
256
|
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=head1 PAGE-FETCHING METHODS |
|
259
|
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
=head2 $moz->get( $url ) |
|
261
|
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
Fetch C<$url>. |
|
263
|
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
=cut |
|
265
|
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
sub get { |
|
267
|
|
|
|
|
|
|
my $self = shift; |
|
268
|
|
|
|
|
|
|
my $agent = $self->agent; |
|
269
|
|
|
|
|
|
|
my ($url) = @_; |
|
270
|
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
my $uri = $self->uri |
|
272
|
|
|
|
|
|
|
? URI->new_abs($url, $self->uri->as_string) |
|
273
|
|
|
|
|
|
|
: URI->new($url); |
|
274
|
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
# XXX: how to add headers? |
|
276
|
|
|
|
|
|
|
# $agent->navigate({ URL => $uri->as_string, |
|
277
|
|
|
|
|
|
|
# Headers => $self->_extra_headers($uri) }); |
|
278
|
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
$agent->embedded->load_url($uri->as_string); |
|
280
|
|
|
|
|
|
|
$self->_wait_while_busy; |
|
281
|
|
|
|
|
|
|
} |
|
282
|
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
=head2 $moz->reload() |
|
284
|
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
Reload the page. |
|
286
|
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
=cut |
|
288
|
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
sub reload { |
|
290
|
|
|
|
|
|
|
$_[0]->agent->embedded->reload('reloadnormal'); |
|
291
|
|
|
|
|
|
|
$_[0]->_wait_while_busy; |
|
292
|
|
|
|
|
|
|
} |
|
293
|
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
=head2 $moz->back() |
|
295
|
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
Go back a page in the browser history. |
|
297
|
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
=cut |
|
299
|
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
sub back { |
|
301
|
|
|
|
|
|
|
$_[0]->agent->embedded->go_back; |
|
302
|
|
|
|
|
|
|
$_[0]->_wait_while_busy; |
|
303
|
|
|
|
|
|
|
} |
|
304
|
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
=head1 STATUS METHODS |
|
306
|
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
=head2 $moz->success |
|
308
|
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
B
|
|
310
|
|
|
|
|
|
|
So this always returns true for now.> |
|
311
|
|
|
|
|
|
|
In fact, if a URL doesn't exist, it'll pop up a dialog. :/ |
|
312
|
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
=cut |
|
314
|
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
sub success { |
|
316
|
|
|
|
|
|
|
# $_[0]->agent->ReadyState >= 2; |
|
317
|
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
# XXX: uh?? |
|
319
|
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
return 1; |
|
321
|
|
|
|
|
|
|
} |
|
322
|
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
=head2 $moz->uri |
|
324
|
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
Return the URI of this document (as a URI object). |
|
326
|
|
|
|
|
|
|
Note: whenever you do a submit, Mozilla appends a question mark |
|
327
|
|
|
|
|
|
|
followed by any form input (name=value pairs separated by ampersands). |
|
328
|
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
=cut |
|
330
|
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
sub uri { |
|
332
|
|
|
|
|
|
|
my $self = shift; |
|
333
|
|
|
|
|
|
|
my $agent = $self->agent; |
|
334
|
|
|
|
|
|
|
my $embed = $agent->embedded; |
|
335
|
|
|
|
|
|
|
my $uri = $embed->get_location; |
|
336
|
|
|
|
|
|
|
URI->new($uri); |
|
337
|
|
|
|
|
|
|
} |
|
338
|
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
=head2 $moz->ct |
|
340
|
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
Fetch the content-type of the document. |
|
342
|
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
=cut |
|
344
|
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
sub ct { |
|
346
|
|
|
|
|
|
|
my $self = shift; |
|
347
|
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
my $doc = $self->get_document; |
|
349
|
|
|
|
|
|
|
my $diid = Mozilla::DOM::NSDocument->GetIID; |
|
350
|
|
|
|
|
|
|
my $nsdoc = $doc->QueryInterface($diid); |
|
351
|
|
|
|
|
|
|
return $nsdoc->GetContentType; |
|
352
|
|
|
|
|
|
|
} |
|
353
|
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
=head2 $moz->current_form |
|
355
|
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
Returns the current form as a C object. |
|
357
|
|
|
|
|
|
|
|
|
358
|
|
|
|
|
|
|
=cut |
|
359
|
|
|
|
|
|
|
|
|
360
|
|
|
|
|
|
|
sub current_form { |
|
361
|
|
|
|
|
|
|
my $self = shift; |
|
362
|
|
|
|
|
|
|
defined $self->{cur_form} or $self->form_number( 1 ); |
|
363
|
|
|
|
|
|
|
$self->{cur_form}; |
|
364
|
|
|
|
|
|
|
} |
|
365
|
|
|
|
|
|
|
|
|
366
|
|
|
|
|
|
|
=head2 $moz->is_html |
|
367
|
|
|
|
|
|
|
|
|
368
|
|
|
|
|
|
|
Return true if this is an HTML Document. |
|
369
|
|
|
|
|
|
|
|
|
370
|
|
|
|
|
|
|
=cut |
|
371
|
|
|
|
|
|
|
|
|
372
|
|
|
|
|
|
|
sub is_html { |
|
373
|
|
|
|
|
|
|
my $self = shift; |
|
374
|
|
|
|
|
|
|
return $self->ct eq 'text/html'; |
|
375
|
|
|
|
|
|
|
} |
|
376
|
|
|
|
|
|
|
|
|
377
|
|
|
|
|
|
|
=head2 $moz->title |
|
378
|
|
|
|
|
|
|
|
|
379
|
|
|
|
|
|
|
Fetch the C from the document. |
|
380
|
|
|
|
|
|
|
|
|
381
|
|
|
|
|
|
|
=cut |
|
382
|
|
|
|
|
|
|
|
|
383
|
|
|
|
|
|
|
sub title { $_[0]->agent->embedded->get_title } |
|
384
|
|
|
|
|
|
|
|
|
385
|
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
=head1 CONTENT-HANDLING METHODS |
|
387
|
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
=head2 $moz->content |
|
389
|
|
|
|
|
|
|
|
|
390
|
|
|
|
|
|
|
Fetch the HTML of the document. This won't exactly match |
|
391
|
|
|
|
|
|
|
the HTML that's sent by the server... (The DOCTYPE/DTD will not |
|
392
|
|
|
|
|
|
|
be there, the element is kind of generated so its attributes |
|
393
|
|
|
|
|
|
|
might be rearranged, and some linebreaks might be missing.) |
|
394
|
|
|
|
|
|
|
|
|
395
|
|
|
|
|
|
|
=cut |
|
396
|
|
|
|
|
|
|
|
|
397
|
|
|
|
|
|
|
sub content { |
|
398
|
|
|
|
|
|
|
my $self = shift; |
|
399
|
|
|
|
|
|
|
my $embed = $self->agent->embedded; |
|
400
|
|
|
|
|
|
|
my $html = ''; |
|
401
|
|
|
|
|
|
|
|
|
402
|
|
|
|
|
|
|
# Boohoo, no outerHTML |
|
403
|
|
|
|
|
|
|
my $docelem = $self->get_document_element(); |
|
404
|
|
|
|
|
|
|
|
|
405
|
|
|
|
|
|
|
# Try to output with any attributes |
|
406
|
|
|
|
|
|
|
$html .= '<' . lc($docelem->GetNodeName); |
|
407
|
|
|
|
|
|
|
if ($docelem->HasAttributes) { |
|
408
|
|
|
|
|
|
|
$html .= ' '; |
|
409
|
|
|
|
|
|
|
my $attrs = $docelem->GetAttributes; |
|
410
|
|
|
|
|
|
|
for (my $i = 0; $i < $attrs->GetLength; $i++) { |
|
411
|
|
|
|
|
|
|
my $attr = $attrs->Item($i); |
|
412
|
|
|
|
|
|
|
$html .= $attr->GetNodeName . '="' . $attr->GetNodeValue . '" '; |
|
413
|
|
|
|
|
|
|
} |
|
414
|
|
|
|
|
|
|
} |
|
415
|
|
|
|
|
|
|
$html .= ">\n"; |
|
416
|
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
# Add ... |
|
418
|
|
|
|
|
|
|
my $iid = Mozilla::DOM::NSHTMLElement->GetIID; |
|
419
|
|
|
|
|
|
|
my $nshtmlelement = $docelem->QueryInterface($iid); |
|
420
|
|
|
|
|
|
|
$html .= $nshtmlelement->GetInnerHTML; |
|
421
|
|
|
|
|
|
|
|
|
422
|
|
|
|
|
|
|
# Add back the closing html tag |
|
423
|
|
|
|
|
|
|
$html .= "\n\n"; |
|
424
|
|
|
|
|
|
|
|
|
425
|
|
|
|
|
|
|
return $html; |
|
426
|
|
|
|
|
|
|
} |
|
427
|
|
|
|
|
|
|
|
|
428
|
|
|
|
|
|
|
=head1 LINK METHODS |
|
429
|
|
|
|
|
|
|
|
|
430
|
|
|
|
|
|
|
=head2 $moz->links |
|
431
|
|
|
|
|
|
|
|
|
432
|
|
|
|
|
|
|
When called in a list context, returns a list of the links found in |
|
433
|
|
|
|
|
|
|
the last fetched page. In a scalar context it returns a reference to |
|
434
|
|
|
|
|
|
|
an array with those links. The links returned are all |
|
435
|
|
|
|
|
|
|
C objects. |
|
436
|
|
|
|
|
|
|
|
|
437
|
|
|
|
|
|
|
=cut |
|
438
|
|
|
|
|
|
|
|
|
439
|
|
|
|
|
|
|
sub links { |
|
440
|
|
|
|
|
|
|
my $self = shift; |
|
441
|
|
|
|
|
|
|
|
|
442
|
|
|
|
|
|
|
defined $self->{links} or $self->{links} = $self->_extract_links(); |
|
443
|
|
|
|
|
|
|
|
|
444
|
|
|
|
|
|
|
return wantarray ? @{ $self->{links} } : $self->{links}; |
|
445
|
|
|
|
|
|
|
} |
|
446
|
|
|
|
|
|
|
|
|
447
|
|
|
|
|
|
|
=head2 $moz->follow_link( %opt ) |
|
448
|
|
|
|
|
|
|
|
|
449
|
|
|
|
|
|
|
Uses the C<< $self->find_link() >> interface to locate a link and |
|
450
|
|
|
|
|
|
|
C<< $self->get() >> it. |
|
451
|
|
|
|
|
|
|
|
|
452
|
|
|
|
|
|
|
=cut |
|
453
|
|
|
|
|
|
|
|
|
454
|
|
|
|
|
|
|
sub follow_link { |
|
455
|
|
|
|
|
|
|
my $self = shift; |
|
456
|
|
|
|
|
|
|
my %parms = ( n => 1, @_ ); |
|
457
|
|
|
|
|
|
|
|
|
458
|
|
|
|
|
|
|
if ( $parms{n} eq "all" ) { |
|
459
|
|
|
|
|
|
|
delete $parms{n}; |
|
460
|
|
|
|
|
|
|
$self->warn( qq{follow_link( n => "all" ) is not valid} ); |
|
461
|
|
|
|
|
|
|
} |
|
462
|
|
|
|
|
|
|
|
|
463
|
|
|
|
|
|
|
my $link = $self->find_link( @_ ); |
|
464
|
|
|
|
|
|
|
$self->get( $link->url ) if $link; |
|
465
|
|
|
|
|
|
|
} |
|
466
|
|
|
|
|
|
|
|
|
467
|
|
|
|
|
|
|
=head2 $moz->find_link( [%options] ) |
|
468
|
|
|
|
|
|
|
|
|
469
|
|
|
|
|
|
|
This method finds a link in the currently fetched page. It returns a |
|
470
|
|
|
|
|
|
|
L object which describes the link. (You'll probably |
|
471
|
|
|
|
|
|
|
be most interested in the C property.) If it fails to find a |
|
472
|
|
|
|
|
|
|
link it returns undef. |
|
473
|
|
|
|
|
|
|
|
|
474
|
|
|
|
|
|
|
You can take the URL part and pass it to the C method. If that's |
|
475
|
|
|
|
|
|
|
your plan, you might as well use the C method directly, |
|
476
|
|
|
|
|
|
|
since it does the C for you automatically. |
|
477
|
|
|
|
|
|
|
|
|
478
|
|
|
|
|
|
|
Note that C<< >> tags are parsed out of the the HTML |
|
479
|
|
|
|
|
|
|
and treated as links so this method works with them. |
|
480
|
|
|
|
|
|
|
|
|
481
|
|
|
|
|
|
|
You can select which link to find by passing in one or more of these |
|
482
|
|
|
|
|
|
|
key/value pairs: |
|
483
|
|
|
|
|
|
|
|
|
484
|
|
|
|
|
|
|
=over 4 |
|
485
|
|
|
|
|
|
|
|
|
486
|
|
|
|
|
|
|
=item * C<< text => 'string', >> and C<< text_regex => qr/regex/, >> |
|
487
|
|
|
|
|
|
|
|
|
488
|
|
|
|
|
|
|
C matches the text of the link against I, which must be an |
|
489
|
|
|
|
|
|
|
exact match. To select a link with text that is exactly "download", use |
|
490
|
|
|
|
|
|
|
|
|
491
|
|
|
|
|
|
|
$mech->find_link( text => "download" ); |
|
492
|
|
|
|
|
|
|
|
|
493
|
|
|
|
|
|
|
C matches the text of the link against I. To select a |
|
494
|
|
|
|
|
|
|
link with text that has "download" anywhere in it, regardless of case, use |
|
495
|
|
|
|
|
|
|
|
|
496
|
|
|
|
|
|
|
$mech->find_link( text_regex => qr/download/i ); |
|
497
|
|
|
|
|
|
|
|
|
498
|
|
|
|
|
|
|
Note that the text extracted from the page's links are trimmed. For |
|
499
|
|
|
|
|
|
|
example, C<< foo >> is stored as 'foo', and searching for |
|
500
|
|
|
|
|
|
|
leading or trailing spaces will fail. |
|
501
|
|
|
|
|
|
|
|
|
502
|
|
|
|
|
|
|
=item * C<< url => 'string', >> and C<< url_regex => qr/regex/, >> |
|
503
|
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
Matches the URL of the link against I or I, as appropriate. |
|
505
|
|
|
|
|
|
|
The URL may be a relative URL, like F, depending on how |
|
506
|
|
|
|
|
|
|
it's coded on the page. |
|
507
|
|
|
|
|
|
|
|
|
508
|
|
|
|
|
|
|
=item * C<< url_abs => string >> and C<< url_abs_regex => regex >> |
|
509
|
|
|
|
|
|
|
|
|
510
|
|
|
|
|
|
|
Matches the absolute URL of the link against I or I, |
|
511
|
|
|
|
|
|
|
as appropriate. The URL will be an absolute URL, even if it's relative |
|
512
|
|
|
|
|
|
|
in the page. |
|
513
|
|
|
|
|
|
|
|
|
514
|
|
|
|
|
|
|
=item * C<< name => string >> and C<< name_regex => regex >> |
|
515
|
|
|
|
|
|
|
|
|
516
|
|
|
|
|
|
|
Matches the name of the link against I or I, as appropriate. |
|
517
|
|
|
|
|
|
|
|
|
518
|
|
|
|
|
|
|
=item * C<< tag => string >> and C<< tag_regex => regex >> |
|
519
|
|
|
|
|
|
|
|
|
520
|
|
|
|
|
|
|
Matches the tag that the link came from against I or I, |
|
521
|
|
|
|
|
|
|
as appropriate. The C is probably most useful to check for |
|
522
|
|
|
|
|
|
|
more than one tag, as in: |
|
523
|
|
|
|
|
|
|
|
|
524
|
|
|
|
|
|
|
$mech->find_link( tag_regex => qr/^(a|frame)$/ ); |
|
525
|
|
|
|
|
|
|
|
|
526
|
|
|
|
|
|
|
The tags and attributes looked at are defined below, at |
|
527
|
|
|
|
|
|
|
L<$mech->find_link() : link format>. |
|
528
|
|
|
|
|
|
|
|
|
529
|
|
|
|
|
|
|
=item * C<< n => number >> |
|
530
|
|
|
|
|
|
|
|
|
531
|
|
|
|
|
|
|
=back |
|
532
|
|
|
|
|
|
|
|
|
533
|
|
|
|
|
|
|
The C parms can be combined with the C or C parms |
|
534
|
|
|
|
|
|
|
as a numeric modifier. For example, |
|
535
|
|
|
|
|
|
|
C<< text => "download", n => 3 >> finds the 3rd link which has the |
|
536
|
|
|
|
|
|
|
exact text "download". |
|
537
|
|
|
|
|
|
|
|
|
538
|
|
|
|
|
|
|
If C is not specified, it defaults to 1. Therefore, if you don't |
|
539
|
|
|
|
|
|
|
specify any parms, this method defaults to finding the first link on the |
|
540
|
|
|
|
|
|
|
page. |
|
541
|
|
|
|
|
|
|
|
|
542
|
|
|
|
|
|
|
Note that you can specify multiple text or URL parameters, which |
|
543
|
|
|
|
|
|
|
will be ANDed together. For example, to find the first link with |
|
544
|
|
|
|
|
|
|
text of "News" and with "cnn.com" in the URL, use: |
|
545
|
|
|
|
|
|
|
|
|
546
|
|
|
|
|
|
|
$moz->find_link( text => "News", url_regex => qr/cnn\.com/ ); |
|
547
|
|
|
|
|
|
|
|
|
548
|
|
|
|
|
|
|
=cut |
|
549
|
|
|
|
|
|
|
|
|
550
|
|
|
|
|
|
|
sub find_link { |
|
551
|
|
|
|
|
|
|
my $self = shift; |
|
552
|
|
|
|
|
|
|
my %parms = ( n=>1, @_ ); |
|
553
|
|
|
|
|
|
|
|
|
554
|
|
|
|
|
|
|
my $wantall = ( $parms{n} eq "all" ); |
|
555
|
|
|
|
|
|
|
|
|
556
|
|
|
|
|
|
|
$self->_clean_keys( |
|
557
|
|
|
|
|
|
|
\%parms, |
|
558
|
|
|
|
|
|
|
qr/^(n|(text|url|url_abs|name|tag)(_regex)?)$/ |
|
559
|
|
|
|
|
|
|
); |
|
560
|
|
|
|
|
|
|
|
|
561
|
|
|
|
|
|
|
my @links = $self->links or return; |
|
562
|
|
|
|
|
|
|
|
|
563
|
|
|
|
|
|
|
my $nmatches = 0; |
|
564
|
|
|
|
|
|
|
my @matches; |
|
565
|
|
|
|
|
|
|
for my $link ( @links ) { |
|
566
|
|
|
|
|
|
|
if ( _match_any_link_parms($link,\%parms) ) { |
|
567
|
|
|
|
|
|
|
if ( $wantall ) { |
|
568
|
|
|
|
|
|
|
push( @matches, $link ); |
|
569
|
|
|
|
|
|
|
} else { |
|
570
|
|
|
|
|
|
|
++$nmatches; |
|
571
|
|
|
|
|
|
|
return $link if $nmatches >= $parms{n}; |
|
572
|
|
|
|
|
|
|
} |
|
573
|
|
|
|
|
|
|
} |
|
574
|
|
|
|
|
|
|
} # for @links |
|
575
|
|
|
|
|
|
|
|
|
576
|
|
|
|
|
|
|
if ( $wantall ) { |
|
577
|
|
|
|
|
|
|
return @matches if wantarray; |
|
578
|
|
|
|
|
|
|
return \@matches; |
|
579
|
|
|
|
|
|
|
} |
|
580
|
|
|
|
|
|
|
|
|
581
|
|
|
|
|
|
|
return; |
|
582
|
|
|
|
|
|
|
} |
|
583
|
|
|
|
|
|
|
|
|
584
|
|
|
|
|
|
|
# Stolen from WWW::Mechanize-1.08 |
|
585
|
|
|
|
|
|
|
# Used by find_links to check for matches |
|
586
|
|
|
|
|
|
|
# The logic is such that ALL parm criteria that are given must match |
|
587
|
|
|
|
|
|
|
sub _match_any_link_parms { |
|
588
|
|
|
|
|
|
|
my $link = shift; |
|
589
|
|
|
|
|
|
|
my $p = shift; |
|
590
|
|
|
|
|
|
|
|
|
591
|
|
|
|
|
|
|
# No conditions, anything matches |
|
592
|
|
|
|
|
|
|
return 1 unless keys %$p; |
|
593
|
|
|
|
|
|
|
|
|
594
|
|
|
|
|
|
|
return if defined $p->{url} |
|
595
|
|
|
|
|
|
|
and !( $link->url eq $p->{url} ); |
|
596
|
|
|
|
|
|
|
return if defined $p->{url_regex} |
|
597
|
|
|
|
|
|
|
and !( $link->url =~ $p->{url_regex} ); |
|
598
|
|
|
|
|
|
|
return if defined $p->{url_abs} |
|
599
|
|
|
|
|
|
|
and !( $link->url_abs eq $p->{url_abs} ); |
|
600
|
|
|
|
|
|
|
return if defined $p->{url_abs_regex} |
|
601
|
|
|
|
|
|
|
and !( $link->url_abs =~ $p->{url_abs_regex} ); |
|
602
|
|
|
|
|
|
|
return if defined $p->{text} |
|
603
|
|
|
|
|
|
|
and !( defined($link->text) and $link->text eq $p->{text} ); |
|
604
|
|
|
|
|
|
|
return if defined $p->{text_regex} |
|
605
|
|
|
|
|
|
|
and !( defined($link->text) and $link->text =~ $p->{text_regex} ); |
|
606
|
|
|
|
|
|
|
return if defined $p->{name} |
|
607
|
|
|
|
|
|
|
and !( defined($link->name) and $link->name eq $p->{name} ); |
|
608
|
|
|
|
|
|
|
return if defined $p->{name_regex} |
|
609
|
|
|
|
|
|
|
and !( defined($link->name) and $link->name =~ $p->{name_regex} ); |
|
610
|
|
|
|
|
|
|
return if defined $p->{tag} |
|
611
|
|
|
|
|
|
|
and !( $link->tag and lc( $link->tag ) eq lc( $p->{tag} ) ); |
|
612
|
|
|
|
|
|
|
return if defined $p->{tag_regex} |
|
613
|
|
|
|
|
|
|
and !( $link->tag and $link->tag =~ $p->{tag_regex} ); |
|
614
|
|
|
|
|
|
|
|
|
615
|
|
|
|
|
|
|
# Success: everything that was defined passed. |
|
616
|
|
|
|
|
|
|
return 1; |
|
617
|
|
|
|
|
|
|
} |
|
618
|
|
|
|
|
|
|
|
|
619
|
|
|
|
|
|
|
# Cleans the %parms parameter for the find_link and find_image methods. |
|
620
|
|
|
|
|
|
|
sub _clean_keys { |
|
621
|
|
|
|
|
|
|
my $self = shift; |
|
622
|
|
|
|
|
|
|
my $parms = shift; |
|
623
|
|
|
|
|
|
|
my $rx_keyname = shift; |
|
624
|
|
|
|
|
|
|
|
|
625
|
|
|
|
|
|
|
for my $key ( keys %$parms ) { |
|
626
|
|
|
|
|
|
|
my $val = $parms->{$key}; |
|
627
|
|
|
|
|
|
|
if ( $key !~ qr/$rx_keyname/ ) { |
|
628
|
|
|
|
|
|
|
$self->warn( qq{Unknown link-finding parameter "$key"} ); |
|
629
|
|
|
|
|
|
|
delete $parms->{$key}; |
|
630
|
|
|
|
|
|
|
next; |
|
631
|
|
|
|
|
|
|
} |
|
632
|
|
|
|
|
|
|
|
|
633
|
|
|
|
|
|
|
my $key_regex = ( $key =~ /_regex$/ ); |
|
634
|
|
|
|
|
|
|
my $val_regex = ( ref($val) eq "Regexp" ); |
|
635
|
|
|
|
|
|
|
|
|
636
|
|
|
|
|
|
|
if ( $key_regex ) { |
|
637
|
|
|
|
|
|
|
if ( !$val_regex ) { |
|
638
|
|
|
|
|
|
|
$self->warn( qq{$val passed as $key is not a regex} ); |
|
639
|
|
|
|
|
|
|
delete $parms->{$key}; |
|
640
|
|
|
|
|
|
|
next; |
|
641
|
|
|
|
|
|
|
} |
|
642
|
|
|
|
|
|
|
} else { |
|
643
|
|
|
|
|
|
|
if ( $val_regex ) { |
|
644
|
|
|
|
|
|
|
$self->warn( qq{$val passed as '$key' is a regex} ); |
|
645
|
|
|
|
|
|
|
delete $parms->{$key}; |
|
646
|
|
|
|
|
|
|
next; |
|
647
|
|
|
|
|
|
|
} |
|
648
|
|
|
|
|
|
|
if ( $val =~ /^\s|\s$/ ) { |
|
649
|
|
|
|
|
|
|
$self->warn( qq{'$val' is space-padded and cannot succeed} ); |
|
650
|
|
|
|
|
|
|
delete $parms->{$key}; |
|
651
|
|
|
|
|
|
|
next; |
|
652
|
|
|
|
|
|
|
} |
|
653
|
|
|
|
|
|
|
} |
|
654
|
|
|
|
|
|
|
} # for keys %parms |
|
655
|
|
|
|
|
|
|
} # _clean_keys() |
|
656
|
|
|
|
|
|
|
|
|
657
|
|
|
|
|
|
|
=head2 $moz->find_all_links( %opt ) |
|
658
|
|
|
|
|
|
|
|
|
659
|
|
|
|
|
|
|
Returns all the links on the current page that match the criteria. |
|
660
|
|
|
|
|
|
|
The method for specifying link criteria is the same as in |
|
661
|
|
|
|
|
|
|
C. Each of the links returned is in the same format |
|
662
|
|
|
|
|
|
|
as in C. |
|
663
|
|
|
|
|
|
|
|
|
664
|
|
|
|
|
|
|
In list context, C returns a list of the links. |
|
665
|
|
|
|
|
|
|
Otherwise, it returns a reference to the list of links. |
|
666
|
|
|
|
|
|
|
|
|
667
|
|
|
|
|
|
|
C with no parameters returns all links in the |
|
668
|
|
|
|
|
|
|
page. |
|
669
|
|
|
|
|
|
|
|
|
670
|
|
|
|
|
|
|
=cut |
|
671
|
|
|
|
|
|
|
|
|
672
|
|
|
|
|
|
|
sub find_all_links { |
|
673
|
|
|
|
|
|
|
my $self = shift; |
|
674
|
|
|
|
|
|
|
$self->find_link( @_, n => 'all' ); |
|
675
|
|
|
|
|
|
|
} |
|
676
|
|
|
|
|
|
|
|
|
677
|
|
|
|
|
|
|
=head1 IMAGE METHODS |
|
678
|
|
|
|
|
|
|
|
|
679
|
|
|
|
|
|
|
=head2 $moz->images |
|
680
|
|
|
|
|
|
|
|
|
681
|
|
|
|
|
|
|
Lists all the images on the current page. Each image is a |
|
682
|
|
|
|
|
|
|
Mozilla::Mechanize::Image object. In list context, returns a list of all |
|
683
|
|
|
|
|
|
|
images. In scalar context, returns an array reference of all images. |
|
684
|
|
|
|
|
|
|
|
|
685
|
|
|
|
|
|
|
B: Although L explicitly only supports |
|
686
|
|
|
|
|
|
|
|
|
687
|
|
|
|
|
|
|
constructs, this is B supported by IE, it must be: |
|
688
|
|
|
|
|
|
|
|
|
689
|
|
|
|
|
|
|
for IE to behave as expected. |
|
690
|
|
|
|
|
|
|
(XXX: not sure if this is true for Mozilla::Mechanize) |
|
691
|
|
|
|
|
|
|
|
|
692
|
|
|
|
|
|
|
=cut |
|
693
|
|
|
|
|
|
|
|
|
694
|
|
|
|
|
|
|
sub images { |
|
695
|
|
|
|
|
|
|
my $self = shift; |
|
696
|
|
|
|
|
|
|
|
|
697
|
|
|
|
|
|
|
$self->_extract_images unless defined $self->{images}; |
|
698
|
|
|
|
|
|
|
|
|
699
|
|
|
|
|
|
|
return wantarray ? @{ $self->{images} } : $self->{images}; |
|
700
|
|
|
|
|
|
|
} |
|
701
|
|
|
|
|
|
|
|
|
702
|
|
|
|
|
|
|
=head2 $moz->find_image() |
|
703
|
|
|
|
|
|
|
|
|
704
|
|
|
|
|
|
|
Finds an image in the current page. It returns a |
|
705
|
|
|
|
|
|
|
L object which describes the image. If it fails |
|
706
|
|
|
|
|
|
|
to find an image it returns undef. |
|
707
|
|
|
|
|
|
|
|
|
708
|
|
|
|
|
|
|
You can select which link to find by passing in one or more of these |
|
709
|
|
|
|
|
|
|
key/value pairs: |
|
710
|
|
|
|
|
|
|
|
|
711
|
|
|
|
|
|
|
=over 4 |
|
712
|
|
|
|
|
|
|
|
|
713
|
|
|
|
|
|
|
=item * C<< alt => 'string' >> and C<< alt_regex => qr/regex/, >> |
|
714
|
|
|
|
|
|
|
|
|
715
|
|
|
|
|
|
|
C matches the ALT attribute of the image against I, which must be a |
|
716
|
|
|
|
|
|
|
n |
|
717
|
|
|
|
|
|
|
exact match. To select a image with an ALT tag that is exactly "download", use |
|
718
|
|
|
|
|
|
|
|
|
719
|
|
|
|
|
|
|
$moz->find_image( alt => "download" ); |
|
720
|
|
|
|
|
|
|
|
|
721
|
|
|
|
|
|
|
C matches the ALT attribute of the image against a regular |
|
722
|
|
|
|
|
|
|
expression. To select an image with an ALT attribute that has "download" |
|
723
|
|
|
|
|
|
|
anywhere in it, regardless of case, use |
|
724
|
|
|
|
|
|
|
|
|
725
|
|
|
|
|
|
|
$moz->find_image( alt_regex => qr/download/i ); |
|
726
|
|
|
|
|
|
|
|
|
727
|
|
|
|
|
|
|
=item * C<< url => 'string', >> and C<< url_regex => qr/regex/, >> |
|
728
|
|
|
|
|
|
|
|
|
729
|
|
|
|
|
|
|
Matches the URL of the image against I or I, as appropriate. |
|
730
|
|
|
|
|
|
|
The URL may be a relative URL, like F, depending on how |
|
731
|
|
|
|
|
|
|
it's coded on the page. |
|
732
|
|
|
|
|
|
|
|
|
733
|
|
|
|
|
|
|
=item * C<< url_abs => string >> and C<< url_abs_regex => regex >> |
|
734
|
|
|
|
|
|
|
|
|
735
|
|
|
|
|
|
|
Matches the absolute URL of the image against I or I, |
|
736
|
|
|
|
|
|
|
as appropriate. The URL will be an absolute URL, even if it's relative |
|
737
|
|
|
|
|
|
|
in the page. |
|
738
|
|
|
|
|
|
|
|
|
739
|
|
|
|
|
|
|
=item * C<< tag => string >> and C<< tag_regex => regex >> |
|
740
|
|
|
|
|
|
|
|
|
741
|
|
|
|
|
|
|
Matches the tag that the image came from against I or I, |
|
742
|
|
|
|
|
|
|
as appropriate. The C is probably most useful to check for |
|
743
|
|
|
|
|
|
|
more than one tag, as in: |
|
744
|
|
|
|
|
|
|
|
|
745
|
|
|
|
|
|
|
$moz->find_image( tag_regex => qr/^(img|input)$/ ); |
|
746
|
|
|
|
|
|
|
|
|
747
|
|
|
|
|
|
|
The tags supported are C<< >> and C<< >>. |
|
748
|
|
|
|
|
|
|
|
|
749
|
|
|
|
|
|
|
=back |
|
750
|
|
|
|
|
|
|
|
|
751
|
|
|
|
|
|
|
If C is not specified, it defaults to 1. Therefore, if you don't |
|
752
|
|
|
|
|
|
|
specify any parms, this method defaults to finding the first image on the |
|
753
|
|
|
|
|
|
|
page. |
|
754
|
|
|
|
|
|
|
|
|
755
|
|
|
|
|
|
|
Note that you can specify multiple ALT or URL parameters, which |
|
756
|
|
|
|
|
|
|
will be ANDed together. For example, to find the first image with |
|
757
|
|
|
|
|
|
|
ALT text of "News" and with "cnn.com" in the URL, use: |
|
758
|
|
|
|
|
|
|
|
|
759
|
|
|
|
|
|
|
$moz->find_image( image => "News", url_regex => qr/cnn\.com/ ); |
|
760
|
|
|
|
|
|
|
|
|
761
|
|
|
|
|
|
|
The return value is a reference to an array containing a |
|
762
|
|
|
|
|
|
|
L object for every image in C<< $self->content >>. |
|
763
|
|
|
|
|
|
|
|
|
764
|
|
|
|
|
|
|
=cut |
|
765
|
|
|
|
|
|
|
|
|
766
|
|
|
|
|
|
|
sub find_image { |
|
767
|
|
|
|
|
|
|
my $self = shift; |
|
768
|
|
|
|
|
|
|
my %parms = ( n=>1, @_ ); |
|
769
|
|
|
|
|
|
|
|
|
770
|
|
|
|
|
|
|
my $wantall = ( $parms{n} eq "all" ); |
|
771
|
|
|
|
|
|
|
|
|
772
|
|
|
|
|
|
|
$self->_clean_keys( \%parms, qr/^(n|(alt|url|url_abs|tag)(_regex)?)$/ ); |
|
773
|
|
|
|
|
|
|
|
|
774
|
|
|
|
|
|
|
my @images = $self->images or return; |
|
775
|
|
|
|
|
|
|
|
|
776
|
|
|
|
|
|
|
my $nmatches = 0; |
|
777
|
|
|
|
|
|
|
my @matches; |
|
778
|
|
|
|
|
|
|
for my $image ( @images ) { |
|
779
|
|
|
|
|
|
|
if ( _match_any_image_parms($image,\%parms) ) { |
|
780
|
|
|
|
|
|
|
if ( $wantall ) { |
|
781
|
|
|
|
|
|
|
push( @matches, $image ); |
|
782
|
|
|
|
|
|
|
} else { |
|
783
|
|
|
|
|
|
|
++$nmatches; |
|
784
|
|
|
|
|
|
|
return $image if $nmatches >= $parms{n}; |
|
785
|
|
|
|
|
|
|
} |
|
786
|
|
|
|
|
|
|
} |
|
787
|
|
|
|
|
|
|
} # for @images |
|
788
|
|
|
|
|
|
|
|
|
789
|
|
|
|
|
|
|
if ( $wantall ) { |
|
790
|
|
|
|
|
|
|
return @matches if wantarray; |
|
791
|
|
|
|
|
|
|
return \@matches; |
|
792
|
|
|
|
|
|
|
} |
|
793
|
|
|
|
|
|
|
|
|
794
|
|
|
|
|
|
|
return; |
|
795
|
|
|
|
|
|
|
} |
|
796
|
|
|
|
|
|
|
|
|
797
|
|
|
|
|
|
|
# Used by find_images to check for matches |
|
798
|
|
|
|
|
|
|
# The logic is such that ALL parm criteria that are given must match |
|
799
|
|
|
|
|
|
|
sub _match_any_image_parms { |
|
800
|
|
|
|
|
|
|
my $image = shift; |
|
801
|
|
|
|
|
|
|
my $p = shift; |
|
802
|
|
|
|
|
|
|
|
|
803
|
|
|
|
|
|
|
# No conditions, anything matches |
|
804
|
|
|
|
|
|
|
return 1 unless keys %$p; |
|
805
|
|
|
|
|
|
|
|
|
806
|
|
|
|
|
|
|
return if defined $p->{url} |
|
807
|
|
|
|
|
|
|
and !( $image->url eq $p->{url} ); |
|
808
|
|
|
|
|
|
|
return if defined $p->{url_regex} |
|
809
|
|
|
|
|
|
|
and !( $image->url =~ $p->{url_regex} ); |
|
810
|
|
|
|
|
|
|
return if defined $p->{url_abs} |
|
811
|
|
|
|
|
|
|
and !( $image->url_abs eq $p->{url_abs} ); |
|
812
|
|
|
|
|
|
|
return if defined $p->{url_abs_regex} |
|
813
|
|
|
|
|
|
|
and !( $image->url_abs =~ $p->{url_abs_regex} ); |
|
814
|
|
|
|
|
|
|
return if defined $p->{alt} |
|
815
|
|
|
|
|
|
|
and !( defined($image->alt) and $image->alt eq $p->{alt} ); |
|
816
|
|
|
|
|
|
|
return if defined $p->{alt_regex} |
|
817
|
|
|
|
|
|
|
and !( defined($image->alt) and $image->alt =~ $p->{alt_regex} ); |
|
818
|
|
|
|
|
|
|
return if defined $p->{tag} |
|
819
|
|
|
|
|
|
|
and !( $image->tag and lc( $image->tag ) eq lc( $p->{tag} ) ); |
|
820
|
|
|
|
|
|
|
return if defined $p->{tag_regex} |
|
821
|
|
|
|
|
|
|
and !( $image->tag and $image->tag =~ $p->{tag_regex} ); |
|
822
|
|
|
|
|
|
|
|
|
823
|
|
|
|
|
|
|
# Success: everything that was defined passed. |
|
824
|
|
|
|
|
|
|
return 1; |
|
825
|
|
|
|
|
|
|
} |
|
826
|
|
|
|
|
|
|
|
|
827
|
|
|
|
|
|
|
=head2 $moz->find_all_images( ... ) |
|
828
|
|
|
|
|
|
|
|
|
829
|
|
|
|
|
|
|
Returns all the images on the current page that match the criteria. The |
|
830
|
|
|
|
|
|
|
method for specifying image criteria is the same as in C>. |
|
831
|
|
|
|
|
|
|
Each of the images returned is a L object. |
|
832
|
|
|
|
|
|
|
|
|
833
|
|
|
|
|
|
|
In list context, C returns a list of the images. |
|
834
|
|
|
|
|
|
|
Otherwise, it returns a reference to the list of images. |
|
835
|
|
|
|
|
|
|
|
|
836
|
|
|
|
|
|
|
C with no parameters returns all images in the |
|
837
|
|
|
|
|
|
|
page. |
|
838
|
|
|
|
|
|
|
|
|
839
|
|
|
|
|
|
|
|
|
840
|
|
|
|
|
|
|
=cut |
|
841
|
|
|
|
|
|
|
|
|
842
|
|
|
|
|
|
|
sub find_all_images { |
|
843
|
|
|
|
|
|
|
my $self = shift; |
|
844
|
|
|
|
|
|
|
return $self->find_image( @_, n=>'all' ); |
|
845
|
|
|
|
|
|
|
} |
|
846
|
|
|
|
|
|
|
|
|
847
|
|
|
|
|
|
|
=head1 FORM METHODS |
|
848
|
|
|
|
|
|
|
|
|
849
|
|
|
|
|
|
|
=head2 $moz->forms |
|
850
|
|
|
|
|
|
|
|
|
851
|
|
|
|
|
|
|
Lists all the forms on the current page. Each form is an |
|
852
|
|
|
|
|
|
|
Mozilla::Mechanize::Form object. In list context, returns a list of all forms. |
|
853
|
|
|
|
|
|
|
In scalar context, returns an array reference of all forms. |
|
854
|
|
|
|
|
|
|
|
|
855
|
|
|
|
|
|
|
=cut |
|
856
|
|
|
|
|
|
|
|
|
857
|
|
|
|
|
|
|
sub forms { |
|
858
|
|
|
|
|
|
|
my $self = shift ; |
|
859
|
|
|
|
|
|
|
$self->_extract_forms unless defined $self->{forms}; |
|
860
|
|
|
|
|
|
|
|
|
861
|
|
|
|
|
|
|
return wantarray ? @{ $self->{forms} } : $self->{forms}; |
|
862
|
|
|
|
|
|
|
} |
|
863
|
|
|
|
|
|
|
|
|
864
|
|
|
|
|
|
|
=head2 $moz->form_number( $number ) |
|
865
|
|
|
|
|
|
|
|
|
866
|
|
|
|
|
|
|
Selects the numberth form on the page as the target for subsequent |
|
867
|
|
|
|
|
|
|
calls to field() and click(). Also returns the form that was |
|
868
|
|
|
|
|
|
|
selected. Emits a warning and returns undef if there is no such form. |
|
869
|
|
|
|
|
|
|
Forms are indexed from 1, so the first form is number 1, not zero. |
|
870
|
|
|
|
|
|
|
|
|
871
|
|
|
|
|
|
|
=cut |
|
872
|
|
|
|
|
|
|
|
|
873
|
|
|
|
|
|
|
sub form_number { |
|
874
|
|
|
|
|
|
|
my $self = shift; |
|
875
|
|
|
|
|
|
|
|
|
876
|
|
|
|
|
|
|
my $number = shift || 1; |
|
877
|
|
|
|
|
|
|
$self->_extract_forms unless defined $self->{forms}; |
|
878
|
|
|
|
|
|
|
if ( @{ $self->{forms} } && $number <= @{ $self->{forms} } ) { |
|
879
|
|
|
|
|
|
|
$self->{cur_form} = $self->{forms}[ $number - 1 ]; |
|
880
|
|
|
|
|
|
|
} else { |
|
881
|
|
|
|
|
|
|
$self->warn( "There is no form numbered $number." ); |
|
882
|
|
|
|
|
|
|
return undef; |
|
883
|
|
|
|
|
|
|
} |
|
884
|
|
|
|
|
|
|
} |
|
885
|
|
|
|
|
|
|
|
|
886
|
|
|
|
|
|
|
=head2 $moz->form_name( $name ) |
|
887
|
|
|
|
|
|
|
|
|
888
|
|
|
|
|
|
|
Selects a form by name. If there is more than one form on the page |
|
889
|
|
|
|
|
|
|
with that name, then the first one is used, and a warning is |
|
890
|
|
|
|
|
|
|
generated. Also returns the form itself, or undef if it is not |
|
891
|
|
|
|
|
|
|
found. |
|
892
|
|
|
|
|
|
|
|
|
893
|
|
|
|
|
|
|
=cut |
|
894
|
|
|
|
|
|
|
|
|
895
|
|
|
|
|
|
|
sub form_name { |
|
896
|
|
|
|
|
|
|
my $self = shift; |
|
897
|
|
|
|
|
|
|
|
|
898
|
|
|
|
|
|
|
my $name = shift or return undef; |
|
899
|
|
|
|
|
|
|
$self->_extract_forms unless defined $self->{forms}; |
|
900
|
|
|
|
|
|
|
my @matches = grep $_->name && $_->name eq $name => @{ $self->{forms} }; |
|
901
|
|
|
|
|
|
|
if ( @matches ) { |
|
902
|
|
|
|
|
|
|
$self->warn( "There are " . scalar @matches . "forms named '$name'. " . |
|
903
|
|
|
|
|
|
|
"The first one was used." ) if @matches > 1; |
|
904
|
|
|
|
|
|
|
$self->{cur_form} = $matches[0]; |
|
905
|
|
|
|
|
|
|
} else { |
|
906
|
|
|
|
|
|
|
$self->warn( "There is no form named '$name'." ); |
|
907
|
|
|
|
|
|
|
return undef; |
|
908
|
|
|
|
|
|
|
} |
|
909
|
|
|
|
|
|
|
} |
|
910
|
|
|
|
|
|
|
|
|
911
|
|
|
|
|
|
|
=head2 $moz->field( $name[, $value[, $index]] ) |
|
912
|
|
|
|
|
|
|
|
|
913
|
|
|
|
|
|
|
=head2 $moz->field( $name, \@values[, $number ] ) |
|
914
|
|
|
|
|
|
|
|
|
915
|
|
|
|
|
|
|
Given the name of a field, set its value to the value specified. This |
|
916
|
|
|
|
|
|
|
applies to the current form (as set by the C> or |
|
917
|
|
|
|
|
|
|
C> method or defaulting to the first form on the page). |
|
918
|
|
|
|
|
|
|
|
|
919
|
|
|
|
|
|
|
The optional I<$number> parameter is used to distinguish between two fields |
|
920
|
|
|
|
|
|
|
with the same name. The fields are numbered from 1. |
|
921
|
|
|
|
|
|
|
|
|
922
|
|
|
|
|
|
|
=cut |
|
923
|
|
|
|
|
|
|
|
|
924
|
|
|
|
|
|
|
sub field { |
|
925
|
|
|
|
|
|
|
my $self = shift; |
|
926
|
|
|
|
|
|
|
|
|
927
|
|
|
|
|
|
|
my( $name, $value, $index ) = @_; |
|
928
|
|
|
|
|
|
|
my $form = $self->current_form; |
|
929
|
|
|
|
|
|
|
|
|
930
|
|
|
|
|
|
|
my @inputs = $form->find_input( $name ); |
|
931
|
|
|
|
|
|
|
$self->debug('field: num inputs = ' . scalar(@inputs)); |
|
932
|
|
|
|
|
|
|
|
|
933
|
|
|
|
|
|
|
@inputs or $self->warn( "No '$name' parameter exists" ); |
|
934
|
|
|
|
|
|
|
$index ||= 1; |
|
935
|
|
|
|
|
|
|
my $control = $inputs[ $index - 1 ]; |
|
936
|
|
|
|
|
|
|
defined $value ? $control->value( $value ) : $control->value(); |
|
937
|
|
|
|
|
|
|
} |
|
938
|
|
|
|
|
|
|
|
|
939
|
|
|
|
|
|
|
=head2 $moz->select( $name, $value ) |
|
940
|
|
|
|
|
|
|
|
|
941
|
|
|
|
|
|
|
=head2 $moz->select( $name, \@values ) |
|
942
|
|
|
|
|
|
|
|
|
943
|
|
|
|
|
|
|
Given the name of a C |
|
944
|
|
|
|
|
|
|
specified. If the field is not Eselect multipleE and the |
|
945
|
|
|
|
|
|
|
C<$value> is an array, only the B value will be set. Passing |
|
946
|
|
|
|
|
|
|
C<$value> as a hash with an C key selects an item by number |
|
947
|
|
|
|
|
|
|
(e.g. C<{n => 3> or C<{n => [2,4]}>). The numbering starts at 1. |
|
948
|
|
|
|
|
|
|
This applies to the current form (as set by the C> method or |
|
949
|
|
|
|
|
|
|
defaulting to the first form on the page). |
|
950
|
|
|
|
|
|
|
|
|
951
|
|
|
|
|
|
|
Returns 1 on successfully setting the value. On failure, returns |
|
952
|
|
|
|
|
|
|
undef and calls C<$self->warn()> with an error message. |
|
953
|
|
|
|
|
|
|
|
|
954
|
|
|
|
|
|
|
=cut |
|
955
|
|
|
|
|
|
|
|
|
956
|
|
|
|
|
|
|
sub select { |
|
957
|
|
|
|
|
|
|
my $self = shift; |
|
958
|
|
|
|
|
|
|
my( $name, $value ) = @_; |
|
959
|
|
|
|
|
|
|
|
|
960
|
|
|
|
|
|
|
my $form = $self->current_form; |
|
961
|
|
|
|
|
|
|
my $input = $form->find_input( $name, 'select' ); |
|
962
|
|
|
|
|
|
|
if ( !$input ) { |
|
963
|
|
|
|
|
|
|
$self->warn( "Select '$name' not found." ); |
|
964
|
|
|
|
|
|
|
return; |
|
965
|
|
|
|
|
|
|
} |
|
966
|
|
|
|
|
|
|
$input->select_value( $value ); |
|
967
|
|
|
|
|
|
|
return 1; |
|
968
|
|
|
|
|
|
|
} |
|
969
|
|
|
|
|
|
|
|
|
970
|
|
|
|
|
|
|
=head2 $moz->set_fields( %arguments ) |
|
971
|
|
|
|
|
|
|
|
|
972
|
|
|
|
|
|
|
This method sets multiple fields of a form. It takes a list of field |
|
973
|
|
|
|
|
|
|
name and value pairs. If there is more than one field with the same |
|
974
|
|
|
|
|
|
|
name, the first one found is set. If you want to select which of the |
|
975
|
|
|
|
|
|
|
duplicate field to set, use a value which is an anonymous array which |
|
976
|
|
|
|
|
|
|
has the field value and its number as the 2 elements. |
|
977
|
|
|
|
|
|
|
|
|
978
|
|
|
|
|
|
|
# set the second foo field |
|
979
|
|
|
|
|
|
|
$moz->set_fields( $name => [ 'foo', 2 ] ) ; |
|
980
|
|
|
|
|
|
|
|
|
981
|
|
|
|
|
|
|
The fields are numbered from 1. |
|
982
|
|
|
|
|
|
|
|
|
983
|
|
|
|
|
|
|
This applies to the current form (as set by the C> or |
|
984
|
|
|
|
|
|
|
C> method or defaulting to the first form on the |
|
985
|
|
|
|
|
|
|
page). |
|
986
|
|
|
|
|
|
|
|
|
987
|
|
|
|
|
|
|
=cut |
|
988
|
|
|
|
|
|
|
|
|
989
|
|
|
|
|
|
|
sub set_fields { |
|
990
|
|
|
|
|
|
|
my $self = shift; |
|
991
|
|
|
|
|
|
|
|
|
992
|
|
|
|
|
|
|
my $form = $self->current_form; |
|
993
|
|
|
|
|
|
|
my %opt = @_ ? UNIVERSAL::isa( $_[0], 'HASH' ) ? %{ $_[0] } : @_ : (); |
|
994
|
|
|
|
|
|
|
while ( my( $fname, $value ) = each %opt ) { |
|
995
|
|
|
|
|
|
|
if ( ref $value eq 'ARRAY' ) { |
|
996
|
|
|
|
|
|
|
my( $input ) = $form->find_input( $fname, undef, $value->[1] ); |
|
997
|
|
|
|
|
|
|
if ( $input ) { |
|
998
|
|
|
|
|
|
|
$input->value( $value->[0] ); |
|
999
|
|
|
|
|
|
|
} else { |
|
1000
|
|
|
|
|
|
|
$self->warn( "No inputcontrol by the name '$fname'" ); |
|
1001
|
|
|
|
|
|
|
} |
|
1002
|
|
|
|
|
|
|
} else { |
|
1003
|
|
|
|
|
|
|
my( $input ) = $form->find_input( $fname ); |
|
1004
|
|
|
|
|
|
|
if ( $input ) { |
|
1005
|
|
|
|
|
|
|
$input->value( $value ); |
|
1006
|
|
|
|
|
|
|
} else { |
|
1007
|
|
|
|
|
|
|
$self->warn( "No inputcontrol by the name '$fname'" ); |
|
1008
|
|
|
|
|
|
|
} |
|
1009
|
|
|
|
|
|
|
} |
|
1010
|
|
|
|
|
|
|
} |
|
1011
|
|
|
|
|
|
|
} |
|
1012
|
|
|
|
|
|
|
|
|
1013
|
|
|
|
|
|
|
=head2 $moz->set_visible( @criteria ) |
|
1014
|
|
|
|
|
|
|
|
|
1015
|
|
|
|
|
|
|
This method sets fields of a form without having to know their |
|
1016
|
|
|
|
|
|
|
names. So if you have a login screen that wants a username and |
|
1017
|
|
|
|
|
|
|
password, you do not have to fetch the form and inspect the source |
|
1018
|
|
|
|
|
|
|
to see what the field names are; you can just say |
|
1019
|
|
|
|
|
|
|
|
|
1020
|
|
|
|
|
|
|
$moz->set_visible( $username, $password ) ; |
|
1021
|
|
|
|
|
|
|
|
|
1022
|
|
|
|
|
|
|
and the first and second fields will be set accordingly. The method |
|
1023
|
|
|
|
|
|
|
is called set_I because it acts only on visible fields; |
|
1024
|
|
|
|
|
|
|
hidden form inputs are not considered. The order of the fields is |
|
1025
|
|
|
|
|
|
|
the order in which they appear in the HTML source which is nearly |
|
1026
|
|
|
|
|
|
|
always the order anyone viewing the page would think they are in, |
|
1027
|
|
|
|
|
|
|
but some creative work with tables could change that; caveat user. |
|
1028
|
|
|
|
|
|
|
|
|
1029
|
|
|
|
|
|
|
Each element in C<@criteria> is either a field value or a field |
|
1030
|
|
|
|
|
|
|
specifier. A field value is a scalar. A field specifier allows |
|
1031
|
|
|
|
|
|
|
you to specify the I of input field you want to set and is |
|
1032
|
|
|
|
|
|
|
denoted with an arrayref containing two elements. So you could |
|
1033
|
|
|
|
|
|
|
specify the first radio button with |
|
1034
|
|
|
|
|
|
|
|
|
1035
|
|
|
|
|
|
|
$moz->set_visible( [ radio => "KCRW" ] ) ; |
|
1036
|
|
|
|
|
|
|
|
|
1037
|
|
|
|
|
|
|
Field values and specifiers can be intermixed, hence |
|
1038
|
|
|
|
|
|
|
|
|
1039
|
|
|
|
|
|
|
$moz->set_visible( "fred", "secret", [ select => "Checking" ] ) ; |
|
1040
|
|
|
|
|
|
|
|
|
1041
|
|
|
|
|
|
|
would set the first two fields to "fred" and "secret", and the I |
|
1042
|
|
|
|
|
|
|
C |
|
1043
|
|
|
|
|
|
|
|
|
1044
|
|
|
|
|
|
|
The possible field specifier types are: "text", "password", "hidden", |
|
1045
|
|
|
|
|
|
|
"textarea", "file", "image", "submit", "radio", "checkbox" and "select". |
|
1046
|
|
|
|
|
|
|
|
|
1047
|
|
|
|
|
|
|
This method was ported from L. |
|
1048
|
|
|
|
|
|
|
|
|
1049
|
|
|
|
|
|
|
=cut |
|
1050
|
|
|
|
|
|
|
|
|
1051
|
|
|
|
|
|
|
sub set_visible { |
|
1052
|
|
|
|
|
|
|
my $self = shift; |
|
1053
|
|
|
|
|
|
|
|
|
1054
|
|
|
|
|
|
|
my $form = $self->current_form; |
|
1055
|
|
|
|
|
|
|
my @inputs = $form->inputs; |
|
1056
|
|
|
|
|
|
|
|
|
1057
|
|
|
|
|
|
|
while (my $value = shift) { |
|
1058
|
|
|
|
|
|
|
if (ref $value eq 'ARRAY') { |
|
1059
|
|
|
|
|
|
|
my ($type, $val) = @$value; |
|
1060
|
|
|
|
|
|
|
while (my $input = shift @inputs) { |
|
1061
|
|
|
|
|
|
|
next if $input->type eq 'hidden'; |
|
1062
|
|
|
|
|
|
|
if ($input->type eq $type) { |
|
1063
|
|
|
|
|
|
|
$input->value($val); |
|
1064
|
|
|
|
|
|
|
last; |
|
1065
|
|
|
|
|
|
|
} |
|
1066
|
|
|
|
|
|
|
} |
|
1067
|
|
|
|
|
|
|
} else { |
|
1068
|
|
|
|
|
|
|
while (my $input = shift @inputs) { |
|
1069
|
|
|
|
|
|
|
next if $input->type eq 'hidden'; |
|
1070
|
|
|
|
|
|
|
$input->value($value); |
|
1071
|
|
|
|
|
|
|
last; |
|
1072
|
|
|
|
|
|
|
} |
|
1073
|
|
|
|
|
|
|
} |
|
1074
|
|
|
|
|
|
|
} |
|
1075
|
|
|
|
|
|
|
} |
|
1076
|
|
|
|
|
|
|
|
|
1077
|
|
|
|
|
|
|
=head2 $moz->tick( $name, $value[, $set] ) |
|
1078
|
|
|
|
|
|
|
|
|
1079
|
|
|
|
|
|
|
'Ticks' the first checkbox that has both the name and value assoicated |
|
1080
|
|
|
|
|
|
|
with it on the current form. Dies if there is no named check box for |
|
1081
|
|
|
|
|
|
|
that value. Passing in a false value as the third optional argument |
|
1082
|
|
|
|
|
|
|
will cause the checkbox to be unticked. |
|
1083
|
|
|
|
|
|
|
|
|
1084
|
|
|
|
|
|
|
=cut |
|
1085
|
|
|
|
|
|
|
|
|
1086
|
|
|
|
|
|
|
sub tick { |
|
1087
|
|
|
|
|
|
|
my $self = shift; |
|
1088
|
|
|
|
|
|
|
|
|
1089
|
|
|
|
|
|
|
my $form = $self->current_form; |
|
1090
|
|
|
|
|
|
|
|
|
1091
|
|
|
|
|
|
|
my( $name, $value, $set ) = @_; |
|
1092
|
|
|
|
|
|
|
$set = 1 if @_ <= 2; |
|
1093
|
|
|
|
|
|
|
my @check_boxes = grep $_->value eq $value |
|
1094
|
|
|
|
|
|
|
=> $form->find_input( $name, 'checkbox' ); |
|
1095
|
|
|
|
|
|
|
|
|
1096
|
|
|
|
|
|
|
$self->warn( "No checkbox '$name' for value '$value' in form." ), return |
|
1097
|
|
|
|
|
|
|
unless @check_boxes; |
|
1098
|
|
|
|
|
|
|
|
|
1099
|
|
|
|
|
|
|
foreach my $check_box ( @check_boxes ) { |
|
1100
|
|
|
|
|
|
|
next unless $check_box->value eq $value; |
|
1101
|
|
|
|
|
|
|
# XXX: breaks encapsulation |
|
1102
|
|
|
|
|
|
|
$check_box->{input}->SetChecked($set || 0); |
|
1103
|
|
|
|
|
|
|
} |
|
1104
|
|
|
|
|
|
|
return 1; |
|
1105
|
|
|
|
|
|
|
} |
|
1106
|
|
|
|
|
|
|
|
|
1107
|
|
|
|
|
|
|
=head2 $moz->untick( $name, $value ) |
|
1108
|
|
|
|
|
|
|
|
|
1109
|
|
|
|
|
|
|
Causes the checkbox to be unticked. Shorthand for |
|
1110
|
|
|
|
|
|
|
C |
|
1111
|
|
|
|
|
|
|
|
|
1112
|
|
|
|
|
|
|
=cut |
|
1113
|
|
|
|
|
|
|
|
|
1114
|
|
|
|
|
|
|
sub untick { |
|
1115
|
|
|
|
|
|
|
my $self = shift; |
|
1116
|
|
|
|
|
|
|
$self->tick( @_[0, 1], undef ); |
|
1117
|
|
|
|
|
|
|
} |
|
1118
|
|
|
|
|
|
|
|
|
1119
|
|
|
|
|
|
|
=head2 $mech->value( $name, $number ) |
|
1120
|
|
|
|
|
|
|
|
|
1121
|
|
|
|
|
|
|
Given the name of a field, return its value. This applies to the current |
|
1122
|
|
|
|
|
|
|
form (as set by the C |
|
1123
|
|
|
|
|
|
|
the page). |
|
1124
|
|
|
|
|
|
|
|
|
1125
|
|
|
|
|
|
|
The option I<$number> parameter is used to distinguish between two fields |
|
1126
|
|
|
|
|
|
|
with the same name. The fields are numbered from 1. |
|
1127
|
|
|
|
|
|
|
|
|
1128
|
|
|
|
|
|
|
If the field is of type file (file upload field), the value is always |
|
1129
|
|
|
|
|
|
|
cleared to prevent remote sites from downloading your local files. |
|
1130
|
|
|
|
|
|
|
To upload a file, specify its file name explicitly. |
|
1131
|
|
|
|
|
|
|
|
|
1132
|
|
|
|
|
|
|
=cut |
|
1133
|
|
|
|
|
|
|
|
|
1134
|
|
|
|
|
|
|
sub value { |
|
1135
|
|
|
|
|
|
|
my $self = shift; |
|
1136
|
|
|
|
|
|
|
my $name = shift; |
|
1137
|
|
|
|
|
|
|
my $number = shift || 1; |
|
1138
|
|
|
|
|
|
|
|
|
1139
|
|
|
|
|
|
|
my $form = $self->current_form; |
|
1140
|
|
|
|
|
|
|
if ( wantarray ) { |
|
1141
|
|
|
|
|
|
|
my @inputs = $form->find_input( $name ); |
|
1142
|
|
|
|
|
|
|
return @inputs ? map $_->value() => @inputs : undef; |
|
1143
|
|
|
|
|
|
|
} |
|
1144
|
|
|
|
|
|
|
if ( $number > 1 ) { |
|
1145
|
|
|
|
|
|
|
return $form->find_input( $name, undef, $number )->value(); |
|
1146
|
|
|
|
|
|
|
} else { |
|
1147
|
|
|
|
|
|
|
return $form->value( $name ); |
|
1148
|
|
|
|
|
|
|
} |
|
1149
|
|
|
|
|
|
|
} |
|
1150
|
|
|
|
|
|
|
|
|
1151
|
|
|
|
|
|
|
=head2 $moz->click( $button ) |
|
1152
|
|
|
|
|
|
|
|
|
1153
|
|
|
|
|
|
|
Call the click method on an INPUT object with the name C<$button>. Has |
|
1154
|
|
|
|
|
|
|
the effect of clicking a button on a form. The first argument is the |
|
1155
|
|
|
|
|
|
|
name of the button to be clicked. I have not found a way to set the |
|
1156
|
|
|
|
|
|
|
(x,y) coordinates of the click. |
|
1157
|
|
|
|
|
|
|
|
|
1158
|
|
|
|
|
|
|
Note: inputs are searched in the order: buttons, images, submits. |
|
1159
|
|
|
|
|
|
|
|
|
1160
|
|
|
|
|
|
|
=cut |
|
1161
|
|
|
|
|
|
|
|
|
1162
|
|
|
|
|
|
|
sub click { |
|
1163
|
|
|
|
|
|
|
my ($self, $button) = @_; |
|
1164
|
|
|
|
|
|
|
|
|
1165
|
|
|
|
|
|
|
my $form = $self->current_form; |
|
1166
|
|
|
|
|
|
|
|
|
1167
|
|
|
|
|
|
|
# XXX: this is unsorted |
|
1168
|
|
|
|
|
|
|
my ($toclick) = ( |
|
1169
|
|
|
|
|
|
|
$form->find_input( $button, 'button' ), |
|
1170
|
|
|
|
|
|
|
$form->find_input( $button, 'image' ), |
|
1171
|
|
|
|
|
|
|
$form->find_input( $button, 'submit' ), |
|
1172
|
|
|
|
|
|
|
); |
|
1173
|
|
|
|
|
|
|
|
|
1174
|
|
|
|
|
|
|
$toclick and $toclick->click(); |
|
1175
|
|
|
|
|
|
|
} |
|
1176
|
|
|
|
|
|
|
|
|
1177
|
|
|
|
|
|
|
=head2 $moz->click_button( %args ) |
|
1178
|
|
|
|
|
|
|
|
|
1179
|
|
|
|
|
|
|
Has the effect of clicking a button on a form by specifying its name, |
|
1180
|
|
|
|
|
|
|
value, or index. Its arguments are a list of key/value pairs. Only |
|
1181
|
|
|
|
|
|
|
one of name, number, or value must be specified. |
|
1182
|
|
|
|
|
|
|
|
|
1183
|
|
|
|
|
|
|
=over 4 |
|
1184
|
|
|
|
|
|
|
|
|
1185
|
|
|
|
|
|
|
=item * name => name |
|
1186
|
|
|
|
|
|
|
|
|
1187
|
|
|
|
|
|
|
Clicks the button named I. |
|
1188
|
|
|
|
|
|
|
|
|
1189
|
|
|
|
|
|
|
=item * number => n |
|
1190
|
|
|
|
|
|
|
|
|
1191
|
|
|
|
|
|
|
Clicks the Ith button in the form. |
|
1192
|
|
|
|
|
|
|
B It will select the Nth button |
|
1193
|
|
|
|
|
|
|
if you assume they're gotten in the order: button, image, submit. |
|
1194
|
|
|
|
|
|
|
This will presumably get fixed sometime, so I'd advise not relying on it. |
|
1195
|
|
|
|
|
|
|
|
|
1196
|
|
|
|
|
|
|
=item * value => value |
|
1197
|
|
|
|
|
|
|
|
|
1198
|
|
|
|
|
|
|
Clicks the button with the value I. |
|
1199
|
|
|
|
|
|
|
|
|
1200
|
|
|
|
|
|
|
=back |
|
1201
|
|
|
|
|
|
|
|
|
1202
|
|
|
|
|
|
|
B: Unlike WWW::Mechanize, Mozilla::Mechanize takes |
|
1203
|
|
|
|
|
|
|
all buttonish types of C<< >> into account: B |
|
1204
|
|
|
|
|
|
|
B, and B. |
|
1205
|
|
|
|
|
|
|
|
|
1206
|
|
|
|
|
|
|
=cut |
|
1207
|
|
|
|
|
|
|
|
|
1208
|
|
|
|
|
|
|
sub click_button { |
|
1209
|
|
|
|
|
|
|
my $self = shift; |
|
1210
|
|
|
|
|
|
|
my %args = @_; |
|
1211
|
|
|
|
|
|
|
|
|
1212
|
|
|
|
|
|
|
for ( keys %args ) { |
|
1213
|
|
|
|
|
|
|
if ( !/^(number|name|value)$/ ) { |
|
1214
|
|
|
|
|
|
|
$self->warn( qq{Unknown click_button_form parameter "$_"} ); |
|
1215
|
|
|
|
|
|
|
} |
|
1216
|
|
|
|
|
|
|
} |
|
1217
|
|
|
|
|
|
|
|
|
1218
|
|
|
|
|
|
|
my $form = $self->current_form; |
|
1219
|
|
|
|
|
|
|
# XXX: this is unsorted |
|
1220
|
|
|
|
|
|
|
my @buttons = ( |
|
1221
|
|
|
|
|
|
|
$form->find_input( $args{name}, 'button' ), |
|
1222
|
|
|
|
|
|
|
$form->find_input( $args{name}, 'image' ), |
|
1223
|
|
|
|
|
|
|
$form->find_input( $args{name}, 'submit' ), |
|
1224
|
|
|
|
|
|
|
); |
|
1225
|
|
|
|
|
|
|
|
|
1226
|
|
|
|
|
|
|
@buttons or return; |
|
1227
|
|
|
|
|
|
|
if ( $args{name} ) { |
|
1228
|
|
|
|
|
|
|
$buttons[0]->click(); |
|
1229
|
|
|
|
|
|
|
return 1; |
|
1230
|
|
|
|
|
|
|
} elsif ( $args{number} ) { |
|
1231
|
|
|
|
|
|
|
@buttons <= $args{number} and return |
|
1232
|
|
|
|
|
|
|
$buttons[ $args{number} - 1 ]->click(); |
|
1233
|
|
|
|
|
|
|
return 1; |
|
1234
|
|
|
|
|
|
|
} elsif ( $args{value} ) { |
|
1235
|
|
|
|
|
|
|
for my $button ( @buttons ) { |
|
1236
|
|
|
|
|
|
|
if ( $button->value eq $args{value} ) { |
|
1237
|
|
|
|
|
|
|
$button->click(); |
|
1238
|
|
|
|
|
|
|
return 1; |
|
1239
|
|
|
|
|
|
|
} |
|
1240
|
|
|
|
|
|
|
} |
|
1241
|
|
|
|
|
|
|
} |
|
1242
|
|
|
|
|
|
|
} |
|
1243
|
|
|
|
|
|
|
|
|
1244
|
|
|
|
|
|
|
=head2 $moz->submit( ) |
|
1245
|
|
|
|
|
|
|
|
|
1246
|
|
|
|
|
|
|
Submits the page, without specifying a button to click. Actually, |
|
1247
|
|
|
|
|
|
|
no button is clicked at all. |
|
1248
|
|
|
|
|
|
|
|
|
1249
|
|
|
|
|
|
|
This will call the C method of the currently selected form. |
|
1250
|
|
|
|
|
|
|
|
|
1251
|
|
|
|
|
|
|
B: It looks like this method does not call the C |
|
1252
|
|
|
|
|
|
|
handler specified in the C<< |
|
1253
|
|
|
|
|
|
|
you call the C method with submit button. |
|
1254
|
|
|
|
|
|
|
|
|
1255
|
|
|
|
|
|
|
=cut |
|
1256
|
|
|
|
|
|
|
|
|
1257
|
|
|
|
|
|
|
sub submit { |
|
1258
|
|
|
|
|
|
|
my $self = shift; |
|
1259
|
|
|
|
|
|
|
|
|
1260
|
|
|
|
|
|
|
my $form = $self->current_form; |
|
1261
|
|
|
|
|
|
|
|
|
1262
|
|
|
|
|
|
|
$form->submit(); |
|
1263
|
|
|
|
|
|
|
} |
|
1264
|
|
|
|
|
|
|
|
|
1265
|
|
|
|
|
|
|
=head2 $moz->submit_form( %opt ) |
|
1266
|
|
|
|
|
|
|
|
|
1267
|
|
|
|
|
|
|
This method lets you select a form from the previously fetched page, |
|
1268
|
|
|
|
|
|
|
fill in its fields, and submit it. It combines the form_number/form_name, |
|
1269
|
|
|
|
|
|
|
set_fields and click methods into one higher level call. Its arguments |
|
1270
|
|
|
|
|
|
|
are a list of key/value pairs, all of which are optional. |
|
1271
|
|
|
|
|
|
|
|
|
1272
|
|
|
|
|
|
|
=over 4 |
|
1273
|
|
|
|
|
|
|
|
|
1274
|
|
|
|
|
|
|
=item * form_number => n |
|
1275
|
|
|
|
|
|
|
|
|
1276
|
|
|
|
|
|
|
Selects the Ith form (calls C>). If this parm is not |
|
1277
|
|
|
|
|
|
|
specified, the currently-selected form is used. |
|
1278
|
|
|
|
|
|
|
|
|
1279
|
|
|
|
|
|
|
=item * form_name => name |
|
1280
|
|
|
|
|
|
|
|
|
1281
|
|
|
|
|
|
|
Selects the form named I (calls C>) |
|
1282
|
|
|
|
|
|
|
|
|
1283
|
|
|
|
|
|
|
=item * fields => fields |
|
1284
|
|
|
|
|
|
|
|
|
1285
|
|
|
|
|
|
|
Sets the field values from the I hashref (calls C>) |
|
1286
|
|
|
|
|
|
|
|
|
1287
|
|
|
|
|
|
|
=item * button => button |
|
1288
|
|
|
|
|
|
|
|
|
1289
|
|
|
|
|
|
|
Clicks on button I |
|
1290
|
|
|
|
|
|
|
|
|
1291
|
|
|
|
|
|
|
=item * button => { value => value } |
|
1292
|
|
|
|
|
|
|
|
|
1293
|
|
|
|
|
|
|
When you specify a hash_ref for button it calls C |
|
1294
|
|
|
|
|
|
|
|
|
1295
|
|
|
|
|
|
|
=back |
|
1296
|
|
|
|
|
|
|
|
|
1297
|
|
|
|
|
|
|
If no form is selected, the first form found is used. |
|
1298
|
|
|
|
|
|
|
|
|
1299
|
|
|
|
|
|
|
If I |
|
1300
|
|
|
|
|
|
|
|
|
1301
|
|
|
|
|
|
|
Returns true on success. |
|
1302
|
|
|
|
|
|
|
|
|
1303
|
|
|
|
|
|
|
=cut |
|
1304
|
|
|
|
|
|
|
|
|
1305
|
|
|
|
|
|
|
sub submit_form { |
|
1306
|
|
|
|
|
|
|
my $self = shift; |
|
1307
|
|
|
|
|
|
|
|
|
1308
|
|
|
|
|
|
|
my %opt = @_; |
|
1309
|
|
|
|
|
|
|
if ( my $form_number = $opt{form_number} ) { |
|
1310
|
|
|
|
|
|
|
$self->form_number( $form_number ) ; |
|
1311
|
|
|
|
|
|
|
} |
|
1312
|
|
|
|
|
|
|
elsif ( my $form_name = $opt{form_name} ) { |
|
1313
|
|
|
|
|
|
|
$self->form_name( $form_name ) ; |
|
1314
|
|
|
|
|
|
|
} else { |
|
1315
|
|
|
|
|
|
|
$self->form_number( 1 ) unless defined $self->{cur_form}; |
|
1316
|
|
|
|
|
|
|
} |
|
1317
|
|
|
|
|
|
|
|
|
1318
|
|
|
|
|
|
|
if ( my $fields = $opt{fields} ) { |
|
1319
|
|
|
|
|
|
|
if ( ref $fields eq 'HASH' ) { |
|
1320
|
|
|
|
|
|
|
$self->set_fields( %{$fields} ) ; |
|
1321
|
|
|
|
|
|
|
} # TODO: What if it's not a hash? We just ignore it silently? |
|
1322
|
|
|
|
|
|
|
} |
|
1323
|
|
|
|
|
|
|
|
|
1324
|
|
|
|
|
|
|
if ( $opt{button} ) { |
|
1325
|
|
|
|
|
|
|
if ( ref $opt{button} ) { |
|
1326
|
|
|
|
|
|
|
$self->click_button( %{ $opt{button} } ); |
|
1327
|
|
|
|
|
|
|
} else { |
|
1328
|
|
|
|
|
|
|
$self->click( $opt{button} ); |
|
1329
|
|
|
|
|
|
|
} |
|
1330
|
|
|
|
|
|
|
} else { |
|
1331
|
|
|
|
|
|
|
$self->submit(); |
|
1332
|
|
|
|
|
|
|
} |
|
1333
|
|
|
|
|
|
|
|
|
1334
|
|
|
|
|
|
|
return $self->success; |
|
1335
|
|
|
|
|
|
|
} |
|
1336
|
|
|
|
|
|
|
|
|
1337
|
|
|
|
|
|
|
=head1 MISCELANEOUS METHODS |
|
1338
|
|
|
|
|
|
|
|
|
1339
|
|
|
|
|
|
|
B |
|
1340
|
|
|
|
|
|
|
|
|
1341
|
|
|
|
|
|
|
=head2 $moz->add_header( name => $value [, name => $value... ] ) |
|
1342
|
|
|
|
|
|
|
|
|
1343
|
|
|
|
|
|
|
Sets HTTP headers for the agent to add or remove from the HTTP request. |
|
1344
|
|
|
|
|
|
|
|
|
1345
|
|
|
|
|
|
|
$moz->add_header( Encoding => 'text/klingon' ); |
|
1346
|
|
|
|
|
|
|
|
|
1347
|
|
|
|
|
|
|
If a I is C, then that header will be removed from any |
|
1348
|
|
|
|
|
|
|
future requests. For example, to never send a Referer header: |
|
1349
|
|
|
|
|
|
|
|
|
1350
|
|
|
|
|
|
|
$moz->add_header( Referer => undef ); |
|
1351
|
|
|
|
|
|
|
|
|
1352
|
|
|
|
|
|
|
Returns the number of name/value pairs added. |
|
1353
|
|
|
|
|
|
|
|
|
1354
|
|
|
|
|
|
|
=cut |
|
1355
|
|
|
|
|
|
|
|
|
1356
|
|
|
|
|
|
|
sub add_header { |
|
1357
|
|
|
|
|
|
|
my $self = shift; |
|
1358
|
|
|
|
|
|
|
my $npairs = 0; |
|
1359
|
|
|
|
|
|
|
|
|
1360
|
|
|
|
|
|
|
while ( @_ ) { |
|
1361
|
|
|
|
|
|
|
my $key = shift; |
|
1362
|
|
|
|
|
|
|
my $value = shift; |
|
1363
|
|
|
|
|
|
|
++$npairs; |
|
1364
|
|
|
|
|
|
|
|
|
1365
|
|
|
|
|
|
|
$self->{headers}{$key} = $value; |
|
1366
|
|
|
|
|
|
|
} |
|
1367
|
|
|
|
|
|
|
|
|
1368
|
|
|
|
|
|
|
return $npairs; |
|
1369
|
|
|
|
|
|
|
} |
|
1370
|
|
|
|
|
|
|
|
|
1371
|
|
|
|
|
|
|
=head2 $moz->delete_header( name [, name ... ] ) |
|
1372
|
|
|
|
|
|
|
|
|
1373
|
|
|
|
|
|
|
B |
|
1374
|
|
|
|
|
|
|
|
|
1375
|
|
|
|
|
|
|
Removes HTTP headers from the agent's list of special headers. |
|
1376
|
|
|
|
|
|
|
|
|
1377
|
|
|
|
|
|
|
B: This might not work like it does with C |
|
1378
|
|
|
|
|
|
|
|
|
1379
|
|
|
|
|
|
|
=cut |
|
1380
|
|
|
|
|
|
|
|
|
1381
|
|
|
|
|
|
|
sub delete_header { |
|
1382
|
|
|
|
|
|
|
my $self = shift; |
|
1383
|
|
|
|
|
|
|
|
|
1384
|
|
|
|
|
|
|
while ( @_ ) { |
|
1385
|
|
|
|
|
|
|
my $key = shift; |
|
1386
|
|
|
|
|
|
|
delete $self->{headers}{$key}; |
|
1387
|
|
|
|
|
|
|
} |
|
1388
|
|
|
|
|
|
|
} |
|
1389
|
|
|
|
|
|
|
|
|
1390
|
|
|
|
|
|
|
=head2 $moz->quiet( [$state] ) |
|
1391
|
|
|
|
|
|
|
|
|
1392
|
|
|
|
|
|
|
Allows you to suppress warnings to the screen. |
|
1393
|
|
|
|
|
|
|
|
|
1394
|
|
|
|
|
|
|
$a->quiet(0); # turns on warnings (the default) |
|
1395
|
|
|
|
|
|
|
$a->quiet(1); # turns off warnings |
|
1396
|
|
|
|
|
|
|
$a->quiet(); # returns the current quietness status |
|
1397
|
|
|
|
|
|
|
|
|
1398
|
|
|
|
|
|
|
=cut |
|
1399
|
|
|
|
|
|
|
|
|
1400
|
|
|
|
|
|
|
sub quiet { |
|
1401
|
|
|
|
|
|
|
my $self = shift; |
|
1402
|
|
|
|
|
|
|
|
|
1403
|
|
|
|
|
|
|
$self->{quiet} = $_[0] if @_; |
|
1404
|
|
|
|
|
|
|
|
|
1405
|
|
|
|
|
|
|
return $self->{quiet}; |
|
1406
|
|
|
|
|
|
|
} |
|
1407
|
|
|
|
|
|
|
|
|
1408
|
|
|
|
|
|
|
=head2 $moz->find_frame( $frame_name ) |
|
1409
|
|
|
|
|
|
|
|
|
1410
|
|
|
|
|
|
|
Returns the URL to the source of the frame with C |
|
1411
|
|
|
|
|
|
|
|
|
1412
|
|
|
|
|
|
|
=cut |
|
1413
|
|
|
|
|
|
|
|
|
1414
|
|
|
|
|
|
|
sub find_frame { |
|
1415
|
|
|
|
|
|
|
my ($self, $frame) = @_; |
|
1416
|
|
|
|
|
|
|
|
|
1417
|
|
|
|
|
|
|
# XXX: there must be an easier way than this...? |
|
1418
|
|
|
|
|
|
|
my $docelem = $self->get_document_element; |
|
1419
|
|
|
|
|
|
|
my $framelist = $docelem->GetElementsByTagName('frame'); |
|
1420
|
|
|
|
|
|
|
for (my $i = 0; $i < $framelist->GetLength; $i++) { |
|
1421
|
|
|
|
|
|
|
my $frame_elem = $framelist->Item($i); |
|
1422
|
|
|
|
|
|
|
if ($frame_elem->HasAttributes) { |
|
1423
|
|
|
|
|
|
|
my $attrs = $frame_elem->GetAttributes; |
|
1424
|
|
|
|
|
|
|
my %attrs = (); |
|
1425
|
|
|
|
|
|
|
for (my $j = 0; $j < $attrs->GetLength; $j++) { |
|
1426
|
|
|
|
|
|
|
my $attr = $attrs->Item($j); |
|
1427
|
|
|
|
|
|
|
$attrs{lc($attr->GetNodeName)} = $attr->GetNodeValue; |
|
1428
|
|
|
|
|
|
|
} |
|
1429
|
|
|
|
|
|
|
|
|
1430
|
|
|
|
|
|
|
if ($attrs{'name'} eq $frame) { |
|
1431
|
|
|
|
|
|
|
return (URI->new_abs($attrs{'src'}, $self->uri))->as_string; |
|
1432
|
|
|
|
|
|
|
} |
|
1433
|
|
|
|
|
|
|
} |
|
1434
|
|
|
|
|
|
|
} |
|
1435
|
|
|
|
|
|
|
|
|
1436
|
|
|
|
|
|
|
return; |
|
1437
|
|
|
|
|
|
|
} |
|
1438
|
|
|
|
|
|
|
|
|
1439
|
|
|
|
|
|
|
=head2 $moz->load_frame( $frame_name ) |
|
1440
|
|
|
|
|
|
|
|
|
1441
|
|
|
|
|
|
|
C<< $self->get( $self->find_frame( $frame_name )) >> |
|
1442
|
|
|
|
|
|
|
|
|
1443
|
|
|
|
|
|
|
=cut |
|
1444
|
|
|
|
|
|
|
|
|
1445
|
|
|
|
|
|
|
sub load_frame { |
|
1446
|
|
|
|
|
|
|
my( $self, $frame ) = @_; |
|
1447
|
|
|
|
|
|
|
$self->get( $self->find_frame($frame) ); |
|
1448
|
|
|
|
|
|
|
} |
|
1449
|
|
|
|
|
|
|
|
|
1450
|
|
|
|
|
|
|
=head1 DOM CONVENIENCE METHODS |
|
1451
|
|
|
|
|
|
|
|
|
1452
|
|
|
|
|
|
|
=cut |
|
1453
|
|
|
|
|
|
|
|
|
1454
|
|
|
|
|
|
|
=head2 $moz->get_window; |
|
1455
|
|
|
|
|
|
|
|
|
1456
|
|
|
|
|
|
|
Convenience method to get the Window |
|
1457
|
|
|
|
|
|
|
(L). |
|
1458
|
|
|
|
|
|
|
(This is the `window' browser object in JavaScript.) |
|
1459
|
|
|
|
|
|
|
|
|
1460
|
|
|
|
|
|
|
=cut |
|
1461
|
|
|
|
|
|
|
|
|
1462
|
|
|
|
|
|
|
sub get_window { |
|
1463
|
|
|
|
|
|
|
my $self = shift; |
|
1464
|
|
|
|
|
|
|
my $embed = $self->agent->embedded; |
|
1465
|
|
|
|
|
|
|
my $browser = $embed->get_nsIWebBrowser; |
|
1466
|
|
|
|
|
|
|
return $browser->GetContentDOMWindow; |
|
1467
|
|
|
|
|
|
|
} |
|
1468
|
|
|
|
|
|
|
|
|
1469
|
|
|
|
|
|
|
=head2 $moz->get_document; |
|
1470
|
|
|
|
|
|
|
|
|
1471
|
|
|
|
|
|
|
Convenience method to get the Document |
|
1472
|
|
|
|
|
|
|
(L). |
|
1473
|
|
|
|
|
|
|
(This is the `document' browser object in JavaScript.) |
|
1474
|
|
|
|
|
|
|
|
|
1475
|
|
|
|
|
|
|
=cut |
|
1476
|
|
|
|
|
|
|
|
|
1477
|
|
|
|
|
|
|
sub get_document { |
|
1478
|
|
|
|
|
|
|
my $self = shift; |
|
1479
|
|
|
|
|
|
|
my $window = $self->get_window; |
|
1480
|
|
|
|
|
|
|
return $window->GetDocument; |
|
1481
|
|
|
|
|
|
|
} |
|
1482
|
|
|
|
|
|
|
|
|
1483
|
|
|
|
|
|
|
=head2 $moz->get_document_element; |
|
1484
|
|
|
|
|
|
|
|
|
1485
|
|
|
|
|
|
|
Convenience method to get the document element |
|
1486
|
|
|
|
|
|
|
(L). |
|
1487
|
|
|
|
|
|
|
(Actually this is a L, |
|
1488
|
|
|
|
|
|
|
i.e. , if you want to QueryInterface to it). |
|
1489
|
|
|
|
|
|
|
This is useful for calling GetElementsByTagName. |
|
1490
|
|
|
|
|
|
|
|
|
1491
|
|
|
|
|
|
|
=cut |
|
1492
|
|
|
|
|
|
|
|
|
1493
|
|
|
|
|
|
|
sub get_document_element { |
|
1494
|
|
|
|
|
|
|
my $self = shift; |
|
1495
|
|
|
|
|
|
|
my $doc = $self->get_document; |
|
1496
|
|
|
|
|
|
|
return $doc->GetDocumentElement; |
|
1497
|
|
|
|
|
|
|
} |
|
1498
|
|
|
|
|
|
|
|
|
1499
|
|
|
|
|
|
|
=head1 LWP COMPATABILITY METHODS |
|
1500
|
|
|
|
|
|
|
|
|
1501
|
|
|
|
|
|
|
=head2 $moz->credentials( $netloc, $realm, $uid, $pass ) |
|
1502
|
|
|
|
|
|
|
|
|
1503
|
|
|
|
|
|
|
B |
|
1504
|
|
|
|
|
|
|
|
|
1505
|
|
|
|
|
|
|
Set the user name and password to be used for a realm. |
|
1506
|
|
|
|
|
|
|
|
|
1507
|
|
|
|
|
|
|
C<$netloc> looks like C. |
|
1508
|
|
|
|
|
|
|
|
|
1509
|
|
|
|
|
|
|
=cut |
|
1510
|
|
|
|
|
|
|
|
|
1511
|
|
|
|
|
|
|
sub credentials { |
|
1512
|
|
|
|
|
|
|
my($self, $netloc, $realm, $uid, $pass) = @_; |
|
1513
|
|
|
|
|
|
|
$self->{basic_authentication}{ $netloc }{ $realm } = [ $uid, $pass ]; |
|
1514
|
|
|
|
|
|
|
$self->{basic_authentication}{ $netloc }{__active_realm__} = $realm; |
|
1515
|
|
|
|
|
|
|
} |
|
1516
|
|
|
|
|
|
|
|
|
1517
|
|
|
|
|
|
|
=head2 $moz->get_basic_credentials( $realm, $uri ) |
|
1518
|
|
|
|
|
|
|
|
|
1519
|
|
|
|
|
|
|
B |
|
1520
|
|
|
|
|
|
|
|
|
1521
|
|
|
|
|
|
|
This is called by C<_extra_headers> to retrieve credentials for documents |
|
1522
|
|
|
|
|
|
|
protected by Basic Authentication. The arguments passed in |
|
1523
|
|
|
|
|
|
|
are the C<$realm> and the C<$uri> requested. |
|
1524
|
|
|
|
|
|
|
|
|
1525
|
|
|
|
|
|
|
The method should return a username and password. It should return an |
|
1526
|
|
|
|
|
|
|
empty list to abort the authentication resolution attempt. |
|
1527
|
|
|
|
|
|
|
|
|
1528
|
|
|
|
|
|
|
The base implementation simply checks a set of pre-stored member |
|
1529
|
|
|
|
|
|
|
variables, set up with the credentials() method. |
|
1530
|
|
|
|
|
|
|
|
|
1531
|
|
|
|
|
|
|
=cut |
|
1532
|
|
|
|
|
|
|
|
|
1533
|
|
|
|
|
|
|
sub get_basic_credentials { |
|
1534
|
|
|
|
|
|
|
my($self, $realm, $uri ) = @_; |
|
1535
|
|
|
|
|
|
|
|
|
1536
|
|
|
|
|
|
|
my $host_port = $uri->can( 'host_port' ) |
|
1537
|
|
|
|
|
|
|
? $uri->host_port : $uri->as_string; |
|
1538
|
|
|
|
|
|
|
|
|
1539
|
|
|
|
|
|
|
$realm ||= $self->{basic_authentication}{ $host_port }{__active_realm__}; |
|
1540
|
|
|
|
|
|
|
$realm or return ( ); |
|
1541
|
|
|
|
|
|
|
|
|
1542
|
|
|
|
|
|
|
if ( exists $self->{basic_authentication}{ $host_port }{ $realm } ) { |
|
1543
|
|
|
|
|
|
|
return @{ $self->{basic_authentication}{ $host_port }{ $realm } }; |
|
1544
|
|
|
|
|
|
|
} |
|
1545
|
|
|
|
|
|
|
|
|
1546
|
|
|
|
|
|
|
return ( ); |
|
1547
|
|
|
|
|
|
|
} |
|
1548
|
|
|
|
|
|
|
|
|
1549
|
|
|
|
|
|
|
=head2 $moz->set_realm( $netloc, $realm ); |
|
1550
|
|
|
|
|
|
|
|
|
1551
|
|
|
|
|
|
|
B |
|
1552
|
|
|
|
|
|
|
|
|
1553
|
|
|
|
|
|
|
Sets the authentication realm to C<$realm> for C<$netloc>. An empty value |
|
1554
|
|
|
|
|
|
|
unsets the realm for C<$netloc>. |
|
1555
|
|
|
|
|
|
|
|
|
1556
|
|
|
|
|
|
|
C<$netloc> looks like C. |
|
1557
|
|
|
|
|
|
|
|
|
1558
|
|
|
|
|
|
|
As I have not found a way to access response-headers, I cannot find |
|
1559
|
|
|
|
|
|
|
out the authentication realm (if any) and automagically set the right |
|
1560
|
|
|
|
|
|
|
headers. You will have to do some bookkeeping for now. |
|
1561
|
|
|
|
|
|
|
|
|
1562
|
|
|
|
|
|
|
=cut |
|
1563
|
|
|
|
|
|
|
|
|
1564
|
|
|
|
|
|
|
sub set_realm { |
|
1565
|
|
|
|
|
|
|
my( $self, $netloc, $realm ) = @_; |
|
1566
|
|
|
|
|
|
|
$netloc or return; |
|
1567
|
|
|
|
|
|
|
defined $realm or $realm = ""; |
|
1568
|
|
|
|
|
|
|
$self->{basic_authentication}{ $netloc }{__active_realm__} = $realm; |
|
1569
|
|
|
|
|
|
|
} |
|
1570
|
|
|
|
|
|
|
|
|
1571
|
|
|
|
|
|
|
=head1 INTERNAL-ONLY METHODS |
|
1572
|
|
|
|
|
|
|
|
|
1573
|
|
|
|
|
|
|
=head2 DESTROY |
|
1574
|
|
|
|
|
|
|
|
|
1575
|
|
|
|
|
|
|
Close the browser. |
|
1576
|
|
|
|
|
|
|
|
|
1577
|
|
|
|
|
|
|
=cut |
|
1578
|
|
|
|
|
|
|
|
|
1579
|
|
|
|
|
|
|
sub DESTROY { |
|
1580
|
|
|
|
|
|
|
my $agent = shift->agent; |
|
1581
|
|
|
|
|
|
|
$agent && $agent->quit; |
|
1582
|
|
|
|
|
|
|
} |
|
1583
|
|
|
|
|
|
|
|
|
1584
|
|
|
|
|
|
|
|
|
1585
|
|
|
|
|
|
|
=head2 $moz->_extract_forms() |
|
1586
|
|
|
|
|
|
|
|
|
1587
|
|
|
|
|
|
|
Return a list of forms. All forms are mapped onto the |
|
1588
|
|
|
|
|
|
|
L interface |
|
1589
|
|
|
|
|
|
|
that mimics L. |
|
1590
|
|
|
|
|
|
|
|
|
1591
|
|
|
|
|
|
|
=cut |
|
1592
|
|
|
|
|
|
|
|
|
1593
|
|
|
|
|
|
|
sub _extract_forms { |
|
1594
|
|
|
|
|
|
|
my $self = shift; |
|
1595
|
|
|
|
|
|
|
my @forms; |
|
1596
|
|
|
|
|
|
|
|
|
1597
|
|
|
|
|
|
|
my $docelem = $self->get_document_element; |
|
1598
|
|
|
|
|
|
|
my $formlist = $docelem->GetElementsByTagName('form'); |
|
1599
|
|
|
|
|
|
|
for (my $i = 0; $i < $formlist->GetLength; $i++) { |
|
1600
|
|
|
|
|
|
|
push @forms, Mozilla::Mechanize::Form->new($formlist->Item($i), $self); |
|
1601
|
|
|
|
|
|
|
} |
|
1602
|
|
|
|
|
|
|
$self->{forms} = \@forms; |
|
1603
|
|
|
|
|
|
|
|
|
1604
|
|
|
|
|
|
|
return wantarray ? @{ $self->{forms} } : $self->{forms}; |
|
1605
|
|
|
|
|
|
|
} |
|
1606
|
|
|
|
|
|
|
|
|
1607
|
|
|
|
|
|
|
=head2 $self->_extract_links() |
|
1608
|
|
|
|
|
|
|
|
|
1609
|
|
|
|
|
|
|
The links come from the following: |
|
1610
|
|
|
|
|
|
|
|
|
1611
|
|
|
|
|
|
|
=over 2 |
|
1612
|
|
|
|
|
|
|
|
|
1613
|
|
|
|
|
|
|
=item "" |
|
1614
|
|
|
|
|
|
|
|
|
1615
|
|
|
|
|
|
|
=item "" |
|
1616
|
|
|
|
|
|
|
|
|
1617
|
|
|
|
|
|
|
=item "" |
|
1618
|
|
|
|
|
|
|
|
|
1619
|
|
|
|
|
|
|
Note: only works within a |
|
1620
|
|
|
|
|
|
|
|
|
1621
|
|
|
|
|
|
|
=item " |
|
1622
|
|
|
|
|
|
|
|
|
1623
|
|
|
|
|
|
|
Note: this must be like |
|
1624
|
|
|
|
|
|
|
|
|
1625
|
|
|
|
|
|
|
=back |
|
1626
|
|
|
|
|
|
|
|
|
1627
|
|
|
|
|
|
|
=cut |
|
1628
|
|
|
|
|
|
|
|
|
1629
|
|
|
|
|
|
|
{ |
|
1630
|
|
|
|
|
|
|
# Recursively get link elements. This is necessary in order |
|
1631
|
|
|
|
|
|
|
# to preserve their order. Too bad Mozilla doesn't have |
|
1632
|
|
|
|
|
|
|
# an `all' method like Internet Explorer. |
|
1633
|
|
|
|
|
|
|
|
|
1634
|
|
|
|
|
|
|
my @links; |
|
1635
|
|
|
|
|
|
|
|
|
1636
|
|
|
|
|
|
|
sub _extract_links { |
|
1637
|
|
|
|
|
|
|
my ($self, $subelement) = @_; |
|
1638
|
|
|
|
|
|
|
my $node; |
|
1639
|
|
|
|
|
|
|
|
|
1640
|
|
|
|
|
|
|
# The first time, it's called with no subelement |
|
1641
|
|
|
|
|
|
|
if (defined $subelement) { |
|
1642
|
|
|
|
|
|
|
$node = $subelement; |
|
1643
|
|
|
|
|
|
|
} else { |
|
1644
|
|
|
|
|
|
|
@links = (); |
|
1645
|
|
|
|
|
|
|
$node = $self->get_document_element; |
|
1646
|
|
|
|
|
|
|
} |
|
1647
|
|
|
|
|
|
|
|
|
1648
|
|
|
|
|
|
|
# If it's a link element, get it; otherwise, recurse if has children |
|
1649
|
|
|
|
|
|
|
if ($node->GetNodeName =~ /^(iframe|frame|area|a)$/i) { |
|
1650
|
|
|
|
|
|
|
my $tagname = lc $1; |
|
1651
|
|
|
|
|
|
|
|
|
1652
|
|
|
|
|
|
|
if ($tagname eq 'a') { |
|
1653
|
|
|
|
|
|
|
# Element interface is more convenient for attributes |
|
1654
|
|
|
|
|
|
|
my $element = $node->QueryInterface(Mozilla::DOM::Element->GetIID); |
|
1655
|
|
|
|
|
|
|
# are links only if they have an href |
|
1656
|
|
|
|
|
|
|
push @links, Mozilla::Mechanize::Link->new($element, $self) |
|
1657
|
|
|
|
|
|
|
if $element->HasAttribute('href'); |
|
1658
|
|
|
|
|
|
|
$self->debug("added '$tagname' link"); |
|
1659
|
|
|
|
|
|
|
} else { |
|
1660
|
|
|
|
|
|
|
push @links, Mozilla::Mechanize::Link->new($node, $self); |
|
1661
|
|
|
|
|
|
|
$self->debug("added '$tagname' link"); |
|
1662
|
|
|
|
|
|
|
} |
|
1663
|
|
|
|
|
|
|
} elsif ($node->HasChildNodes) { |
|
1664
|
|
|
|
|
|
|
my @children = $node->GetChildNodes; |
|
1665
|
|
|
|
|
|
|
# skips #text nodes |
|
1666
|
|
|
|
|
|
|
foreach my $child (grep {$_->GetNodeName !~ /^#/} @children) { |
|
1667
|
|
|
|
|
|
|
$self->_extract_links($child); |
|
1668
|
|
|
|
|
|
|
} |
|
1669
|
|
|
|
|
|
|
} |
|
1670
|
|
|
|
|
|
|
|
|
1671
|
|
|
|
|
|
|
# Continue only at the top-level |
|
1672
|
|
|
|
|
|
|
return if defined $subelement; |
|
1673
|
|
|
|
|
|
|
|
|
1674
|
|
|
|
|
|
|
$self->{links} = \@links; |
|
1675
|
|
|
|
|
|
|
return wantarray ? @{ $self->{links} } : $self->{links}; |
|
1676
|
|
|
|
|
|
|
} |
|
1677
|
|
|
|
|
|
|
} |
|
1678
|
|
|
|
|
|
|
|
|
1679
|
|
|
|
|
|
|
=head2 $moz->_extract_images() |
|
1680
|
|
|
|
|
|
|
|
|
1681
|
|
|
|
|
|
|
Return a list of images. |
|
1682
|
|
|
|
|
|
|
All images are mapped onto the L |
|
1683
|
|
|
|
|
|
|
interface that mimics L. |
|
1684
|
|
|
|
|
|
|
|
|
1685
|
|
|
|
|
|
|
=cut |
|
1686
|
|
|
|
|
|
|
|
|
1687
|
|
|
|
|
|
|
{ |
|
1688
|
|
|
|
|
|
|
# Recursively get image elements. This is necessary in order |
|
1689
|
|
|
|
|
|
|
# to preserve their order. Too bad Mozilla doesn't have |
|
1690
|
|
|
|
|
|
|
# an `all' method like Internet Explorer. |
|
1691
|
|
|
|
|
|
|
|
|
1692
|
|
|
|
|
|
|
my @images; |
|
1693
|
|
|
|
|
|
|
|
|
1694
|
|
|
|
|
|
|
sub _extract_images { |
|
1695
|
|
|
|
|
|
|
my ($self, $subelement) = @_; |
|
1696
|
|
|
|
|
|
|
my $node; |
|
1697
|
|
|
|
|
|
|
|
|
1698
|
|
|
|
|
|
|
# The first time, it's called with no subelement |
|
1699
|
|
|
|
|
|
|
if (defined $subelement) { |
|
1700
|
|
|
|
|
|
|
$node = $subelement; |
|
1701
|
|
|
|
|
|
|
} else { |
|
1702
|
|
|
|
|
|
|
@images = (); |
|
1703
|
|
|
|
|
|
|
$node = $self->get_document_element; |
|
1704
|
|
|
|
|
|
|
} |
|
1705
|
|
|
|
|
|
|
|
|
1706
|
|
|
|
|
|
|
# If it's an image element, get it; otherwise, recurse if has children |
|
1707
|
|
|
|
|
|
|
if ($node->GetNodeName =~ /^(img|input)$/i) { |
|
1708
|
|
|
|
|
|
|
my $tagname = lc $1; |
|
1709
|
|
|
|
|
|
|
|
|
1710
|
|
|
|
|
|
|
if ($tagname eq 'input') { |
|
1711
|
|
|
|
|
|
|
# Element interface is more convenient for attributes |
|
1712
|
|
|
|
|
|
|
my $element = $node->QueryInterface(Mozilla::DOM::Element->GetIID); |
|
1713
|
|
|
|
|
|
|
# are images only if they have a src |
|
1714
|
|
|
|
|
|
|
# (XXX: or maybe should be if type="image"...) |
|
1715
|
|
|
|
|
|
|
push @images, Mozilla::Mechanize::Image->new($element, $self) |
|
1716
|
|
|
|
|
|
|
if $element->HasAttribute('src'); |
|
1717
|
|
|
|
|
|
|
$self->debug("added '$tagname' image"); |
|
1718
|
|
|
|
|
|
|
} else { |
|
1719
|
|
|
|
|
|
|
push @images, Mozilla::Mechanize::Image->new($node, $self); |
|
1720
|
|
|
|
|
|
|
$self->debug("added '$tagname' image"); |
|
1721
|
|
|
|
|
|
|
} |
|
1722
|
|
|
|
|
|
|
} elsif ($node->HasChildNodes) { |
|
1723
|
|
|
|
|
|
|
my @children = $node->GetChildNodes; |
|
1724
|
|
|
|
|
|
|
# skips #text nodes |
|
1725
|
|
|
|
|
|
|
foreach my $child (grep {$_->GetNodeName !~ /^#/} @children) { |
|
1726
|
|
|
|
|
|
|
$self->_extract_images($child); |
|
1727
|
|
|
|
|
|
|
} |
|
1728
|
|
|
|
|
|
|
} |
|
1729
|
|
|
|
|
|
|
|
|
1730
|
|
|
|
|
|
|
# Continue only at the top-level |
|
1731
|
|
|
|
|
|
|
return if defined $subelement; |
|
1732
|
|
|
|
|
|
|
|
|
1733
|
|
|
|
|
|
|
$self->{images} = \@images; |
|
1734
|
|
|
|
|
|
|
return wantarray ? @{ $self->{forms} } : $self->{forms}; |
|
1735
|
|
|
|
|
|
|
} |
|
1736
|
|
|
|
|
|
|
} |
|
1737
|
|
|
|
|
|
|
|
|
1738
|
|
|
|
|
|
|
=head2 $self->_wait_while_busy() |
|
1739
|
|
|
|
|
|
|
|
|
1740
|
|
|
|
|
|
|
This adds a "single-shot" idle callback that does Gtk2->main_quit, |
|
1741
|
|
|
|
|
|
|
then does Gtk2->main. The result is that whenever the UI becomes idle |
|
1742
|
|
|
|
|
|
|
it will exit the main loop. Thanks to muppet for the idea. |
|
1743
|
|
|
|
|
|
|
This is repeated until the net_stop event fires, indicating that |
|
1744
|
|
|
|
|
|
|
the new page has finished loading. (Note therefore that you can only |
|
1745
|
|
|
|
|
|
|
call this when you expect a new page to load.) |
|
1746
|
|
|
|
|
|
|
|
|
1747
|
|
|
|
|
|
|
=cut |
|
1748
|
|
|
|
|
|
|
|
|
1749
|
|
|
|
|
|
|
sub _wait_while_busy { |
|
1750
|
|
|
|
|
|
|
my $self = shift; |
|
1751
|
|
|
|
|
|
|
my $agent = $self->agent; |
|
1752
|
|
|
|
|
|
|
|
|
1753
|
|
|
|
|
|
|
do { |
|
1754
|
|
|
|
|
|
|
Glib::Idle->add(sub { |
|
1755
|
|
|
|
|
|
|
Gtk2->main_quit; |
|
1756
|
|
|
|
|
|
|
FALSE; # uninstall |
|
1757
|
|
|
|
|
|
|
}, undef, G_PRIORITY_LOW); |
|
1758
|
|
|
|
|
|
|
Gtk2->main; |
|
1759
|
|
|
|
|
|
|
} until $agent->{netstopped}; |
|
1760
|
|
|
|
|
|
|
|
|
1761
|
|
|
|
|
|
|
$self->{$_} = undef for qw(forms cur_form links images); |
|
1762
|
|
|
|
|
|
|
return $self->success; |
|
1763
|
|
|
|
|
|
|
} |
|
1764
|
|
|
|
|
|
|
|
|
1765
|
|
|
|
|
|
|
=head2 warn( @messages ) |
|
1766
|
|
|
|
|
|
|
|
|
1767
|
|
|
|
|
|
|
Centralized warning method, for diagnostics and non-fatal problems. |
|
1768
|
|
|
|
|
|
|
Defaults to calling C, but may be overridden by setting |
|
1769
|
|
|
|
|
|
|
C in the construcotr. |
|
1770
|
|
|
|
|
|
|
|
|
1771
|
|
|
|
|
|
|
=cut |
|
1772
|
|
|
|
|
|
|
|
|
1773
|
|
|
|
|
|
|
sub warn { |
|
1774
|
|
|
|
|
|
|
my $self = shift; |
|
1775
|
|
|
|
|
|
|
|
|
1776
|
|
|
|
|
|
|
return unless my $handler = $self->{onwarn}; |
|
1777
|
|
|
|
|
|
|
|
|
1778
|
|
|
|
|
|
|
return if $self->quiet; |
|
1779
|
|
|
|
|
|
|
|
|
1780
|
|
|
|
|
|
|
$handler->(@_); |
|
1781
|
|
|
|
|
|
|
} |
|
1782
|
|
|
|
|
|
|
|
|
1783
|
|
|
|
|
|
|
=head2 die( @messages ) |
|
1784
|
|
|
|
|
|
|
|
|
1785
|
|
|
|
|
|
|
Centralized error method. Defaults to calling C, but |
|
1786
|
|
|
|
|
|
|
may be overridden by setting C in the constructor. |
|
1787
|
|
|
|
|
|
|
|
|
1788
|
|
|
|
|
|
|
=cut |
|
1789
|
|
|
|
|
|
|
|
|
1790
|
|
|
|
|
|
|
sub die { |
|
1791
|
|
|
|
|
|
|
my $self = shift; |
|
1792
|
|
|
|
|
|
|
|
|
1793
|
|
|
|
|
|
|
return unless my $handler = $self->{onerror}; |
|
1794
|
|
|
|
|
|
|
|
|
1795
|
|
|
|
|
|
|
$handler->(@_); |
|
1796
|
|
|
|
|
|
|
} |
|
1797
|
|
|
|
|
|
|
|
|
1798
|
|
|
|
|
|
|
# Not a method |
|
1799
|
|
|
|
|
|
|
sub __warn { |
|
1800
|
|
|
|
|
|
|
|
|
1801
|
|
|
|
|
|
|
eval "require Carp"; |
|
1802
|
|
|
|
|
|
|
if ( $@ ) { |
|
1803
|
|
|
|
|
|
|
CORE::warn @_; |
|
1804
|
|
|
|
|
|
|
} else { |
|
1805
|
|
|
|
|
|
|
&Carp::carp; |
|
1806
|
|
|
|
|
|
|
} |
|
1807
|
|
|
|
|
|
|
} |
|
1808
|
|
|
|
|
|
|
|
|
1809
|
|
|
|
|
|
|
# Not a method |
|
1810
|
|
|
|
|
|
|
sub __die { |
|
1811
|
|
|
|
|
|
|
require Carp; |
|
1812
|
|
|
|
|
|
|
&Carp::croak; # pass thru |
|
1813
|
|
|
|
|
|
|
} |
|
1814
|
|
|
|
|
|
|
|
|
1815
|
|
|
|
|
|
|
sub debug { |
|
1816
|
|
|
|
|
|
|
my ($self, $msg) = @_; |
|
1817
|
|
|
|
|
|
|
my (undef, $file, $line) = caller(); |
|
1818
|
|
|
|
|
|
|
print STDERR "$msg at $file line $line\n" if $self->{debug}; |
|
1819
|
|
|
|
|
|
|
} |
|
1820
|
|
|
|
|
|
|
|
|
1821
|
|
|
|
|
|
|
|
|
1822
|
|
|
|
|
|
|
=head2 $self->_extra_headers( ) |
|
1823
|
|
|
|
|
|
|
|
|
1824
|
|
|
|
|
|
|
(XXX: Not implemented.) |
|
1825
|
|
|
|
|
|
|
For the moment we only support B. |
|
1826
|
|
|
|
|
|
|
|
|
1827
|
|
|
|
|
|
|
=cut |
|
1828
|
|
|
|
|
|
|
|
|
1829
|
|
|
|
|
|
|
sub _extra_headers { |
|
1830
|
|
|
|
|
|
|
my( $self, $uri ) = @_; |
|
1831
|
|
|
|
|
|
|
|
|
1832
|
|
|
|
|
|
|
my $header = ""; |
|
1833
|
|
|
|
|
|
|
|
|
1834
|
|
|
|
|
|
|
for my $header ( keys %{ $self->{headers} } ) { |
|
1835
|
|
|
|
|
|
|
next unless defined $self->{headers}{ $header }; |
|
1836
|
|
|
|
|
|
|
( my $hfield = $header ) =~ s/(\w+)/ucfirst lc $1/eg; |
|
1837
|
|
|
|
|
|
|
$header .= "$hfield: $self->{headers}{ $header }\015\012"; |
|
1838
|
|
|
|
|
|
|
} |
|
1839
|
|
|
|
|
|
|
|
|
1840
|
|
|
|
|
|
|
my $host_port = $uri->can( 'host_port' ) |
|
1841
|
|
|
|
|
|
|
? $uri->host_port : $uri->as_string; |
|
1842
|
|
|
|
|
|
|
return $header unless exists $self->{basic_authentication}{ $host_port }; |
|
1843
|
|
|
|
|
|
|
|
|
1844
|
|
|
|
|
|
|
my $realm = $self->{basic_authentication}{ $host_port }{__active_realm__}; |
|
1845
|
|
|
|
|
|
|
my( $user, $pass ) = $self->get_basic_credentials( $realm, $uri ); |
|
1846
|
|
|
|
|
|
|
|
|
1847
|
|
|
|
|
|
|
$header .= defined $user ? __authorization_basic( $user, $pass ) : ""; |
|
1848
|
|
|
|
|
|
|
|
|
1849
|
|
|
|
|
|
|
return $header; |
|
1850
|
|
|
|
|
|
|
} |
|
1851
|
|
|
|
|
|
|
|
|
1852
|
|
|
|
|
|
|
=head1 INTERNAL ONLY NON-METHODS |
|
1853
|
|
|
|
|
|
|
|
|
1854
|
|
|
|
|
|
|
=head2 __prop_value( $key[, $value] ) |
|
1855
|
|
|
|
|
|
|
|
|
1856
|
|
|
|
|
|
|
Check to see if we support the property C<$key> and return a validated |
|
1857
|
|
|
|
|
|
|
value or the default value from C<%moz_properties>. |
|
1858
|
|
|
|
|
|
|
|
|
1859
|
|
|
|
|
|
|
=cut |
|
1860
|
|
|
|
|
|
|
|
|
1861
|
|
|
|
|
|
|
sub __prop_value($;$) { |
|
1862
|
|
|
|
|
|
|
my ($key, $value) = @_; |
|
1863
|
|
|
|
|
|
|
$key = lc $key; |
|
1864
|
|
|
|
|
|
|
|
|
1865
|
|
|
|
|
|
|
exists $moz_property{$key} or return undef; |
|
1866
|
|
|
|
|
|
|
@_ > 1 or return $moz_property{$key}{value}; |
|
1867
|
|
|
|
|
|
|
|
|
1868
|
|
|
|
|
|
|
CASE: { |
|
1869
|
|
|
|
|
|
|
local $_ = $moz_property{ $key }{type}; |
|
1870
|
|
|
|
|
|
|
|
|
1871
|
|
|
|
|
|
|
/^b$/ and do { |
|
1872
|
|
|
|
|
|
|
defined $value or return $moz_property{$key}{value}; |
|
1873
|
|
|
|
|
|
|
return $value ? 1 : 0; |
|
1874
|
|
|
|
|
|
|
}; |
|
1875
|
|
|
|
|
|
|
/^n$/ and do { |
|
1876
|
|
|
|
|
|
|
defined $value or return $moz_property{$key}{value}; |
|
1877
|
|
|
|
|
|
|
return $value =~ /((?:\+|-)?[0-9]+)/ ? $1 : 0; |
|
1878
|
|
|
|
|
|
|
}; |
|
1879
|
|
|
|
|
|
|
} |
|
1880
|
|
|
|
|
|
|
} |
|
1881
|
|
|
|
|
|
|
|
|
1882
|
|
|
|
|
|
|
=head2 __authorization_basic( $user, $pass ) |
|
1883
|
|
|
|
|
|
|
|
|
1884
|
|
|
|
|
|
|
(XXX: Not implemented.) |
|
1885
|
|
|
|
|
|
|
Return a HTTP "Authorization: Basic xxx" header. |
|
1886
|
|
|
|
|
|
|
|
|
1887
|
|
|
|
|
|
|
=cut |
|
1888
|
|
|
|
|
|
|
|
|
1889
|
|
|
|
|
|
|
sub __authorization_basic { |
|
1890
|
|
|
|
|
|
|
my( $user, $pass ) = @_; |
|
1891
|
|
|
|
|
|
|
defined $user && defined $pass or return; |
|
1892
|
|
|
|
|
|
|
|
|
1893
|
|
|
|
|
|
|
require MIME::Base64; |
|
1894
|
|
|
|
|
|
|
return "Authorization: Basic " . |
|
1895
|
|
|
|
|
|
|
MIME::Base64::encode_base64( "$user:$pass" ) . |
|
1896
|
|
|
|
|
|
|
"\015\012"; |
|
1897
|
|
|
|
|
|
|
} |
|
1898
|
|
|
|
|
|
|
|
|
1899
|
|
|
|
|
|
|
|
|
1900
|
|
|
|
|
|
|
1; |
|
1901
|
|
|
|
|
|
|
|
|
1902
|
|
|
|
|
|
|
=head1 BUGS |
|
1903
|
|
|
|
|
|
|
|
|
1904
|
|
|
|
|
|
|
Send bugs directly to me or use |
|
1905
|
|
|
|
|
|
|
LErt.cpan.orgENoAuthEBugs.html?Dist=Mozilla-Mechanize>. |
|
1906
|
|
|
|
|
|
|
|
|
1907
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
1908
|
|
|
|
|
|
|
|
|
1909
|
|
|
|
|
|
|
L, |
|
1910
|
|
|
|
|
|
|
L, |
|
1911
|
|
|
|
|
|
|
L, |
|
1912
|
|
|
|
|
|
|
L |
|
1913
|
|
|
|
|
|
|
|
|
1914
|
|
|
|
|
|
|
=head1 CREDITS |
|
1915
|
|
|
|
|
|
|
|
|
1916
|
|
|
|
|
|
|
See F. In particular, I acknowledge having copied a lot of |
|
1917
|
|
|
|
|
|
|
the code from L, by Abe Timmerman. |
|
1918
|
|
|
|
|
|
|
|
|
1919
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
1920
|
|
|
|
|
|
|
|
|
1921
|
|
|
|
|
|
|
Copyright 2005,2009 Scott Lanning . All rights reserved. |
|
1922
|
|
|
|
|
|
|
|
|
1923
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
|
1924
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
|
1925
|
|
|
|
|
|
|
|
|
1926
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, |
|
1927
|
|
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
1928
|
|
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
1929
|
|
|
|
|
|
|
|
|
1930
|
|
|
|
|
|
|
=cut |