OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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. |
| 4 (function() { |
| 5 "use strict"; |
| 6 |
| 7 var document = window.document; |
| 8 |
| 9 /** |
| 10 * Returns the URL of the ad image. |
| 11 */ |
| 12 function getAdUrl(index) { |
| 13 var adPath = "ads/image315.png"; |
| 14 var prefixIndex = location.href.lastIndexOf("/"); |
| 15 return location.href.substr(0, prefixIndex) + "/" + adPath; |
| 16 } |
| 17 |
| 18 var messageSequenceNumber = 0; |
| 19 |
| 20 /** |
| 21 * Sends a message to the embedder app. |
| 22 */ |
| 23 function sendAppMessage(source, message, publisherData, messageData) { |
| 24 source.postMessage({ |
| 25 'sequenceNumber': messageSequenceNumber, |
| 26 'message': message, |
| 27 'publisherData': publisherData, |
| 28 'data': messageData, |
| 29 }, "*"); |
| 30 |
| 31 messageSequenceNumber++; |
| 32 } |
| 33 |
| 34 /** |
| 35 * Called when the img for an ad is loaded into its iframe. |
| 36 */ |
| 37 function onAdLoaded(source, publisherData) { |
| 38 // Resize containing iframe to size of image. |
| 39 var iframe = document.getElementById("ad-frame"); |
| 40 var img = iframe.contentWindow.document.getElementsByTagName("IMG")[0]; |
| 41 |
| 42 iframe.style.width = img.width + "px"; |
| 43 iframe.style.height = img.height + "px"; |
| 44 |
| 45 function getCombinedSize(style, propertyNames) { |
| 46 var result = 0; |
| 47 for(var i = 0; i < propertyNames.length; i++) { |
| 48 var value = style.getPropertyValue(propertyNames[i]); |
| 49 result += parseFloat(value.substr(0, value.indexOf("px"))); |
| 50 } |
| 51 return result; |
| 52 } |
| 53 |
| 54 // Size of ad = size of document body + margins. |
| 55 var style = document.defaultView.getComputedStyle(document.body, ''); |
| 56 var adHeight = |
| 57 getCombinedSize(style, ['height', 'margin-top', 'margin-bottom']); |
| 58 var adWidth = |
| 59 getCombinedSize(style, ['width', 'margin-left', 'margin-right']); |
| 60 |
| 61 var adRect = { |
| 62 height: adHeight + "px", |
| 63 width: adWidth + "px", |
| 64 }; |
| 65 |
| 66 sendAppMessage(source, "ad-displayed", publisherData, { |
| 67 'adSize': { |
| 68 width: adRect.width, |
| 69 height: adRect.height |
| 70 }}); |
| 71 |
| 72 img.onclick = function() { |
| 73 sendAppMessage(source, "ad-clicked", publisherData); |
| 74 }; |
| 75 } |
| 76 |
| 77 /** |
| 78 * Displays the ad image in the ad iframe. |
| 79 */ |
| 80 function displayAd(source, publisherData) { |
| 81 // Remove temporary message since we are about to display an add. |
| 82 var tempDiv = document.getElementById("temp-display"); |
| 83 if (tempDiv) { |
| 84 tempDiv.parentNode.removeChild(tempDiv); |
| 85 } |
| 86 |
| 87 // Load image in iframe. |
| 88 var iframe = document.getElementById("ad-frame"); |
| 89 if (iframe.contentWindow.document.body.firstChild == null) { |
| 90 var img = iframe.contentWindow.document.createElement("img"); |
| 91 iframe.contentWindow.document.body.appendChild(img); |
| 92 } |
| 93 var img = iframe.contentWindow.document.body.firstChild; |
| 94 img.onload = function() { onAdLoaded(source, publisherData); }; |
| 95 img.src = getAdUrl(); |
| 96 iframe.style.visibility = "visible"; |
| 97 } |
| 98 |
| 99 /** |
| 100 * Dispatches |appMessage| according to message value. |
| 101 */ |
| 102 function processAppMessage(source, appMessage) { |
| 103 if (appMessage.message == "display-ad") { |
| 104 displayAd(source, appMessage.publisherData); |
| 105 } |
| 106 else if (appMessage.message == "display-first-ad") { |
| 107 displayAd(source, appMessage.publisherData); |
| 108 } |
| 109 else if (appMessage.message == "onloadcommit") { |
| 110 sendAppMessage(source, "onloadcommit-ack"); |
| 111 } |
| 112 } |
| 113 |
| 114 /** |
| 115 * Handles "message" event. |
| 116 */ |
| 117 function onPostMessage(eventMessage) { |
| 118 processAppMessage(eventMessage.source, eventMessage.data); |
| 119 } |
| 120 |
| 121 /** |
| 122 * Handles "DOMContentLoaded" event. |
| 123 */ |
| 124 function onDocumentReady() { |
| 125 document.getElementById("url").textContent = window.location; |
| 126 } |
| 127 |
| 128 // |
| 129 // Register global event listeners. |
| 130 // |
| 131 window.addEventListener("message", onPostMessage, false); |
| 132 document.addEventListener('DOMContentLoaded', onDocumentReady, false); |
| 133 |
| 134 })(); |
OLD | NEW |