Get the largest image on a page

From Chickenfoot Script Repository

// ==UserScript==
// @name Get the largest image on a page
// @when Pages Match
// @description gets the largest image on a page
// @include *
// ==/UserScript==

//Here is a function that returns the IMG node on the page with the largest area:

 /**
  * Returns the IMG in the document with the largest area
  * or undefined if the document has no images.
  * If two IMGs have the same area, then the first one
  * in the DOM tree will be returned
  */
 function getLargestImage(document) {
   var images = document.images;
   if (images.length == 0) return null;
   var area = -1;
   var largestImg = null;
   for (var i = 0; i < images.length; ++i) {
     var img = images[i];
     var box = document.getBoxObjectFor(img);
     var a = box.width * box.height;
     if (a > area) {
       largestImg = img;
       area = a;
     }
   }
   return largestImg;
 }
 
//Here is an example of how you could use <code>getLargestImage()</code> to get //the Google logo and insert it at the top of the current page:
 
 // grab the Google logo of the day
 var src;
 with (fetch('http://www.google.com/')) {
   src = getLargestImage(document).src;
 }
 
 // insert the logo at the top of your page
 var img = document.createElement('IMG');
 img.src = src;
 document.body.insertBefore(img, document.body.firstChild);

//Or use it as a trigger to display your own logo on Google:

 // install as a trigger on http://www.google.com/*
 replace(getLargestImage(document), '<img src="http://www.bolinfest.com/ninja.png">');