Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I extract the largest image (by dimension) on a site given the URL?

For instance, if I input: http://www.google.com/

It would return: http://www.google.com/images/logos/ps_logo2.png

Using javascript/jquery. These sites would all be external. Thank you!

like image 943
jprim Avatar asked Sep 16 '10 07:09

jprim


People also ask

How do I find out the size of an image?

You can also right-click on an image & choose properties from the drop-down menu. A new window will appear with several tabs. You'll click the details tab, and there you'll find you image size and dimensions.

How do I know the pixel size of an image online?

Right-click on the image and then select "Properties." A window will appear with the image's details. Go to the "Details" tab to see the image's dimensions and resolution.


1 Answers

Since that is a Google Chrome extension, you are not bound to same origin policy.

Basically, you would need content scripts to fetch all the images within a page, and check each image's size within the DOM to know if its larger the last fetched image.

You can use Message Passing, to communicate from the Content Script to the popup/background page.

For example, I will show you how to get the largest image from a page and show it within the popup. We use all the techniques shown above and you will see the largest image within the popup if you activate it. (should show I believe :))

manifest.json (snippet)

 ...

 "permissions": [
   "tabs",
   "http://*/*"
 ],
  "content_scripts": [
  {
    "matches": ["http://*/*"],
    "js": ["images.js"],
    "run_at": "document_start",
    "all_frames": true
  }
 ]

...

popup.html

<!DOCTYPE html> 
<html>
<head>
<script>
function getImage() {
  chrome.tabs.getSelected(null, function(tab) {
    chrome.tabs.sendRequest(tab.id, {method: "getImage"}, function (response) {
      var text = document.getElementById('image'); 
      var image = response.data;
      text.src = image ? response.data : 'no_image.gif';
    });
  });
}
</script>
</head>
<body>
<button onclick="getImage(); ">Get Largest Image</button>
<img src="no_image.gif" id="image"/>
</body>
</html>

images.js (content script)

function getMaxImage() {
  var maxDimension = 0;
  var maxImage = null;

  // Iterate through all the images.
  var imgElements = document.getElementsByTagName('img');
  for (var index in imgElements) {
    var img = imgElements[index];
    var currDimension = img.width * img.height;
    if (currDimension  > maxDimension){
       maxDimension = currDimension
       maxImage = img;
    }
  }
  // Check if an image has been found.
  if (maxImage) 
    return maxImage.src;
  else
    return null;
}

// Listen for extension requests.
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
  if (request.method == "getImage") {
    sendResponse({data: getMaxImage()});
  } else {
    sendResponse({}); // snub them.
  }
});
like image 189
Mohamed Mansour Avatar answered Oct 05 '22 16:10

Mohamed Mansour