1/* 2 * This script checks when an ad is visible within the viewport and loads it. 3 * It only checks for ads that contain the CSS class ".gpt-lazy-load" 4 */ 5(function (win) { 6 if (typeof readAdmiralCookie === "function") { 7 admiral_status = readAdmiralCookie(); 8 } 9 if (typeof getAdmiral === "function") { 10 admiral_status = getAdmiral("admiral_status"); 11 } 12 if ('undefined' === typeof admiral_status || true !== admiral_status) { 13 'use strict'; 14 var adsLoadedCount = 0, 15 PB = win.PB, 16 SidebarLoader = PB.SidebarLoader, 17 waitForSidebar = PB.gptWaitSidebarLoaded, 18 // Google Publisher Tag API object 19 // https://support.google.com/dfp_premium/answer/1650154?hl=en 20 googletag = win.googletag, 21 adsInViewList = document.querySelectorAll('.js-inview'); 22 23 /** 24 * Checks if an element is in the viewport 25 * @param {HTMLElement} element 26 * @return {boolean} True if the element is in the viewport 27 */ 28 function isInViewport(element) { 29 const rect = element.getBoundingClientRect(); 30 return (rect.top >= 0 && rect.left >= 0 && 31 rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && 32 rect.right <= (window.innerWidth || document.documentElement.clientWidth)); 33 } 34 35 /** 36 * Handles the visibility check for ads 37 */ 38 function inViewHandler() { 39 const inViewElements = document.querySelectorAll('.js-inview'); 40 41 inViewElements.forEach(function (adElement) { 42 const adId = adElement.getAttribute('id'); 43 if (isInViewport(adElement)) { 44 if (PB.gptAds[adId] && !PB.gptAds[adId].pbLoaded) { 45 console.info(adId); 46 // Mark ad as loaded 47 adsLoadedCount++; 48 PB.gptAds[adId].pbLoaded = true; 49 50 // Ask Google to load the ad 51 googletag.display(adId); 52 53 if (amazon_uam !== true) { 54 googletag.pubads().refresh([PB.gptAds[adId]]); 55 } 56 57 // If all the ads have been loaded, remove event listeners 58 if (adsLoadedCount === getTotalInviewAds()) { 59 window.removeEventListener('scroll', inViewHandler); 60 } 61 } 62 } 63 }); 64 } 65 66 /** 67 * Initializes the scroll event listener 68 */ 69 function initialize() { 70 window.addEventListener('scroll', inViewHandler); 71 inViewHandler(); 72 } 73 74 /** 75 * Returns the total number of ads that use the "js-inview" class 76 * @return {number} Total in-view ads 77 */ 78 function getTotalInviewAds() { 79 let totalElements = 0;
80 Object.getOwnPropertyNames(PB.gptAds).forEach(function (currentAdId) { 81 const adElement = document.getElementById(currentAdId); 82 if (adElement && adElement.classList.contains('js-inview')) { 83 totalElements++; 84 } 85 }); 86 return totalElements; 87 } 88 89 // Execute when the DOM is ready 90 document.addEventListener("DOMContentLoaded", function () { 91 if (SidebarLoader && waitForSidebar) { 92 // Wait for all the sidebars to be loaded 93 SidebarLoader.onLoad().then(function () { 94 // Load all the ads within the viewport 95 initialize(); 96 }); 97 } else { 98 initialize(); 99 } 100 }); 101 } 102})(window);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.