#!/usr/bin/perl ################################################################################ #these modules must be installed ################################################################################ use JavaScript::SpiderMonkey; use LWP::UserAgent; ################################################################################ #HTML headers included to make page show in browser ################################################################################ print "HTTP/1.0 200 OK\n"; print "Content-Type: text/html\n\n\n"; ################################################################################ #Enable javascript engine ################################################################################ my $js = JavaScript::SpiderMonkey->new(); $js->init(); # Initialize Runtime/Context #Define a perl callback for a new JavaScript function $js->function_set("print_to_perl", sub { print "@_\n"; }); # Create a new (nested) object and a property $js->property_by_path("document.location.href"); # Execute some code my $rc = $js->eval(q! document.location.href = append("https://", "admin8.gtc.edu/wa/wa?&TYPE=M&PID=CORE-WBMAIN&TOKENIDX=3292319802"); print_to_perl("URL is ", document.location.href); function append(first, second) { return first + second; } !); # Get the value of a property set in JS my $url = $js->property_get("document.location.href"); ################################################################################ #Get page contents ################################################################################ require HTTP::Request; my $req = new HTTP::Request('GET', $url); my $ua = new LWP::UserAgent; my $res = $ua->request($req); print $res->code."\n"; print "\n\n"; print $res->content; ################################################################################ #Cleanup ################################################################################ $js->destroy();