Find all JPG/PNG ‘img src’ links in an HTML response
var msg = $context.currentItem.previousItem;
var srcs = msg.getResponse(msg.RESPONSE_TEXT_AS_HTML, "//@src");
var response = msg.getResponse();
// $context.result.postMessage($context.result.LEVEL_INFO, "response: " +response);
// count the number of ‘<img src=’ matches are in the response
var count_matches = response.split("<img src=").length-1;
$context.result.postMessage($context.result.LEVEL_INFO, ("<img src matches: " +count_matches));
// split the array each time '<img src="' is found
var array_of_images = response.split("<img src=\"");
//since the first element of the array (prior to the first img src) is not interesting, delete it from the array
// array_name.splice(indexNumber,numberOfElementsToRemove
array_of_images.splice(0, 1);
var array_of_images_cleaned = new Array();
for (x in array_of_images)
{
// line below shows each element of array
// $context.result.postMessage($context.result.LEVEL_INFO, "img src" + x+ ": " +array_of_images[x]);
// string to find; hard coded
var stringToFind = ".png";
// Determine if the value exists in the response.
var searchIndex = array_of_images[x].indexOf(stringToFind);
// Determine if the value exists in the response. A -1 Index indicates the
// value was not found in the response. A positive integer indicates where
// in the response the value was found.
if (searchIndex == -1)
{
// $context.result.postMessage($context.result.LEVEL_INFO, ".png not found ");
// string not found, so look for a .jpg
// string to find; hard coded
var stringToFind = ".jpg";
// Determine if the value exists in the response.
var searchIndex = array_of_images[x].indexOf(stringToFind);
if (searchIndex == -1)
{
// string not found
$context.result.postMessage($context.result.LEVEL_INFO, ".jpg not found either ");
}
// If the value is found, output a confirmation
else {
//$context.result.postMessage($context.result.LEVEL_INFO, ".jpg found");
//$context.result.postMessage($context.result.LEVEL_INFO, "searchIndex: "+searchIndex);
final_url = array_of_images[x].substring(0, searchIndex+4);
//$context.result.postMessage($context.result.LEVEL_INFO, "final_url: "+final_url);
array_of_images_cleaned.push(final_url);
}
}
// If the value is found, output a confirmation
else {
// $context.result.postMessage($context.result.LEVEL_INFO, ".png found");
// $context.result.postMessage($context.result.LEVEL_INFO, "searchIndex: "+searchIndex);
final_url = array_of_images[x].substring(0, searchIndex+4);
//$context.result.postMessage($context.result.LEVEL_INFO, "final_url: "+final_url);
array_of_images_cleaned.push(final_url);
}
}
$context.result.postMessage($context.result.LEVEL_INFO, "length of array_of_images_cleaned: "+array_of_images_cleaned.length);
// remove duplicates in array by creating a new array
var r = new Array();
o:for(var i = 0, n = array_of_images_cleaned.length; i < n; i++) {
for(var x = i + 1 ; x < n; x++)
{
if(array_of_images_cleaned[x]==array_of_images_cleaned[i]) continue o;
}
r[r.length] = array_of_images_cleaned[i];
}
var count_matches = r.length;
$context.result.postMessage($context.result.LEVEL_INFO, "<img src matches: " +count_matches);
for (x in r)
{
$context.result.postMessage($context.result.LEVEL_INFO, "img src" + x+ ": " +r[x]);
}