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 uses 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 conatians 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 ii = 0; ii < images.length; ii++) { | |
Evan Stade
2015/01/28 00:08:24
why ii?
newt (away)
2015/01/28 00:19:51
No idea. Changed it to i.
| |
48 var currentImage = images[ii]; | |
49 var aspectRatio = currentImage.width / currentImage.height; | |
50 if (aspectRatio >= 2.4 || aspectRatio <= 0.5) { | |
Evan Stade
2015/01/28 00:08:24
nit: no curlies
newt (away)
2015/01/28 00:19:51
Done.
| |
51 continue; // Skip weirdly shaped images. Those are ads or headers. | |
52 } | |
53 var pointSize = currentImage.width * currentImage.height; | |
54 if (pointSize > maxPointSize) { | |
55 maxPointSize = pointSize; | |
56 maxImage = currentImage; | |
57 } | |
58 } | |
59 | |
60 // Only keep images larger than 320*160. | |
61 if (maxPointSize <= 51200.0 || maxImage === null) | |
62 return ''; | |
63 | |
64 return jsonEncoder({ | |
65 'imageUrl': maxImage.src, | |
66 'referrerPolicy': referrerPolicy | |
67 }); | |
68 })(); | |
OLD | NEW |