qb-pc-sdk 1.0.5 → 1.0.6

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
@@ -14,6 +14,11 @@ if (isBrowser && typeof window !== 'undefined' && window.console && !window.cons
14
14
  // 过滤函数,检查是否应该过滤
15
15
  const shouldFilter = function(...args) {
16
16
  const errorText = args.join(' ').toLowerCase();
17
+ // 检查是否包含 localhost.weixin.qq.com(各种格式)
18
+ const hasWeixinLocalhost = /https?:\/\/localhost\.weixin\.qq\.com/i.test(errorText) ||
19
+ /localhost\.weixin\.qq\.com/i.test(errorText) ||
20
+ errorText.includes('localhost.weixin.qq.com');
21
+
17
22
  return (
18
23
  errorText.includes('微信') ||
19
24
  errorText.includes('wechat') ||
@@ -22,7 +27,7 @@ if (isBrowser && typeof window !== 'undefined' && window.console && !window.cons
22
27
  errorText.includes('微信游戏') ||
23
28
  errorText.includes('微信小游戏') ||
24
29
  errorText.includes('wxgamesdkframe') ||
25
- errorText.includes('localhost.weixin.qq.com') ||
30
+ hasWeixinLocalhost ||
26
31
  errorText.includes('wx_game_base') ||
27
32
  errorText.includes('getgamesession') ||
28
33
  errorText.includes('xmlhttprequest') ||
@@ -31,7 +36,14 @@ if (isBrowser && typeof window !== 'undefined' && window.console && !window.cons
31
36
  errorText.includes('cors policy') ||
32
37
  errorText.includes('secure context') ||
33
38
  errorText.includes('loopback') ||
34
- errorText.includes('more-private address space')
39
+ errorText.includes('more-private address space') ||
40
+ errorText.includes('net::') ||
41
+ errorText.includes('timeout') ||
42
+ errorText.includes('frame on invoke') ||
43
+ errorText.includes('/wx_game_base/api/business') ||
44
+ /localhost\.weixin\.qq\.com:\d+/.test(errorText) ||
45
+ /:\d{5}\/wx_game_base/.test(errorText) ||
46
+ /:\d{5}\/wx_game_base/.test(errorText)
35
47
  );
36
48
  };
37
49
 
@@ -87,10 +99,101 @@ if (isBrowser && typeof window !== 'undefined' && window.console && !window.cons
87
99
  }
88
100
  });
89
101
 
102
+ // 拦截 XMLHttpRequest 的错误事件,只隐藏错误日志,不阻止请求
103
+ if (window.XMLHttpRequest) {
104
+ const OriginalXHR = window.XMLHttpRequest;
105
+ window.XMLHttpRequest = function() {
106
+ const xhr = new OriginalXHR();
107
+ const originalAddEventListener = xhr.addEventListener;
108
+
109
+ // 拦截 addEventListener,过滤错误事件
110
+ xhr.addEventListener = function(type, listener, options) {
111
+ if (type === 'error' || type === 'loadend' || type === 'abort') {
112
+ const wrappedListener = function(event) {
113
+ const url = (xhr.responseURL || xhr._requestURL || '').toLowerCase();
114
+ // 如果请求被标记为需要过滤,或者 URL 匹配过滤规则,则静默处理
115
+ if (xhr._qbsdkFiltered || shouldFilter(url)) {
116
+ return; // 静默处理错误事件
117
+ }
118
+ if (listener) {
119
+ listener.apply(this, arguments);
120
+ }
121
+ };
122
+ return originalAddEventListener.call(this, type, wrappedListener, options);
123
+ }
124
+ return originalAddEventListener.apply(this, arguments);
125
+ };
126
+
127
+ // 保存请求 URL 以便后续判断
128
+ const originalOpen = xhr.open;
129
+ xhr.open = function(method, url, async, user, password) {
130
+ xhr._requestURL = url;
131
+ // 如果是 localhost.weixin.qq.com 的请求,标记为需要过滤
132
+ if (url && /localhost\.weixin\.qq\.com/i.test(url)) {
133
+ xhr._qbsdkFiltered = true;
134
+ }
135
+ return originalOpen.apply(this, arguments);
136
+ };
137
+
138
+ // 拦截 onerror 属性
139
+ Object.defineProperty(xhr, 'onerror', {
140
+ set: function(handler) {
141
+ const wrappedHandler = function(event) {
142
+ const url = (xhr.responseURL || xhr._requestURL || '').toLowerCase();
143
+ // 如果请求被标记为需要过滤,或者 URL 匹配过滤规则,则静默处理
144
+ if (!xhr._qbsdkFiltered && !shouldFilter(url) && handler) {
145
+ handler.apply(this, arguments);
146
+ }
147
+ };
148
+ originalAddEventListener.call(this, 'error', wrappedHandler);
149
+ },
150
+ get: function() {
151
+ return this._onerror;
152
+ },
153
+ configurable: true
154
+ });
155
+
156
+ return xhr;
157
+ };
158
+ }
159
+
90
160
  window._qbsdkErrorHandlerAdded = true;
91
161
  }
92
162
  }
93
163
 
164
+ // 在加载底层SDK之前,拦截并阻止所有对 localhost.weixin.qq.com 的请求
165
+ if (isBrowser && window.XMLHttpRequest && !window._qbsdkXHRBlocked) {
166
+ const OriginalXHR = window.XMLHttpRequest;
167
+ window.XMLHttpRequest = function() {
168
+ const xhr = new OriginalXHR();
169
+ const originalOpen = xhr.open;
170
+ const originalSend = xhr.send;
171
+
172
+ // 拦截 open 方法,阻止对 localhost.weixin.qq.com 的请求
173
+ xhr.open = function(method, url, async, user, password) {
174
+ // 如果是微信游戏SDK的请求,直接阻止
175
+ if (url && /localhost\.weixin\.qq\.com/i.test(url)) {
176
+ xhr._qbsdkBlocked = true;
177
+ // 不调用原始 open,直接返回,阻止请求
178
+ return;
179
+ }
180
+ return originalOpen.apply(this, arguments);
181
+ };
182
+
183
+ // 拦截 send 方法,如果请求被阻止则不发送
184
+ xhr.send = function(data) {
185
+ if (xhr._qbsdkBlocked) {
186
+ // 请求已被阻止,不发送
187
+ return;
188
+ }
189
+ return originalSend.apply(this, arguments);
190
+ };
191
+
192
+ return xhr;
193
+ };
194
+ window._qbsdkXHRBlocked = true;
195
+ }
196
+
94
197
  // 浏览器中预加载底层 SDK(仅 side-effect,挂载 window.GDTAdSDK)
95
198
  if (isBrowser) {
96
199
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qb-pc-sdk",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "趣变广告 SDK 封装器 - PC端广告接入工具,自动处理多级接口映射与原生渲染逻辑",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -8,6 +8,39 @@
8
8
  // 兼容浏览器和 Node.js 环境
9
9
  const window = typeof global !== 'undefined' && global.window ? global.window : (typeof window !== 'undefined' ? window : global);
10
10
 
11
+ // 在加载底层SDK之前,拦截并阻止所有对 localhost.weixin.qq.com 的请求
12
+ if (typeof window !== 'undefined' && window.XMLHttpRequest && !window._qbsdkXHRBlocked) {
13
+ const OriginalXHR = window.XMLHttpRequest;
14
+ window.XMLHttpRequest = function() {
15
+ const xhr = new OriginalXHR();
16
+ const originalOpen = xhr.open;
17
+ const originalSend = xhr.send;
18
+
19
+ // 拦截 open 方法,阻止对 localhost.weixin.qq.com 的请求
20
+ xhr.open = function(method, url, async, user, password) {
21
+ // 如果是微信游戏SDK的请求,直接阻止
22
+ if (url && /localhost\.weixin\.qq\.com/i.test(url)) {
23
+ xhr._qbsdkBlocked = true;
24
+ // 不调用原始 open,直接返回,阻止请求
25
+ return;
26
+ }
27
+ return originalOpen.apply(this, arguments);
28
+ };
29
+
30
+ // 拦截 send 方法,如果请求被阻止则不发送
31
+ xhr.send = function(data) {
32
+ if (xhr._qbsdkBlocked) {
33
+ // 请求已被阻止,不发送
34
+ return;
35
+ }
36
+ return originalSend.apply(this, arguments);
37
+ };
38
+
39
+ return xhr;
40
+ };
41
+ window._qbsdkXHRBlocked = true;
42
+ }
43
+
11
44
  // 拦截并过滤微信游戏SDK相关的错误日志(仅拦截一次)
12
45
  if (typeof window !== 'undefined' && window.console && !window.console.error._qbsdkFiltered) {
13
46
  const originalError = window.console.error;
@@ -16,6 +49,11 @@
16
49
  // 过滤函数,检查是否应该过滤
17
50
  const shouldFilter = function(...args) {
18
51
  const errorText = args.join(' ').toLowerCase();
52
+ // 检查是否包含 localhost.weixin.qq.com(各种格式)
53
+ const hasWeixinLocalhost = /https?:\/\/localhost\.weixin\.qq\.com/i.test(errorText) ||
54
+ /localhost\.weixin\.qq\.com/i.test(errorText) ||
55
+ errorText.includes('localhost.weixin.qq.com');
56
+
19
57
  return (
20
58
  errorText.includes('微信') ||
21
59
  errorText.includes('wechat') ||
@@ -24,7 +62,7 @@
24
62
  errorText.includes('微信游戏') ||
25
63
  errorText.includes('微信小游戏') ||
26
64
  errorText.includes('wxgamesdkframe') ||
27
- errorText.includes('localhost.weixin.qq.com') ||
65
+ hasWeixinLocalhost ||
28
66
  errorText.includes('wx_game_base') ||
29
67
  errorText.includes('getgamesession') ||
30
68
  errorText.includes('xmlhttprequest') ||
@@ -33,7 +71,14 @@
33
71
  errorText.includes('cors policy') ||
34
72
  errorText.includes('secure context') ||
35
73
  errorText.includes('loopback') ||
36
- errorText.includes('more-private address space')
74
+ errorText.includes('more-private address space') ||
75
+ errorText.includes('net::') ||
76
+ errorText.includes('timeout') ||
77
+ errorText.includes('frame on invoke') ||
78
+ errorText.includes('/wx_game_base/api/business') ||
79
+ /localhost\.weixin\.qq\.com:\d+/.test(errorText) ||
80
+ /:\d{5}\/wx_game_base/.test(errorText) ||
81
+ /:\d{5}\/wx_game_base/.test(errorText)
37
82
  );
38
83
  };
39
84
 
@@ -89,6 +134,64 @@
89
134
  }
90
135
  });
91
136
 
137
+ // 拦截 XMLHttpRequest 的错误事件,只隐藏错误日志,不阻止请求
138
+ if (window.XMLHttpRequest) {
139
+ const OriginalXHR = window.XMLHttpRequest;
140
+ window.XMLHttpRequest = function() {
141
+ const xhr = new OriginalXHR();
142
+ const originalAddEventListener = xhr.addEventListener;
143
+
144
+ // 拦截 addEventListener,过滤错误事件
145
+ xhr.addEventListener = function(type, listener, options) {
146
+ if (type === 'error' || type === 'loadend' || type === 'abort') {
147
+ const wrappedListener = function(event) {
148
+ const url = (xhr.responseURL || xhr._requestURL || '').toLowerCase();
149
+ // 如果请求被标记为需要过滤,或者 URL 匹配过滤规则,则静默处理
150
+ if (xhr._qbsdkFiltered || shouldFilter(url)) {
151
+ return; // 静默处理错误事件
152
+ }
153
+ if (listener) {
154
+ listener.apply(this, arguments);
155
+ }
156
+ };
157
+ return originalAddEventListener.call(this, type, wrappedListener, options);
158
+ }
159
+ return originalAddEventListener.apply(this, arguments);
160
+ };
161
+
162
+ // 保存请求 URL 以便后续判断
163
+ const originalOpen = xhr.open;
164
+ xhr.open = function(method, url, async, user, password) {
165
+ xhr._requestURL = url;
166
+ // 如果是 localhost.weixin.qq.com 的请求,标记为需要过滤
167
+ if (url && /localhost\.weixin\.qq\.com/i.test(url)) {
168
+ xhr._qbsdkFiltered = true;
169
+ }
170
+ return originalOpen.apply(this, arguments);
171
+ };
172
+
173
+ // 拦截 onerror 属性
174
+ Object.defineProperty(xhr, 'onerror', {
175
+ set: function(handler) {
176
+ const wrappedHandler = function(event) {
177
+ const url = (xhr.responseURL || xhr._requestURL || '').toLowerCase();
178
+ // 如果请求被标记为需要过滤,或者 URL 匹配过滤规则,则静默处理
179
+ if (!xhr._qbsdkFiltered && !shouldFilter(url) && handler) {
180
+ handler.apply(this, arguments);
181
+ }
182
+ };
183
+ originalAddEventListener.call(this, 'error', wrappedHandler);
184
+ },
185
+ get: function() {
186
+ return this._onerror;
187
+ },
188
+ configurable: true
189
+ });
190
+
191
+ return xhr;
192
+ };
193
+ }
194
+
92
195
  window._qbsdkErrorHandlerAdded = true;
93
196
  }
94
197
  }