balazs
2/6/2019 - 8:47 PM

Checking big images (by file size)

/****************************************************************************************************************
IMPORTANT:  You'll need to install the xmlhttprequest package(if using this script outside a browser):

> npm init
> npm install xmlhttprequest --save-dev
*****************************************************************************************************************/
 * var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

var images = ["https://appliedapartments.com/wp-content/themes/appliedapartments/library/images/logo.png","https://appliedapartments.com/wp-content/themes/appliedapartments/library/images/icon-menu.png","https://appliedapartments.com/wp-content/themes/appliedapartments/library/images/icon-search.png","https://appliedapartments.com/wp-content/themes/appliedapartments/library/images/search-dropdown-arrows.png","https://appliedapartments.com/wp-content/themes/appliedapartments/library/images/search-dropdown-arrows.png","https://appliedapartments.com/wp-content/themes/appliedapartments/library/images/search-dropdown-arrows.png","https://appliedapartments.com/wp-content/themes/appliedapartments/library/images/search-dropdown-arrows.png","https://appliedapartments.com/wp-content/themes/appliedapartments/library/images/icon-search.png","https://appliedapartments.com/wp-content/uploads/2016/04/111107_EJ_untitled_0115-Edit-2560x1707.jpg","https://appliedapartments.com/wp-content/uploads/2016/04/333RiverStreet_DSC_8435-e1487778624792.jpg","https://appliedapartments.com/wp-content/uploads/2016/04/207_living_room_b_052.jpg","https://appliedapartments.com/wp-content/uploads/2016/04/333RiverStreet_DSC_4298-e1487780022158.jpg","https://appliedapartments.com/wp-content/uploads/2016/04/70Columbus_DSC_7697-e1487780050252.jpg","https://appliedapartments.com/wp-content/themes/appliedapartments/library/images/search-dropdown-arrows.png","https://appliedapartments.com/wp-content/themes/appliedapartments/library/images/search-dropdown-arrows.png","https://appliedapartments.com/wp-content/themes/appliedapartments/library/images/search-dropdown-arrows.png","https://appliedapartments.com/wp-content/themes/appliedapartments/library/images/search-dropdown-arrows.png","https://appliedapartments.com/wp-content/themes/appliedapartments/library/images/search-dropdown-arrows.png","https://appliedapartments.com/wp-content/themes/appliedapartments/library/images/search-dropdown-arrows.png","https://appliedapartments.com/wp-content/themes/appliedapartments/library/images/search-dropdown-arrows.png","https://appliedapartments.com/wp-content/themes/appliedapartments/library/images/search-dropdown-arrows.png","https://appliedapartments.com/wp-content/uploads/2016/10/building-01.jpg","https://appliedapartments.com/wp-content/uploads/2016/10/building-01.jpg","https://appliedapartments.com/wp-content/uploads/2017/02/333RiverStreet_DSC_4288_DxOVP-e1487795394312.jpg","https://appliedapartments.com/wp-content/uploads/2017/02/333RiverStreet_DSC_4288_DxOVP-e1487795394312.jpg","https://appliedapartments.com/wp-content/uploads/2017/02/FilipWolak-Harrison-0528-e1487882713419.jpg","https://appliedapartments.com/wp-content/uploads/2017/02/FilipWolak-Harrison-0528-e1487882713419.jpg","https://appliedapartments.com/wp-content/uploads/2017/03/DSC_3815.jpg","https://appliedapartments.com/wp-content/uploads/2016/04/Stocksy_txp51869732uIO100_Small_906465-e1488396992214.jpg","https://appliedapartments.com/wp-content/uploads/2016/04/Stocksy_txp51869732uIO100_Small_592320-e1488397027548.jpg","https://appliedapartments.com/wp-content/uploads/2016/04/Stocksy_txp51869732uIO100_Small_221354-e1488397077307.jpg","https://appliedapartments.com/wp-content/themes/appliedapartments/library/images/footer-house-logo.png"];

// console.log(JSON.stringify(images, undefined, 2));

var promises = [];
var big_images = [];

images.map((url) => {
    var promise = new Promise((resolve, reject) => {
      var xhr = new XMLHttpRequest();
      xhr.open('HEAD', url, true);
      xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
          if (xhr.status == 200) {
            var isValid = xhr.getResponseHeader('Content-Length') < 1000000;
            if (isValid) {
                resolve(url);
            } else {
              big_images.push(url);
              resolve(url);
            }
          }
        }
      };
      xhr.send();
    });

    promises.push(promise);
});

/******************************************************************************
Damn It!!! I learned something new!
Unfortunately Promise.all only resolves when all promises are resolved,
so if any of the promises fails, the catch() is called instead of then()

SO THAT'S WHY THIS BELOW IT'S NOT WORKING UNLESS ALL IMAGE URLs ARE RESOLVED
******************************************************************************/
/*
Promise.all(promises)
.then((value) => { return true; })
.catch((e) => { return false; });
*/

/******************************************************************************
// Here's a trick to make Promise.all() work even when there are rejected promises

// More about this hack here: https://davidwalsh.name/promises-results

// UNFORTUNATELY THIS IS NOT WORKING ON GhostInspector
// it throws an error like this one: 'p.catch' it's not a function
******************************************************************************/
/*
var areAllImagesOk = Promise.all(
  promises.map(p => p.catch(() => undefined))
).then((values) => {
  // removing the 'undefined' values
  var bigImages = values.filter(value => typeof value !== 'undefined');

  console.log(bigImages);
  console.log('Are there big images: ', bigImages.length > 0);

  return bigImages.length == 0;
});
*/

var areAllImagesOk = Promise.all(promises).then(() => {
  console.log('Are there big images: ', big_images.length > 0);
  console.log(JSON.stringify(big_images, undefined, 2));

  var result = big_images.length === 0;
  console.log('All Good (test passes):', result);

  return result;
});