WWW::Mechanize::Plugin::JavaScript - JavaScript plugin for WWW::Mechanize
Version 0.009 (alpha)
use WWW::Mechanize; $m = new WWW::Mechanize; $m->use_plugin('JavaScript'); $m->get('http://www.cpan.org/'); $m->get('javascript:alert("Hello!")'); # prints Hello! # (not yet implemented) $m->use_plugin(JavaScript => engine => 'SpiderMonkey', alert => \&alert, # custom alert function confirm => \&confirm, prompt => \&prompt, init => \&init, # initialisation function ); # for the JS environment
This module is a plugin for WWW::Mechanize that provides JavaScript capabilities (who would have guessed?).
To load the plugin, just use WWW::Mechanize's use_plugin method (note that the current stable release of that module doesn't support this; see "PREREQUISITES", below):
use_plugin
$m = new WWW::Mechanize; $m->use_plugin('JavaScript');
You can pass options to the plugin via the use_plugin method. It takes hash-style arguments and they are as follows:
Which JavaScript back end to use. Currently, this module only supports JE, a pure-Perl JavaScript interpreter. Later it will support SpiderMonkey via either JavaScript::SpiderMonkey or JavaScript.pm. If this option is not specified, either SpiderMonkey or JE will be used, whichever is available. It is possible to write one's own bindings for a particular JavaScript engine. See below, under "BACK ENDS".
Use this to provide a custom alert function. The default one will print its arguments followed by a new line.
alert
Like alert, but for the confirm function instead. There is no default.
confirm
Likewise.
Pass to this option a reference to a subroutine and it will be run every time a new JavaScript environment is initialised. This happens after the functions above have been created. The first argument will be the plugin object (more on that below). You can use this, for instance, to make your own functions available to JavaScript.
WWW::Mechanize's use_plugin method will return a plugin object. The same object can be retrieved via $m->plugin('JavaScript') after the plugin is loaded. The following methods can be called on that object:
$m->plugin('JavaScript')
This evaluates the JavaScript code passed to it. You can optionally pass two more arguments: the file name or URL, and the first line number.
This creates a new global JavaScript function out of a coderef. Pass the name as the first argument and the code ref as the second.
Sets the named variable to the value given. If you want to assign to a property of a property ... of a global property, pass each property name as a separate argument:
$m->plugin('JavaScript')->set( 'document', 'location', 'href' => 'http://www.perl.org/' );
With this you can bind Perl classes to JavaScript, so that JavaScript can handle objects of those classes. These class bindings will persist from one page to the next.
You should pass a hash ref that has the structure described in HTML::DOM::Interface, except that this method also accepts a _constructor hash element, which should be set to the name of the method to be called when the constructor function is called within JavaScript; e.g., _constructor => 'new'.
_constructor
_constructor => 'new'
The members of the HTML DOM that are available depend on the versions of HTML::DOM and CSS::DOM installed. See HTML::DOM::Interface and CSS::DOM::Interface.
For a list of the properties of the window object, see WWW::Mechanize::Plugin::DOM::Window.
The JavaScript plugin itself provides just the screen object, which is empty. Later this may be moved to the DOM plugin's window object, but that should make little difference to you, unless you are writing bindings for another scripting language.
screen
A back end has to be in the WWW::Mechanize::Plugin::JavaScript:: name space. It will be required by this plugin implicitly when its name is passed to the engine option.
require
engine
The following methods must be implemented:
This method is passed a window (WWW::Mechanize::Plugin::DOM::Window) object.
It has to create a JavaScript environment, in which the global object delegates to the window object for the members listed in %WWW::Mechanize::Plugin::DOM::Window::Interface (that's quite a mouthful, isn't it). When the window object is passed to the JavaScript environment, the global object must be returned instead.
%WWW::Mechanize::Plugin::DOM::Window::Interface
This method can optionally create window, self and frames properties that refer to the global object, but this is not necessary. It might make things a little more efficient.
window
self
frames
Finally, it has to return an object that implements the interface below.
The back end has to do some magic to make sure that, when the global object is passed to another JS environment, references to it automatically point to a new global object when the user (or calling code) browses to another page.
For instance, it could wrap up the global object in a proxy object that delegates to whichever global object corresponds to the document.
These correspond to those listed above for the plugin object. Those methods are simply delegated to the back end, except that bind_classes also does some caching if the back end hasn't been initialised yet.
bind_classes
new_function must also accept a third argument, indicating the return type. This (when specified) will be the name of a JavaScript function that does the type conversion. Only 'Number' is used right now.
new_function
This method needs to turn the event handler code in $code into a coderef, or an object that can be used as such, and then return it. That coderef will be called with an HTML::DOM::Event object as its sole argument. It's return value, if defined, will be used to determine whether the event's preventDefault method should be called.
$code
preventDefault
This will be called with a list of property names representing the 'path' to the property. The last argument will be a coderef that must be called with the value assigned to the property.
Note: This is actually not used right now. The requirement for this may be removed some time before version 1.
perl 5.8.3 or later (actually, this module doesn't use any features that perl 5.6 doesn't provide, but its prerequisites require 5.8.3)
HTML::DOM 0.010 or later
JE 0.022 or later (when there is a SpiderMonkey binding available it will become optional)
The experimental version of WWW::Mechanize available at http://www-mechanize.googlecode.com/svn/wm/branches/plugins/, revision 506 or higher
CSS::DOM
(See also "Bugs" in WWW::Mechanize::Plugin::DOM and "Bugs" in WWW::Mechanize::Plugin::JavaScript::JE.)
There is currently no system in place for preventing pages from different sites from communicating with each other.
To report bugs, please e-mail the author.
Copyright (C) 2007-8 Father Chrysostomos <join '@', sprout => join '.', reverse org => 'cpan'>
join '@', sprout => join '.', reverse org => 'cpan'
This program is free software; you may redistribute it and/or modify it under the same terms as perl.
WWW::Mechanize
WWW::Mechanize::Plugin::DOM
HTML::DOM
JE
JavaScript.pm
JavaScript::SpiderMonkey
1 POD Error
The following errors were encountered while parsing the POD:
You forgot a '=back' before '=head1'
To install WWW::Mechanize::Plugin::JavaScript, copy and paste the appropriate command in to your terminal.
cpanm
cpanm WWW::Mechanize::Plugin::JavaScript
CPAN shell
perl -MCPAN -e shell install WWW::Mechanize::Plugin::JavaScript
For more information on module installation, please visit the detailed CPAN module installation guide.