I am trying to extract only the used javascript from a JS file, using the Coverage Chrome DevTools option. I have exported the coverage json file, then I used a PHP script to extract with substr:
$target_js = 'js/input.js';
$json_string = 'coverage.json';
$output_filename = 'output.js';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata,true);
$output_js = '';
foreach( $obj as $arr ) {
if( strpos( $arr['url'], $target_js ) ) {
foreach ($arr['ranges'] as $name => $value) {
$length = $value['end'] - $value['start'];
$output_js .= substr($arr['text'], $value['start'], $length);
}
break;
}
}
echo $output_js;
file_put_contents($output_filename, $output_js);
Extracting from the JS file the code from "start" to "end" as mentioned in the exported json file, will result a broken js file.
Do you know if there is a bug in DevTools and the returned ranges are not correct? Or maybe other way to extract the JS? Thank you