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.
6 * Looks on the page to find the image that could be the most representative
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();
20 // See what to use for JSON. Some pages use a library that overrides JSON.
21 var jsonEncoder = JSON.stringify;
23 jsonEncoder = JSON.encode;
25 // First look if there is an Open Graph Image property available.
26 var ogImage = document.querySelector('meta[property=\"og:image\"]');
28 // Checks that the url in ogImage has a path that contains more than just a
30 var url = ogImage.content;
31 var location = document.createElement('a');
33 if (location.pathname.length > 1) {
36 'referrerPolicy': referrerPolicy
41 // Iterates through the images on the page, find the largest one that doesn't
42 // look like a banner.
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;
59 // Only keep images larger than 320*160.
60 if (maxPointSize <= 51200.0 || maxImage === null)
64 'imageUrl': maxImage.src,
65 'referrerPolicy': referrerPolicy