1// Google AdSense 广åå è½½å管ç 2// ç»ä¸å¤çææé¡µé¢çå¹¿åæ¾ç¤º 3 4(function() { 5 'use strict'; 6 7 // æ£æ¥é ç½®æ¯å¦åå¨ 8 if (typeof window.AdSenseConfig === 'undefined') { 9 console.warn('AdSenseConfig not found, ads will not be loaded'); 10 return; 11 } 12 13 const config = window.AdSenseConfig; 14 15 // å¦æå¹¿åæªå¯ç¨,ç´æ¥è¿å 16 if (!config.enabled) { 17 console.log('AdSense is disabled in config'); 18 return; 19 } 20 21 // æ£æ¥æ¯å¦å¨ç§»å¨è®¾å¤ä¸ 22 function isMobile() { 23 return window.innerWidth <= config.breakpoints.mobile; 24 } 25 26 // å è½½ AdSense èæ¬ 27 function loadAdSenseScript() { 28 // æ£æ¥æ¯å¦å·²ç»å è½½ 29 if (document.querySelector('script[src*="adsbygoogle"]')) { 30 return; 31 } 32 33 const script = document.createElement('script'); 34 script.src = `https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=${config.publisherId}`; 35 script.async = true; 36 script.crossOrigin = 'anonymous'; 37 script.onerror = function() { 38 console.error('Failed to load AdSense script'); 39 }; 40 document.head.appendChild(script); 41 } 42 43 // å建广ååå 44 function createAdUnit(slotConfig, containerId) { 45 if (!slotConfig.enabled) { 46 return; 47 } 48 49 const container = document.getElementById(containerId); 50 if (!container) { 51 console.warn(`Ad container ${containerId} not found`); 52 return; 53 } 54 55 // å建广å ins å ç´ 56 const adElement = document.createElement('ins'); 57 adElement.className = 'adsbygoogle'; 58 adElement.style.display = 'block'; 59 60 // åºç¨æ ·å¼ 61 if (slotConfig.style) { 62 Object.assign(adElement.style, slotConfig.style); 63 } 64 65 // 设置 AdSense 屿§ 66 adElement.setAttribute('data-ad-client', config.publisherId); 67 adElement.setAttribute('data-ad-slot', slotConfig.slotId); 68 adElement.setAttribute('data-ad-format', slotConfig.format || 'auto'); 69 70 if (slotConfig.responsive) { 71 adElement.setAttribute('data-full-width-responsive', 'true'); 72 } 73 74 // æ·»å å°å®¹å¨ 75 container.appendChild(adElement); 76 77 // å»¶è¿æ¨é广å请æ±,ç¡®ä¿èæ¬å·²å è½½ 78 setTimeout(function() { 79 try { 80 if (window.adsbygoogle && window.adsbygoogle.loaded !== true) { 81 (window.adsbygoogle = window.adsbygoogle || []).push({}); 82 } 83 } catch (e) { 84 console.error('Error pushing ad to adsbygoogle:', e); 85 } 86 }, 100); 87 } 88 89 // åå§åææå¹¿å 90 function initAds() { 91 // å è½½ AdSense èæ¬ 92 loadAdSenseScript(); 93 94 // çå¾ èæ¬å è½½å®æååå建广ååå 95 const checkAndInit = setInterval(function() { 96 if (window.adsbygoogle) { 97 clearInterval(checkAndInit); 98 99 // å建å个广åä½ï¼ä» å·¦å³ä¸¤ä¾§ï¼ 100 if (config.adSlots.leftSidebar && !isMobile()) { 101 createAdUnit(config.adSlots.leftSidebar, 'adsense-left-sidebar'); 102 } 103 104 if (config.adSlots.rightSidebar && !isMobile()) { 105 createAdUnit(config.adSlots.rightSidebar, 'adsense-right-sidebar'); 106 } 107 } 108 }, 100); 109 110 // è¶ æ¶ä¿æ¤ (10ç§ååæ¢æ£æ¥) 111 setTimeout(function() { 112 clearInterval(checkAndInit); 113 }, 10000); 114 } 115 116 // DOM å è½½å®æååå§å 117 if (document.readyState === 'loading') { 118 document.addEventListener('DOMContentLoaded', initAds); 119 } else { 120 initAds(); 121 } 122 123 // 导åºå½æ°ä¾å¤é¨è°ç¨ 124 window.initAdSense = initAds; 125 window.createAdUnit = createAdUnit; 126 127})(); 128
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.