Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: chrome/browser/resources/get_salient_image_url.js

Issue 885513002: Upstream script to get "salient" image from webpage. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/browser_resources.grd ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 })();
OLDNEW
« no previous file with comments | « chrome/browser/browser_resources.grd ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698