0

Lets say i have an array which holds the contents of the body tag like shown below: print Dumper(\@array);

$VAR1 = 

[

<body>

<table width=\'100%\' height=\'100%\'>

<tr>

<td width=\'100%\' height=\'100%\'

valign=\'top\'><div style=\'height:100%\' hrefmode=\'ajax-html\' id=\'a_tabbar\' 

width=\'100%\'  imgpath=\'../images/datagrid/\' skinColors=\'#FCFBFC,#F4F3EE\'/>

</td>

</tr>

</table>

<script>

tabbar=newdhtmlXTabBar(\'a_tabbar\',\'top\');
tabbar.setImagePath(\'../images/datagrid/\');
tabbar.setSkinColors(\'#FCFBFC\',\'#F4F3EE\');
tabbar.setHrefMode(\'ajax-html\');

</script>

<script>
tabbar.addTab(\'866346569493123700\',\'Details \',\'242px\'); 
tabbar.setContentHref(\'866346569493123700\',\'../reports/dhtmlgridqueryhandler.jsp?id=866346569493123700&oracleDb=read&htmlDataId=&Type=generic&queryCode=GetDetails\');
tabbar.setTabActive(\'866346569493123700\');

</script>

</body>
]

Lets say that i want to fetch the id of the "div" tag from the contents of the @array:

I do that by :

$tree=HTML::TreeBuilder->new_from_content(@array);
$first_match = $tree->find_by_attribute('hrefmode' => 'ajax-html');
$id = $first_match->attr('id');

This works fine for the cases where there is a single value for the attribute. But how do i fetch 866346569493123700 from the script tag in @array?

Any help on this would be much appreciated as i have been trying to get this for hours

1 Answers1

1

Your use of HTML::TreeBuilder for parsing HTML is very good. You're running into a problem though because you also want information from inside a <script> tag with contains JavaScript. Unfortunately, the above module isn't going to help you beyond isolating the JS.

Given the simplicity of your goal, I believe that I'd just use a regex to find the tab id. The final command tabbar.setTabActive is fairly simple and most likely won't change much since it's a function that only accepts one value and is integral to the functionality of creating and activating this new tab.

The below code demonstrates iterating over the script tags until it finds a match for tabid:

use HTML::TreeBuilder;

use strict;
use warnings;

my $root = HTML::TreeBuilder->new_from_content(<DATA>);

if (my $element = $root->look_down('_tag' => 'div', 'hrefmode' => 'ajax-html')) {
    print "div.id = '" . $element->attr('id') . "'\n";
} else {
    warn "div.id not found";
}

my $tabid = '';
for ($root->find_by_tag_name('script')) {
    my $scripttext = $_->as_HTML;
    if ($scripttext =~ /tabbar.setTabActive\('(\d+)'\);/) {
        $tabid = $1;
        print "TabID = '$tabid'";
        last;
    }
}
warn "Tab ID not found\n" if ! $tabid;


__DATA__
<body>
<table width='100%' height='100%'>
<tr>
<td width='100%' height='100%'
valign='top'><div style='height:100%' hrefmode='ajax-html' id='a_tabbar' 
width='100%'  imgpath='../images/datagrid/' skinColors='#FCFBFC,#F4F3EE'/>
</td>
</tr>
</table>
<script>
tabbar=newdhtmlXTabBar('a_tabbar','top');
tabbar.setImagePath('../images/datagrid/');
tabbar.setSkinColors('#FCFBFC','#F4F3EE');
tabbar.setHrefMode('ajax-html');
</script>
<script>
tabbar.addTab('866346569493123700','Details ','242px'); 
tabbar.setContentHref('866346569493123700','../reports/dhtmlgridqueryhandler.jsp?id=866346569493123700&oracleDb=read&htmlDataId=&Type=generic&queryCode=GetDetails');
tabbar.setTabActive('866346569493123700');
</script>
</body>
Miller
  • 34,962
  • 4
  • 39
  • 60