1 // Copyright 2014 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.
5 * Looks on the page to find the image that could be the most representative
9 // Extract the referrer policy from the page.
10 var referrerPolicy = "";
11 if (window.__gCrWeb && __gCrWeb['getPageReferrerPolicy']) {
12 referrerPolicy = __gCrWeb['getPageReferrerPolicy'];
15 // See what to use for JSON. Some pages uses a library that overrides JSON.
16 var jsonEncoder = JSON.stringify;
18 jsonEncoder = JSON.encode;
20 // First look if there is an Open Graph Image property available.
21 var ogImage = document.querySelector('meta[property=\"og:image\"]');
23 // Checks that the url in ogImage has a path that conatians more than just a
25 var url = ogImage.content;
26 var location = document.createElement("a");
28 if (location.pathname.length > 1) {
31 'referrerPolicy': referrerPolicy
36 // Iterates through the images on the page, find the largest one that doesn't
37 // look like a banner.
41 var images = document.getElementsByTagName('img');
42 for (var ii = 0; ii < images.length; ii++) {
43 var currentImage = images[ii];
44 var aspectRatio = currentImage.width / currentImage.height;
45 if (aspectRatio >= 2.4 || aspectRatio <= 0.5) {
46 continue; // Skip weirdly shaped images. Those are ads or headers.
48 var pointSize = currentImage.width * currentImage.height;
49 if (pointSize > maxPointSize) {
50 maxPointSize = pointSize;
51 maxImage = currentImage;
55 // Only keep images larger than 320*160.
56 if (maxPointSize <= 51200.0 || maxImage === null)
60 'imageUrl': maxImage.src,
61 'referrerPolicy': referrerPolicy