PageSourceSearch

https://poe2lens.com/js/adsense.js

js poe2lens.com collected 2026-10-02 01:40:40 UTC 4,077 bytes, 128 lines download raw bytes

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.