Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 (function( window ) { | |
| 2 "use strict"; | |
| 3 | |
| 4 var document = window.document; | |
| 5 | |
| 6 var ad_urls = [ | |
| 7 'ads/image315.png', | |
| 8 'ads/image316.png', | |
| 9 'ads/image317.png', | |
| 10 'ads/image318.png', | |
| 11 'ads/image319.png', | |
| 12 ]; | |
| 13 | |
| 14 // Pick a random ad url | |
| 15 function getAdUrl(index) { | |
| 16 if (typeof index === "undefined") | |
| 17 index = Math.floor(Math.random() * ad_urls.length); | |
| 18 var ad_path = ad_urls[index]; | |
| 19 var prefix_index = location.href.lastIndexOf("/"); | |
| 20 return location.href.substr(0, prefix_index) + "/" + ad_path; | |
|
asargent_no_longer_on_chrome
2013/03/11 19:10:14
FYI, we are usually careful about using randomness
rpaquay
2013/03/12 19:56:48
We actually don't need the randomness for tests, s
| |
| 21 } | |
| 22 | |
| 23 // | |
| 24 // Send a message to the embedding app | |
| 25 // | |
| 26 var message_sequence_number = 0; | |
| 27 | |
| 28 function sendAppMessage(source, message, publisher_data, message_data) { | |
| 29 source.postMessage({ | |
| 30 'sequence_number': message_sequence_number, | |
| 31 'message': message, | |
| 32 'publisher_data': publisher_data, | |
| 33 'data': message_data, | |
| 34 }, "*"); | |
| 35 | |
| 36 message_sequence_number++; | |
| 37 } | |
| 38 | |
| 39 // | |
| 40 // When the img for an ad is loaded into its iframe | |
| 41 // | |
| 42 function onAdLoaded(source, publisher_data) { | |
| 43 // Resize containing iframe to size of image | |
| 44 var iframe = document.getElementById("ad-frame"); | |
| 45 var img = iframe.contentWindow.document.getElementsByTagName("IMG")[0]; | |
| 46 | |
| 47 iframe.style.width = img.width + "px"; | |
| 48 iframe.style.height = img.height + "px"; | |
| 49 | |
| 50 // Size of ad = size of document body + margins | |
| 51 function sint(value) { | |
|
asargent_no_longer_on_chrome
2013/03/11 19:10:14
nits: can you use a more meaningful function name
rpaquay
2013/03/12 19:56:48
Done.
| |
| 52 return parseFloat(value.substr(0, value.indexOf("px"))); | |
| 53 } | |
| 54 | |
| 55 var style = document.defaultView.getComputedStyle(document.body, ''); | |
| 56 var elemHeight = sint(style.getPropertyValue('height')); | |
| 57 var elemVerticalMargin = | |
| 58 sint(style.getPropertyValue('margin-top')) + | |
| 59 sint(style.getPropertyValue('margin-bottom')); | |
| 60 | |
| 61 var elemWidth = sint(style.getPropertyValue('width')); | |
| 62 var elemHorizontalMargin = | |
| 63 sint(style.getPropertyValue('margin-left')) + | |
| 64 sint(style.getPropertyValue('margin-right')); | |
| 65 | |
| 66 var adRect = { | |
| 67 height: (elemHeight + elemVerticalMargin) + "px", | |
| 68 width: (elemWidth + elemHorizontalMargin) + "px", | |
| 69 }; | |
| 70 | |
| 71 sendAppMessage(source, "ad-displayed", publisher_data, { | |
| 72 'ad_size': { | |
| 73 width: adRect.width, | |
| 74 height: adRect.height | |
| 75 }}); | |
| 76 | |
| 77 img.onclick = function() { | |
| 78 sendAppMessage(source, "ad-clicked", publisher_data); | |
| 79 }; | |
| 80 } | |
| 81 | |
| 82 // | |
| 83 // display a random ad in the ad iframe | |
| 84 // | |
| 85 function displayAd(source, publisher_data, ad_index) { | |
| 86 // Remove temporary message since we are about to display an add | |
| 87 var tempDiv = document.getElementById("temp-display"); | |
| 88 if (tempDiv) { | |
| 89 tempDiv.parentNode.removeChild(tempDiv); | |
| 90 } | |
| 91 | |
| 92 // Load image in iframe | |
| 93 var iframe = document.getElementById("ad-frame"); | |
| 94 if (iframe.contentWindow.document.body.firstChild == null) { | |
| 95 var img = iframe.contentWindow.document.createElement("img"); | |
| 96 iframe.contentWindow.document.body.appendChild(img); | |
| 97 } | |
| 98 var img = iframe.contentWindow.document.body.firstChild; | |
| 99 img.onload = function() { onAdLoaded(source, publisher_data); }; | |
| 100 img.src = getAdUrl(ad_index); | |
| 101 iframe.style.visibility = "visible"; | |
| 102 } | |
| 103 | |
| 104 function processAppMessage(source, data) { | |
| 105 if (data.message == "display-ad") { | |
| 106 displayAd(source, data.publisher_data); | |
| 107 } | |
| 108 else if (data.message == "display-first-ad") { | |
| 109 displayAd(source, data.publisher_data, 0/*ad_index*/); | |
| 110 } | |
| 111 else if (data.message == "onloadcommit") { | |
| 112 sendAppMessage(source, "onloadcommit-ack"); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 function onPostMessage(eventMessage) { | |
| 117 processAppMessage(eventMessage.source, eventMessage.data); | |
| 118 } | |
| 119 | |
| 120 function onDocumentReady() { | |
| 121 document.getElementById("url").textContent = window.location; | |
| 122 } | |
| 123 | |
| 124 // | |
| 125 // Add global event listeners. | |
| 126 // | |
| 127 window.addEventListener("message", onPostMessage, false); | |
| 128 document.addEventListener('DOMContentLoaded', onDocumentReady, false); | |
| 129 | |
| 130 })(window); | |
| OLD | NEW |