monkster has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,I am trying to get the contents of web pages that has JavaScripts in them.. Please see my previous query for more details on that.. As one of the suggestions I am tryin to glue JavaScript::SpiderMonkey and DOM together to handle DOM related javascript commands using the 'function_set' option in JavaScript::SpiderMonkey to write new functions in perl and call them in javascript. One of the problems is like
var metas = document.getElementsByTagName('meta');
The perl code returns an array of hash references whereas the value that comes here is the length of the array.. Also please tell me how to get the handle of the calling object.. eg
meta.getAttribute('name');
How to get the handle of meta in perl so as to return the appropriate value.. Please suggest me some ways to do the above..Thanks a lot in advance

Replies are listed 'Best First'.
Re: How to glue JavaScript and DOM
by Joost (Canon) on Feb 26, 2008 at 13:43 UTC
    The perl code returns an array of hash references whereas the value that comes here is the length of the array.
    What perl code? And the javascript code you've shown does nothing like that (or at least, it shouldn't).

    Also please tell me how to get the handle of the calling object.. eg
    I don't understand what you're asking. And the example doesn't help.

    How to get the handle of meta in perl so as to return the appropriate value..
    Again, I have no idea what you're asking.

      1. var metas = document.getElementsByTagName('meta') is supposed to return an array containin the lines that has the meta tag.. since this is not supported by SpiderMonkey per se, i wrote a perl code somethin like
      function_get("getElementsByTagName", sub { #use HTML::TagParser, my @array = $p->getElementsByTagName('@_); #$p must point to # the html file containin the page from which meta tags are # to be taken return @array; });
      So ideally i must get an array, but I m not..2. handle of the callin object: eg: javascript has lines like write
      metas[i].getAttribute('name'); //metas is the array obtained from the +above snippet<code>
      So to write a perl function similar to the one that i wrote for getElementsByTagName as shown above, i need to get a handle for this metas array object in perl.. How do i get this is my question..I guess I have it more clear now.. Please tell me if it's still hazy
        1. var metas = document.getElementsByTagName('meta') is supposed to return an array containin the lines that has the meta tag.
        document.getElementsByTagName is supposed to return a NodeList, not a plain array of Nodes.

        function_get("getElementsByTagName", sub { #use HTML::TagParser, my @array = $p->getElementsByTagName('@_); #$p must point to # the html file containin the page from which meta tags are # to be taken return @array; });
        That defines a global getElementsByTagName function. You want set it as a property of the document object instead. That way, you should get the document object as the first argument when called.

        update: in fact, function_get() does not exist in JavaScript::SpiderMonkey. There is a function_set, but that's a method. Have you actually read the documentation?

        So to write a perl function similar to the one that i wrote for getElementsByTagName as shown above, i need to get a handle for this metas array object in perl.. How do i get this is my question..
        Again, metas is not an array. Also, metas[i].getAttribute refers to the getAttribute property of whatever is element i in metas, so you'd better make sure that that property exists. As far as I can tell, your code does nothing of the kind.