Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

javascript interpreter in perl???

by monkster (Sexton)
on Feb 05, 2008 at 08:07 UTC ( #666236=perlquestion: print w/ replies, xml ) Need Help??
monkster has asked for the wisdom of the Perl Monks concerning the following question:

can u pl suggest any perl modules that does the job of a javascript interpreter.. my main interest is to solve the javascripts that are put in html pages nowadays. pl suggest how that can be done.. thanks a lot in advance :)

Update:Win32::IE::Mechanize works well in windows.. But I am supposed to do this in solaris platform..

WWW::Mechanize::Plugin::JavaScript attempts to solve the javascripts in the html page, but unfortunately I have only JE as the interpreter and it is not able to handle browser specific commands..

Comment on javascript interpreter in perl???
Select or Download Code
Re: javascript interpreter in perl???
by McDarren (Abbot) on Feb 05, 2008 at 08:32 UTC
    I think you need to define what you mean by "solve the javascripts" - I wasn't aware that javascript was a riddle (although some might argue otherwise ;))

    CGI has some javascript support - refer to the docs.

    Regards,
    Darren

Re: javascript interpreter in perl???
by plobsing (Friar) on Feb 05, 2008 at 08:37 UTC
    By "solve javascripts" do you mean view the content of a heavily js'ed/ajaxed page? In that case:

    Javascript engines: As for actually using javascript inside real world webpages (malformedness problems, etc), you are probably best automating a browser (Win32::IE::Mechanize, etc), but theres also WWW::Mechanize::Plugin::JavaScript

    Of course you could have found all that with a super search and a quick look at cpan.
Re: ny JavaScript interpreters in perl???
by ikegami (Pope) on Feb 05, 2008 at 08:52 UTC
Re: javascript interpreter in perl???
by marcussen (Pilgrim) on Feb 05, 2008 at 09:42 UTC
    Just on the odd chance that you wished to automate the creation of such javascript code, for instance AJAX, you can look at CGI::Ajax If not, then you should inspect the modules provided in the earlier replies and if neither offer what you need you can look at any of the other 683 results a search for javascript on cpan returns.
    while ( whoring ){ for ( xp ){ grep /the source/,@perlmonks; }}
Re: javascript interpreter in perl???
by Erez (Curate) on Feb 05, 2008 at 09:44 UTC
Re: javascript interpreter in perl???
by bart (Canon) on Feb 05, 2008 at 12:00 UTC
    If you use the module Win32::IE::Mechanize on Windows as an alternative for WWW::Mechanize then it'll execute the Javascript on the web pages it fetches. That happens due to the fact that this module uses the MSIE core to fetch the web pages.

    Nevertheless, the idea of being able to use Javascript from within perl (thus, without using a browser core) and have it being able to use the DOM to process a html page does appeal to me. I'd prefer to have some tie-like mechanism for variables and object fields in Javascript, which invokes routines in Perl. Chalk it up on the wishlist...

      Hi..
      Tried it with Win32::IE::Mechanize in windows and i was able to get the code.. thanks a lot :)
      But want it in unix as the main workin platform is that. so still tryin to find a way to access web page and process the javascripts that occur in that.

      However thanks a ton for the help :)
      Sorry for not responding for a long time.. I understand that JavaScript interpreter and DOM must be glued together to solve this.. There's a method in JavaScript::SpiderMonkey called 'function_set' which allows a perl code to be exec thru javascript.. So i thought of implementing the DOM accessing javascript commands thru this method by using HTML::TagParser or HTML::Tree in perl..

      But I encounter a few problems here.. Please see this post to find out them..

      And please help me solve them..

      Thanks a lot..

        I'm interested in looking into this, even though I've never used JavaScript::SpiderMonkey in my life. So it's going to take a while.

        I see you've started looking into HTML::TagParser, and as I currently have some interest in HTML::Tree due to current work on another module (where most work is being done by grandfather), I'm interested of trying that route.

        But don't hold your breath. :)

Re: javascript interpreter in perl???
by Starky (Chaplain) on Feb 05, 2008 at 14:46 UTC
    At first I was confused by peculiar syntax of the question, but then it occurred to me that the question must have been submitted via SMS.

    I believe I have a newfound respect for Herr monkster.

      my excessive usage of sms brought in such a language.. sorry about that.. (or sry abt tat :) )
Re: javascript interpreter in perl???
by gam3 (Curate) on Feb 06, 2008 at 04:44 UTC
    Note that an interpreter is not all you need to solve this problem. You need to have a DOM as well.

    Depending on what you want to do this can be hard or easy.

    -- gam3
    A picture is worth a thousand words, but takes 200K.
      Can you please tell me how to get a DOM for working on the web page??
      I tested the JE module and it was able to solve javascripts.. but I not sure how to test it connected with a web page.. i mean, the module was able to solve simple scripts like
      function xxx() { return 42; }

      but i do not know how to test it for scripts that access the DOM. for example. the script has statements like document.getElementByID etc. how do i create the handle of the retrieved web page as 'document'..

      can u please suggest me on this..
        If you really want all of this to be in perl it is quite a hard problem requiring javascript and perl code.

        You might want to look at HTML-DOM, but there is still a lot of glue needed to tie it all together.

        -- gam3
        A picture is worth a thousand words, but takes 200K.
Re: javascript interpreter in perl???
by Anonymous Monk on Jun 28, 2010 at 00:19 UTC
    Here's my little Javascript Perl Interpreter. It's much easier than you think.
    #!/usr/bin/perl ###################################################################### +######### # JtoP.cgi + # # + # # This program is distributed in the hope that it will be useful, + # # but WITHOUT ANY WARRANTY; without even the implied warranty of + # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + # # GNU General Public License for more details. + # # + # # + # # Last modified: 06/27/10 + # ###################################################################### +######### # Set-up the cgi enviroment # this little gizmo is something I adopted # and works like a champ use CGI; $query = new CGI; @names = $query->param; foreach $parameter (@names) { $_ = $query->param($parameter); $input{$parameter} = $_; } # we won't have this until the Ajax kicks in after page load # then it happens quietly behind the scenes if ($_) { $screenwidth = $input{'screenwidth'}; $screenheight = $input{'screenheight'}; $availwidth = $input{'availwidth'}; $availheight = $input{'availheight'}; $screen = "$input{'screenwidth'} x $input{'screenheight'}"; $availscreen = "$input{'availwidth'} x $input{'availheight'}"; $colordepth = $input{'colordepth'}; open(FILE, ">>foobat.txt"); print FILE "$screen\n"; print FILE "$availscreen\n"; print FILE "$colordepth\n"; close FILE; } # no HTML here just get to the Ajax and send the data print "content-type: text/html\n\n"; print qq~ <script type="text/javascript"> // OOAjax thanks to Patrick Hunlock function ajaxObject(url, callbackFunction) { var that=this; this.updating = false; this.abort = function() { if (that.updating) { that.updating=false; that.AJAX.abort(); that.AJAX=null; } } this.update = function(passData,postMethod) { if (that.updating) { return false; } that.AJAX = null; if (window.XMLHttpRequest) { that.AJAX=new XMLHttpRequest(); } else { that.AJAX=new ActiveXObject("Microsoft.XMLHTTP"); } if (that.AJAX==null) { return false; } else { that.AJAX.onreadystatechange = function() { if (that.AJAX.readyState==4) { that.updating=false; that.callback(that.AJAX.responseText,that.AJAX.status,that.A +JAX.responseXML); that.AJAX=null; } } that.updating = new Date(); if (/post/i.test(postMethod)) { var uri=urlCall+'?'+that.updating.getTime(); that.AJAX.open("POST", uri, true); that.AJAX.setRequestHeader("Content-type", "application/x-www- +form-urlencoded"); that.AJAX.setRequestHeader("Content-Length", passData.length); that.AJAX.send(passData); } else { var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getT +ime()); that.AJAX.open("GET", uri, true); that.AJAX.send(null); } return true; } + } var urlCall = url; this.callback = callbackFunction || function () { }; } // get the variables //var sw = document.getElementById("screenwidth").value; //var sh = document.getElementById("screenheight").value; //var aw = document.getElementById("availwidth").value; //var ah = document.getElementById("availheight").value; //var cd = document.getElementById("colordepth").value; var sw = screen.width; var sh = screen.height; var aw = screen.availWidth; var ah = screen.availHeight; var cd = screen.colorDepth; // the Ajax object var myRequest = new ajaxObject('JtoP.cgi'); // the query string var sendString = 'screenwidth='+sw+'&screenheight='+sh+'&availwidt +h='+aw+'&availheight='+ah+'&colordepth='+cd; // and go man go myRequest.update(sendString,'POST'); </script> ~; 1;
    It works without form fields. It send the javascript data straight to Perl upon page load. Maybe too simple but it works like a charm. Ted Cambron tedcambron@hotmail.com
        Call it what you will. It works and a whole lot better than any solution you have given.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://666236]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (12)
As of 2014-10-21 15:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (105 votes), past polls