| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /* | |
| 6 * Looks on the page to find the image that could be the most representative | |
| 7 * one. | |
| 8 */ | |
| 9 (function() { | |
| 10 // Extract the referrer policy from the page. | |
| 11 var referrerPolicy = 'default'; | |
| 12 var metaTags = document.getElementsByTagName('meta'); | |
| 13 for (var i = 0; i < metaTags.length; ++i) { | |
| 14 if (metaTags[i].name.toLowerCase() == 'referrer') { | |
| 15 referrerPolicy = metaTags[i].content.toLowerCase(); | |
| 16 break; | |
| 17 } | |
| 18 } | |
| 19 | |
| 20 // See what to use for JSON. Some pages use a library that overrides JSON. | |
| 21 var jsonEncoder = JSON.stringify; | |
| 22 if (!jsonEncoder) | |
| 23 jsonEncoder = JSON.encode; | |
| 24 | |
| 25 // First look if there is an Open Graph Image property available. | |
| 26 var ogImage = document.querySelector('meta[property=\"og:image\"]'); | |
| 27 if (ogImage) { | |
| 28 // Checks that the url in ogImage has a path that contains more than just a | |
| 29 // simple '/'. | |
| 30 var url = ogImage.content; | |
| 31 var location = document.createElement('a'); | |
| 32 location.href = url; | |
| 33 if (location.pathname.length > 1) { | |
| 34 return jsonEncoder({ | |
| 35 'imageUrl': url, | |
| 36 'referrerPolicy': referrerPolicy | |
| 37 }); | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 // Iterates through the images on the page, find the largest one that doesn't | |
| 42 // look like a banner. | |
| 43 var maxPointSize = 0; | |
| 44 var maxImage = null; | |
| 45 | |
| 46 var images = document.getElementsByTagName('img'); | |
| 47 for (var i = 0; i < images.length; i++) { | |
| 48 var currentImage = images[i]; | |
| 49 var aspectRatio = currentImage.width / currentImage.height; | |
| 50 if (aspectRatio >= 2.4 || aspectRatio <= 0.5) | |
| 51 continue; // Skip weirdly shaped images. Those are ads or headers. | |
| 52 var pointSize = currentImage.width * currentImage.height; | |
| 53 if (pointSize > maxPointSize) { | |
| 54 maxPointSize = pointSize; | |
| 55 maxImage = currentImage; | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 // Only keep images larger than 320*160. | |
| 60 if (maxPointSize <= 51200.0 || maxImage === null) | |
| 61 return ''; | |
| 62 | |
| 63 return jsonEncoder({ | |
| 64 'imageUrl': maxImage.src, | |
| 65 'referrerPolicy': referrerPolicy | |
| 66 }); | |
| 67 })(); | |
| OLD | NEW |