Commit 189f9ba3 authored by newt's avatar newt Committed by Commit bot

Upstream script to get "salient" image from webpage.

This is used by enhanced bookmarks to choose an image to show for each
bookmark.

BUG=450287

Review URL: https://codereview.chromium.org/885513002

Cr-Commit-Position: refs/heads/master@{#313440}
parent 3cfbb63e
......@@ -149,6 +149,9 @@
<include name="IDR_FEEDBACK_MANIFEST" file="resources\feedback\manifest.json" type="BINDATA" />
<include name="IDR_FLAGS_HTML" file="resources\flags.html" flattenhtml="true" type="BINDATA" />
<include name="IDR_FLAGS_JS" file="resources\flags.js" type="BINDATA" />
<if expr="is_android">
<include name="IDR_GET_SALIENT_IMAGE_URL_JS" file="resources\get_salient_image_url.js" type="BINDATA" />
</if>
<if expr="_google_chrome or enable_hangout_services_extension">
<!-- Hangout Services extension, included in Google Chrome builds only. -->
<include name="IDR_HANGOUT_SERVICES_MANIFEST" file="resources\hangout_services\manifest.json" type="BINDATA" />
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*
* Looks on the page to find the image that could be the most representative
* one.
*/
(function() {
// Extract the referrer policy from the page.
var referrerPolicy = 'default';
var metaTags = document.getElementsByTagName('meta');
for (var i = 0; i < metaTags.length; ++i) {
if (metaTags[i].name.toLowerCase() == 'referrer') {
referrerPolicy = metaTags[i].content.toLowerCase();
break;
}
}
// See what to use for JSON. Some pages use a library that overrides JSON.
var jsonEncoder = JSON.stringify;
if (!jsonEncoder)
jsonEncoder = JSON.encode;
// First look if there is an Open Graph Image property available.
var ogImage = document.querySelector('meta[property=\"og:image\"]');
if (ogImage) {
// Checks that the url in ogImage has a path that contains more than just a
// simple '/'.
var url = ogImage.content;
var location = document.createElement('a');
location.href = url;
if (location.pathname.length > 1) {
return jsonEncoder({
'imageUrl': url,
'referrerPolicy': referrerPolicy
});
}
}
// Iterates through the images on the page, find the largest one that doesn't
// look like a banner.
var maxPointSize = 0;
var maxImage = null;
var images = document.getElementsByTagName('img');
for (var i = 0; i < images.length; i++) {
var currentImage = images[i];
var aspectRatio = currentImage.width / currentImage.height;
if (aspectRatio >= 2.4 || aspectRatio <= 0.5)
continue; // Skip weirdly shaped images. Those are ads or headers.
var pointSize = currentImage.width * currentImage.height;
if (pointSize > maxPointSize) {
maxPointSize = pointSize;
maxImage = currentImage;
}
}
// Only keep images larger than 320*160.
if (maxPointSize <= 51200.0 || maxImage === null)
return '';
return jsonEncoder({
'imageUrl': maxImage.src,
'referrerPolicy': referrerPolicy
});
})();
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment