1 // Copyright 2013 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.
7 * @fileoverview Rendering for iframed most visited thumbnails.
10 window.addEventListener('DOMContentLoaded', function() {
13 fillMostVisited(document.location, function(params, data) {
14 function logEvent(eventName) {
15 chrome.embeddedSearch.newTabPage.logEvent(eventName);
17 function showDomainElement() {
18 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_ERROR);
19 var link = createMostVisitedLink(
20 params, data.url, data.title, undefined, data.ping);
21 var domain = document.createElement('div');
22 domain.textContent = data.domain;
23 link.appendChild(domain);
24 document.body.appendChild(link);
26 function createAndAppendThumbnail(isVisible) {
27 var image = new Image();
28 image.onload = function() {
29 var shadow = document.createElement('span');
30 shadow.classList.add('shadow');
31 var link = createMostVisitedLink(
32 params, data.url, data.title, undefined, data.ping);
33 link.appendChild(shadow);
34 link.appendChild(image);
35 // We add 'position: absolute' in anticipation that there could be more
36 // than one thumbnail. This will superpose the elements.
37 link.style.position = 'absolute';
38 document.body.appendChild(link);
41 image.style.visibility = 'hidden';
45 if (data.thumbnailUrl) {
46 var image = createAndAppendThumbnail(true);
47 // If a backup thumbnail URL was provided, preload it in case the first
48 // thumbnail errors. The backup thumbnail is always preloaded so that the
49 // server can't gain knowledge on the local thumbnail DB by specifying a
50 // second URL that is only sometimes fetched.
51 if (data.thumbnailUrl2) {
52 var image2 = createAndAppendThumbnail(false);
53 var imageFailed = false;
54 var image2Failed = false;
55 image2.onerror = function() {
57 image2.style.visibility = 'hidden';
62 image2.src = data.thumbnailUrl2;
63 // The first thumbnail's onerror function will swap the visibility of
64 // the two thumbnails.
65 image.onerror = function() {
66 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_FALLBACK_THUMBNAIL_USED);
68 image.style.visibility = 'hidden';
72 image2.style.visibility = 'visible';
75 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_FALLBACK_THUMBNAIL_REQUESTED);
77 image.onerror = showDomainElement;
79 image.src = data.thumbnailUrl;
80 logEvent(NTP_LOGGING_EVENT_TYPE.NTP_THUMBNAIL_ATTEMPT);