qb-pc-sdk 1.3.1 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "qb-pc-sdk",
3
- "version": "1.3.1",
4
- "description": "pc广告 SDK 封装器 - 非windows不加载广告",
3
+ "version": "1.3.2",
4
+ "description": "pc广告 SDK 封装器 - 新增懒加载模式",
5
5
  "main": "core.min.js",
6
6
  "files": [
7
7
  "core.min.js",
@@ -663,6 +663,66 @@
663
663
 
664
664
  return true;
665
665
  }
666
+
667
+ var lazyAdObserver = null;
668
+ function loadLazyAd(element) {
669
+ if (element._qbAdInitialized) return;
670
+ var appId = element.getAttribute('data-app-id');
671
+ var placementId = element.getAttribute('data-ad-id') || element.getAttribute('data-placement-id');
672
+ if (!appId || !placementId) return;
673
+ element._qbAdInitialized = true;
674
+ element.innerHTML = '';
675
+ try {
676
+ var adSDKInstance = new window.AdSDK({
677
+ appId: appId,
678
+ placementId: placementId,
679
+ container: element,
680
+ onAdLoaded: function(ad, instance) {
681
+ if (window.qbAds._onAdLoaded && typeof window.qbAds._onAdLoaded === 'function') {
682
+ window.qbAds._onAdLoaded.call(this, ad, instance, element);
683
+ }
684
+ },
685
+ onAdError: function(err, msg) {
686
+ if (window.qbAds._onAdError && typeof window.qbAds._onAdError === 'function') {
687
+ window.qbAds._onAdError.call(this, err, msg, element);
688
+ }
689
+ },
690
+ onAdExpose: function() {
691
+ if (window.qbAds._onAdExpose && typeof window.qbAds._onAdExpose === 'function') {
692
+ window.qbAds._onAdExpose.call(this, element);
693
+ }
694
+ },
695
+ onAdClick: function() {
696
+ if (window.qbAds._onAdClick && typeof window.qbAds._onAdClick === 'function') {
697
+ window.qbAds._onAdClick.call(this, element);
698
+ }
699
+ }
700
+ });
701
+ window._qbAdsInstances.push({ element: element, instance: adSDKInstance });
702
+ } catch (error) {
703
+ element._qbAdInitialized = false;
704
+ }
705
+ }
706
+ function startLazyAdObserver() {
707
+ if (!window.AdSDK || !window.GDTAdSDK) return;
708
+ var lazyEls = window.document.querySelectorAll('.lazy-ad');
709
+ if (lazyEls.length === 0) return;
710
+ if (!lazyAdObserver) {
711
+ lazyAdObserver = new window.IntersectionObserver(function(entries) {
712
+ entries.forEach(function(entry) {
713
+ if (!entry.isIntersecting) return;
714
+ var el = entry.target;
715
+ loadLazyAd(el);
716
+ lazyAdObserver.unobserve(el);
717
+ });
718
+ }, { root: null, rootMargin: '200px', threshold: 0 });
719
+ }
720
+ lazyEls.forEach(function(el) {
721
+ if (el._qbLazyObserved) return;
722
+ el._qbLazyObserved = true;
723
+ lazyAdObserver.observe(el);
724
+ });
725
+ }
666
726
 
667
727
  const originalPush = window.qbAds.push.bind(window.qbAds);
668
728
 
@@ -671,6 +731,7 @@
671
731
  if (window.AdSDK && window.GDTAdSDK) {
672
732
  setTimeout(function() {
673
733
  initAllAdSlots();
734
+ startLazyAdObserver();
674
735
  }, 0);
675
736
  }
676
737
  };
@@ -693,13 +754,14 @@
693
754
  }
694
755
  }
695
756
 
696
- // 尝试初始化所有广告位
757
+ // 尝试初始化所有广告位(即插即用 ins)
697
758
  const initialized = initAllAdSlots();
759
+ startLazyAdObserver();
698
760
 
699
761
  if (!initialized && window.AdSDK && window.GDTAdSDK) {
700
- // 如果 SDK 已经加载但初始化失败,稍后重试
701
762
  setTimeout(function() {
702
763
  initAllAdSlots();
764
+ startLazyAdObserver();
703
765
  }, 100);
704
766
  }
705
767
 
@@ -712,30 +774,31 @@
712
774
  setTimeout(function() {
713
775
  if (window.AdSDK && window.GDTAdSDK) {
714
776
  initAllAdSlots();
777
+ startLazyAdObserver();
715
778
  }
716
779
  }, 0);
717
780
  } else {
718
- // 监听 DOMContentLoaded 事件
719
781
  window.addEventListener('DOMContentLoaded', function() {
720
782
  setTimeout(function() {
721
783
  if (window.AdSDK && window.GDTAdSDK) {
722
784
  initAllAdSlots();
785
+ startLazyAdObserver();
723
786
  }
724
787
  }, 0);
725
788
  });
726
789
  }
727
790
 
728
- // 如果 SDK 已经加载完成(同步加载的情况),立即尝试初始化
729
791
  if (window.AdSDK && window.GDTAdSDK) {
730
792
  setTimeout(function() {
731
793
  initAllAdSlots();
794
+ startLazyAdObserver();
732
795
  }, 0);
733
796
  }
734
797
 
735
- // 监听 SDK 就绪事件(异步加载的情况)
736
798
  window.addEventListener('qb-pc-sdk-ready', function() {
737
799
  setTimeout(function() {
738
800
  initAllAdSlots();
801
+ startLazyAdObserver();
739
802
  }, 0);
740
803
  });
741
804
 
@@ -743,18 +806,20 @@
743
806
  if (window.MutationObserver) {
744
807
  const observer = new window.MutationObserver(function(mutations) {
745
808
  let shouldInit = false;
809
+ let shouldInitLazy = false;
746
810
  mutations.forEach(function(mutation) {
747
811
  mutation.addedNodes.forEach(function(node) {
748
812
  if (node.nodeType === 1) {
749
- // 检查是否是广告位标签,或其包含广告位标签
750
813
  if (node.tagName && node.tagName.toLowerCase() === 'ins' &&
751
814
  node.classList && node.classList.contains('qb-adsbyqubian')) {
752
815
  shouldInit = true;
753
- } else if (node.querySelectorAll) {
816
+ } else if (node.classList && node.classList.contains('lazy-ad')) {
817
+ shouldInitLazy = true;
818
+ }
819
+ if (node.querySelectorAll) {
754
820
  const adSlots = node.querySelectorAll('ins.qb-adsbyqubian');
755
- if (adSlots.length > 0) {
756
- shouldInit = true;
757
- }
821
+ if (adSlots.length > 0) shouldInit = true;
822
+ if (node.querySelectorAll('.lazy-ad').length > 0) shouldInitLazy = true;
758
823
  }
759
824
  }
760
825
  });
@@ -763,7 +828,12 @@
763
828
  if (shouldInit && window.AdSDK && window.GDTAdSDK) {
764
829
  setTimeout(function() {
765
830
  initAllAdSlots();
766
- }, 100); // 延迟一点,确保 DOM 完全插入
831
+ }, 100);
832
+ }
833
+ if (shouldInitLazy && window.AdSDK && window.GDTAdSDK) {
834
+ setTimeout(function() {
835
+ startLazyAdObserver();
836
+ }, 100);
767
837
  }
768
838
  });
769
839
 
@@ -0,0 +1,37 @@
1
+ <!DOCTYPE html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Native 广告懒加载测试 (SDK 内置 .lazy-ad)</title>
7
+ <!-- 用户只需引用 SDK,懒加载由 SDK 内部 IntersectionObserver 完成 -->
8
+ <script src="https://unpkg.com/qb-pc-sdk@latest/inline-loader.js" defer></script>
9
+ <style>
10
+ body { margin: 0; padding: 20px; font-family: sans-serif; background: #f0f2f5; }
11
+ .section { max-width: 900px; margin: 0 auto 24px; background: #fff; padding: 24px; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.08); }
12
+ .section h2 { margin: 0 0 16px; font-size: 18px; color: #333; }
13
+ .lazy-ad { min-height: 220px; border: 2px dashed #e0e0e0; border-radius: 8px; display: flex; align-items: center; justify-content: center; color: #999; font-size: 14px; box-sizing: border-box; }
14
+ </style>
15
+ </head>
16
+ <body>
17
+
18
+ <div class="section">
19
+ <h2>首屏广告位</h2>
20
+ <div class="lazy-ad" id="ad-pos-1" data-app-id="2009110913851875373" data-ad-id="2009111010614468619">广告位 1 - 进入视口约 200px 内时加载</div>
21
+ </div>
22
+
23
+ <div class="section" style="height: 400px;">
24
+ <h2>占位内容(向下滚动触发下方广告)</h2>
25
+ </div>
26
+
27
+ <div class="section">
28
+ <h2>下方广告位</h2>
29
+ <div class="lazy-ad" id="ad-pos-2" data-app-id="2009110913851875373" data-ad-id="2009111010614468619">广告位 2 - 懒加载</div>
30
+ </div>
31
+
32
+ <p style="max-width:900px;margin:20px auto;color:#666;font-size:14px;">
33
+ 接入说明:页面中放入 <code>&lt;div class="lazy-ad" data-app-id="应用ID" data-ad-id="广告位ID"&gt;&lt;/div&gt;</code>,引用 SDK 后即可自动懒加载(rootMargin 200px),无需额外脚本。
34
+ </p>
35
+
36
+ </body>
37
+ </html>