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

Unified 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: thakis's nits 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/browser_resources.grd ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/resources/get_salient_image_url.js
diff --git a/chrome/browser/resources/get_salient_image_url.js b/chrome/browser/resources/get_salient_image_url.js
new file mode 100644
index 0000000000000000000000000000000000000000..cb3020e6d5dba0512691e2d50e1dae2df87cc719
--- /dev/null
+++ b/chrome/browser/resources/get_salient_image_url.js
@@ -0,0 +1,67 @@
+// 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
+ });
+})();
« 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