qb-pc-sdk 1.0.7 → 1.0.8

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/index.js CHANGED
@@ -194,6 +194,110 @@ if (isBrowser && window.XMLHttpRequest && !window._qbsdkXHRBlocked) {
194
194
  window._qbsdkXHRBlocked = true;
195
195
  }
196
196
 
197
+ // 拦截 iframe 的创建,阻止加载 wxgamesdkframe.html
198
+ if (isBrowser && !window._qbsdkIframeBlocked) {
199
+ const OriginalCreateElement = document.createElement;
200
+ document.createElement = function(tagName, options) {
201
+ const element = OriginalCreateElement.call(this, tagName, options);
202
+
203
+ // 如果是 iframe,拦截 src 属性的设置
204
+ if (tagName.toLowerCase() === 'iframe') {
205
+ const originalSetAttribute = element.setAttribute;
206
+ const originalSetProperty = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'src') ||
207
+ Object.getOwnPropertyDescriptor(HTMLIFrameElement.prototype, 'src');
208
+
209
+ // 拦截 setAttribute
210
+ element.setAttribute = function(name, value) {
211
+ if (name === 'src' && value && /wxgamesdkframe/i.test(value)) {
212
+ // 阻止加载 wxgamesdkframe
213
+ return;
214
+ }
215
+ return originalSetAttribute.apply(this, arguments);
216
+ };
217
+
218
+ // 拦截 src 属性设置
219
+ if (originalSetProperty && originalSetProperty.set) {
220
+ Object.defineProperty(element, 'src', {
221
+ set: function(value) {
222
+ if (value && /wxgamesdkframe/i.test(value)) {
223
+ // 阻止加载 wxgamesdkframe
224
+ return;
225
+ }
226
+ originalSetProperty.set.call(this, value);
227
+ },
228
+ get: function() {
229
+ return originalSetProperty.get ? originalSetProperty.get.call(this) : this.getAttribute('src');
230
+ },
231
+ configurable: true
232
+ });
233
+ } else {
234
+ // 降级方案:使用 Object.defineProperty
235
+ let _src = '';
236
+ Object.defineProperty(element, 'src', {
237
+ set: function(value) {
238
+ if (value && /wxgamesdkframe/i.test(value)) {
239
+ // 阻止加载 wxgamesdkframe
240
+ return;
241
+ }
242
+ _src = value;
243
+ if (originalSetAttribute) {
244
+ originalSetAttribute.call(this, 'src', value);
245
+ }
246
+ },
247
+ get: function() {
248
+ return _src || this.getAttribute('src') || '';
249
+ },
250
+ configurable: true
251
+ });
252
+ }
253
+ }
254
+
255
+ return element;
256
+ };
257
+ window._qbsdkIframeBlocked = true;
258
+ }
259
+
260
+ // 使用 MutationObserver 监听已存在的 iframe,阻止加载 wxgamesdkframe
261
+ if (isBrowser && window.document && window.MutationObserver && !window._qbsdkObserverAdded) {
262
+ const observer = new MutationObserver(function(mutations) {
263
+ mutations.forEach(function(mutation) {
264
+ mutation.addedNodes.forEach(function(node) {
265
+ if (node.nodeType === 1 && node.tagName && node.tagName.toLowerCase() === 'iframe') {
266
+ const src = node.src || node.getAttribute('src') || '';
267
+ if (src && /wxgamesdkframe/i.test(src)) {
268
+ // 阻止加载
269
+ node.src = '';
270
+ node.setAttribute('src', '');
271
+ // 移除 iframe
272
+ try {
273
+ node.parentNode && node.parentNode.removeChild(node);
274
+ } catch (e) {
275
+ // 忽略错误
276
+ }
277
+ }
278
+ }
279
+ });
280
+ });
281
+ });
282
+
283
+ // 等待 DOM 加载完成
284
+ if (window.document.body) {
285
+ observer.observe(window.document.body, {
286
+ childList: true,
287
+ subtree: true
288
+ });
289
+ } else {
290
+ window.addEventListener('DOMContentLoaded', function() {
291
+ observer.observe(window.document.body, {
292
+ childList: true,
293
+ subtree: true
294
+ });
295
+ });
296
+ }
297
+
298
+ window._qbsdkObserverAdded = true;
299
+ }
300
+
197
301
  // 浏览器中预加载底层 SDK(仅 side-effect,挂载 window.GDTAdSDK)
198
302
  if (isBrowser) {
199
303
  try {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "qb-pc-sdk",
3
- "version": "1.0.7",
4
- "description": "趣变广告 SDK 封装器 - PC端广告接入工具,自动处理多级接口映射与原生渲染逻辑",
3
+ "version": "1.0.8",
4
+ "description": "趣变广告 SDK 封装器 - 隐藏自动请求微信sdk",
5
5
  "main": "index.js",
6
6
  "files": [
7
7
  "index.js",
@@ -13,6 +13,151 @@
13
13
  // 核心拦截逻辑:在 SDK 加载前彻底屏蔽 WeChat Localhost 请求
14
14
  // ============================================================
15
15
  if (isBrowser) {
16
+ // 0. 拦截 iframe 的创建,阻止加载 wxgamesdkframe.html
17
+ if (window.document && !window._qbsdkIframeBlocked) {
18
+ const OriginalCreateElement = window.document.createElement;
19
+ window.document.createElement = function(tagName, options) {
20
+ const element = OriginalCreateElement.call(this, tagName, options);
21
+
22
+ // 如果是 iframe,拦截 src 属性的设置
23
+ if (tagName.toLowerCase() === 'iframe') {
24
+ const originalSetAttribute = element.setAttribute;
25
+
26
+ // 拦截 setAttribute
27
+ element.setAttribute = function(name, value) {
28
+ if (name === 'src' && value && /wxgamesdkframe/i.test(value)) {
29
+ // 阻止加载 wxgamesdkframe
30
+ return;
31
+ }
32
+ return originalSetAttribute.apply(this, arguments);
33
+ };
34
+
35
+ // 拦截 src 属性设置
36
+ let _src = '';
37
+ Object.defineProperty(element, 'src', {
38
+ set: function(value) {
39
+ if (value && /wxgamesdkframe/i.test(value)) {
40
+ // 阻止加载 wxgamesdkframe
41
+ return;
42
+ }
43
+ _src = value;
44
+ if (originalSetAttribute) {
45
+ originalSetAttribute.call(this, 'src', value);
46
+ }
47
+
48
+ // 如果 iframe 加载完成,在其内部也设置拦截
49
+ if (value && element.contentWindow) {
50
+ try {
51
+ const iframeWindow = element.contentWindow;
52
+ if (iframeWindow && iframeWindow.XMLHttpRequest && !iframeWindow._qbsdkXHRBlocked) {
53
+ const OriginalXHR = iframeWindow.XMLHttpRequest;
54
+ iframeWindow.XMLHttpRequest = function() {
55
+ const xhr = new OriginalXHR();
56
+ const originalOpen = xhr.open;
57
+ const originalSend = xhr.send;
58
+
59
+ xhr.open = function(method, url, async, user, password) {
60
+ if (url && /localhost\.weixin\.qq\.com/i.test(url)) {
61
+ xhr._qbsdkBlocked = true;
62
+ return;
63
+ }
64
+ return originalOpen.apply(this, arguments);
65
+ };
66
+
67
+ xhr.send = function(data) {
68
+ if (xhr._qbsdkBlocked) {
69
+ return;
70
+ }
71
+ return originalSend.apply(this, arguments);
72
+ };
73
+
74
+ return xhr;
75
+ };
76
+ iframeWindow._qbsdkXHRBlocked = true;
77
+ }
78
+ } catch (e) {
79
+ // 跨域 iframe 无法访问,忽略
80
+ }
81
+ }
82
+ },
83
+ get: function() {
84
+ return _src || this.getAttribute('src') || '';
85
+ },
86
+ configurable: true
87
+ });
88
+
89
+ // 监听 iframe 加载事件,在加载后设置拦截
90
+ element.addEventListener('load', function() {
91
+ try {
92
+ const iframeWindow = element.contentWindow;
93
+ if (iframeWindow && iframeWindow.XMLHttpRequest && !iframeWindow._qbsdkXHRBlocked) {
94
+ const OriginalXHR = iframeWindow.XMLHttpRequest;
95
+ iframeWindow.XMLHttpRequest = function() {
96
+ const xhr = new OriginalXHR();
97
+ const originalOpen = xhr.open;
98
+ const originalSend = xhr.send;
99
+
100
+ xhr.open = function(method, url, async, user, password) {
101
+ if (url && /localhost\.weixin\.qq\.com/i.test(url)) {
102
+ xhr._qbsdkBlocked = true;
103
+ return;
104
+ }
105
+ return originalOpen.apply(this, arguments);
106
+ };
107
+
108
+ xhr.send = function(data) {
109
+ if (xhr._qbsdkBlocked) {
110
+ return;
111
+ }
112
+ return originalSend.apply(this, arguments);
113
+ };
114
+
115
+ return xhr;
116
+ };
117
+ iframeWindow._qbsdkXHRBlocked = true;
118
+ }
119
+ } catch (e) {
120
+ // 跨域 iframe 无法访问,忽略
121
+ }
122
+ });
123
+ }
124
+
125
+ return element;
126
+ };
127
+ window._qbsdkIframeBlocked = true;
128
+ }
129
+
130
+ // 0.1. 使用 MutationObserver 监听已存在的 iframe,阻止加载 wxgamesdkframe
131
+ if (window.document && window.MutationObserver && !window._qbsdkObserverAdded) {
132
+ const observer = new MutationObserver(function(mutations) {
133
+ mutations.forEach(function(mutation) {
134
+ mutation.addedNodes.forEach(function(node) {
135
+ if (node.nodeType === 1 && node.tagName && node.tagName.toLowerCase() === 'iframe') {
136
+ const src = node.src || node.getAttribute('src') || '';
137
+ if (src && /wxgamesdkframe/i.test(src)) {
138
+ // 阻止加载
139
+ node.src = '';
140
+ node.setAttribute('src', '');
141
+ // 移除 iframe
142
+ try {
143
+ node.parentNode && node.parentNode.removeChild(node);
144
+ } catch (e) {
145
+ // 忽略错误
146
+ }
147
+ }
148
+ }
149
+ });
150
+ });
151
+ });
152
+
153
+ observer.observe(window.document.body || window.document.documentElement, {
154
+ childList: true,
155
+ subtree: true
156
+ });
157
+
158
+ window._qbsdkObserverAdded = true;
159
+ }
160
+
16
161
  // 1. 拦截 XMLHttpRequest
17
162
  if (window.XMLHttpRequest && !window._qbsdkXHRBlocked) {
18
163
  const OriginalXHR = window.XMLHttpRequest;