qb-pc-sdk 1.0.6 → 1.0.7
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 +1 -1
- package/src/ad-sdk-wrapper.js +121 -174
package/package.json
CHANGED
package/src/ad-sdk-wrapper.js
CHANGED
|
@@ -7,194 +7,136 @@
|
|
|
7
7
|
|
|
8
8
|
// 兼容浏览器和 Node.js 环境
|
|
9
9
|
const window = typeof global !== 'undefined' && global.window ? global.window : (typeof window !== 'undefined' ? window : global);
|
|
10
|
+
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
|
|
10
11
|
|
|
11
|
-
//
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const
|
|
12
|
+
// ============================================================
|
|
13
|
+
// 核心拦截逻辑:在 SDK 加载前彻底屏蔽 WeChat Localhost 请求
|
|
14
|
+
// ============================================================
|
|
15
|
+
if (isBrowser) {
|
|
16
|
+
// 1. 拦截 XMLHttpRequest
|
|
17
|
+
if (window.XMLHttpRequest && !window._qbsdkXHRBlocked) {
|
|
18
|
+
const OriginalXHR = window.XMLHttpRequest;
|
|
18
19
|
|
|
19
|
-
//
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
20
|
+
// 定义一个安全的 XHR 代理类
|
|
21
|
+
window.XMLHttpRequest = function() {
|
|
22
|
+
const xhr = new OriginalXHR();
|
|
23
|
+
const originalOpen = xhr.open;
|
|
24
|
+
const originalSend = xhr.send;
|
|
25
|
+
|
|
26
|
+
// 标记此实例是否被阻断
|
|
27
|
+
xhr._qbsdkBlocked = false;
|
|
28
|
+
|
|
29
|
+
xhr.open = function(method, url, async, user, password) {
|
|
30
|
+
const targetUrl = (url || '').toString().toLowerCase();
|
|
31
|
+
|
|
32
|
+
// 严格匹配 localhost.weixin.qq.com 及其变体
|
|
33
|
+
if (targetUrl.includes('localhost.weixin.qq.com') ||
|
|
34
|
+
targetUrl.includes('wx_game_base') ||
|
|
35
|
+
/127\.0\.0\.1:\d+\/wx_game_base/.test(targetUrl)) {
|
|
36
|
+
|
|
37
|
+
xhr._qbsdkBlocked = true;
|
|
38
|
+
|
|
39
|
+
// 伪造一个成功的状态,防止 SDK 内部抛出未捕获异常导致重试
|
|
40
|
+
// 我们不调用 originalOpen,而是让它"空转"
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return originalOpen.apply(this, arguments);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
xhr.send = function(data) {
|
|
48
|
+
if (xhr._qbsdkBlocked) {
|
|
49
|
+
// 模拟异步响应,防止同步阻塞
|
|
50
|
+
setTimeout(() => {
|
|
51
|
+
// 如果 SDK 监听了 error 或 loadend,我们需要伪造一个响应
|
|
52
|
+
// 但为了静默,通常什么都不做最好,或者触发一个假的 load 事件
|
|
53
|
+
// 这里选择静默失败,不触发网络层面的报错
|
|
54
|
+
if (xhr.readyState !== 4) {
|
|
55
|
+
Object.defineProperty(xhr, 'readyState', { value: 4, writable: true });
|
|
56
|
+
Object.defineProperty(xhr, 'status', { value: 0, writable: true }); // 0 status usually implies network error but internally handled
|
|
57
|
+
Object.defineProperty(xhr, 'statusText', { value: 'Blocked by AdSDKWrapper', writable: true });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// 触发 fake error 事件以满足某些 SDK 的超时逻辑,但不会在 Console 报 net::ERR
|
|
61
|
+
if (xhr.onerror) xhr.onerror(new ProgressEvent('error'));
|
|
62
|
+
}, 0);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
return originalSend.apply(this, arguments);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
return xhr;
|
|
28
69
|
};
|
|
29
70
|
|
|
30
|
-
//
|
|
31
|
-
|
|
32
|
-
if (
|
|
33
|
-
|
|
34
|
-
return;
|
|
71
|
+
// 复制静态属性 (如 UNSENT, OPENED 等)
|
|
72
|
+
for (let key in OriginalXHR) {
|
|
73
|
+
if (Object.prototype.hasOwnProperty.call(OriginalXHR, key)) {
|
|
74
|
+
window.XMLHttpRequest[key] = OriginalXHR[key];
|
|
35
75
|
}
|
|
36
|
-
|
|
37
|
-
};
|
|
76
|
+
}
|
|
38
77
|
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
window._qbsdkXHRBlocked = true;
|
|
42
|
-
}
|
|
78
|
+
window._qbsdkXHRBlocked = true;
|
|
79
|
+
}
|
|
43
80
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
-
|
|
57
|
-
return (
|
|
58
|
-
errorText.includes('微信') ||
|
|
59
|
-
errorText.includes('wechat') ||
|
|
60
|
-
errorText.includes('weixin') ||
|
|
61
|
-
errorText.includes('wxgame') ||
|
|
62
|
-
errorText.includes('微信游戏') ||
|
|
63
|
-
errorText.includes('微信小游戏') ||
|
|
64
|
-
errorText.includes('wxgamesdkframe') ||
|
|
65
|
-
hasWeixinLocalhost ||
|
|
66
|
-
errorText.includes('wx_game_base') ||
|
|
67
|
-
errorText.includes('getgamesession') ||
|
|
68
|
-
errorText.includes('xmlhttprequest') ||
|
|
69
|
-
errorText.includes('err_failed') ||
|
|
70
|
-
errorText.includes('err_connection_refused') ||
|
|
71
|
-
errorText.includes('cors policy') ||
|
|
72
|
-
errorText.includes('secure context') ||
|
|
73
|
-
errorText.includes('loopback') ||
|
|
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)
|
|
82
|
-
);
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
const filteredError = function(...args) {
|
|
86
|
-
// 如果不是微信相关的错误,正常输出
|
|
87
|
-
if (!shouldFilter(...args)) {
|
|
88
|
-
originalError.apply(window.console, args);
|
|
89
|
-
}
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
const filteredWarn = function(...args) {
|
|
93
|
-
// 如果不是微信相关的警告,正常输出
|
|
94
|
-
if (!shouldFilter(...args)) {
|
|
95
|
-
originalWarn.apply(window.console, args);
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
// 标记已过滤,避免重复拦截
|
|
100
|
-
filteredError._qbsdkFiltered = true;
|
|
101
|
-
filteredWarn._qbsdkFiltered = true;
|
|
102
|
-
window.console.error = filteredError;
|
|
103
|
-
window.console.warn = filteredWarn;
|
|
104
|
-
|
|
105
|
-
// 拦截全局错误事件,过滤微信游戏SDK相关的错误
|
|
106
|
-
if (!window._qbsdkErrorHandlerAdded) {
|
|
107
|
-
const originalOnError = window.onerror;
|
|
108
|
-
window.onerror = function(message, source, lineno, colno, error) {
|
|
109
|
-
const errorText = (message || '').toLowerCase() + (source || '').toLowerCase();
|
|
110
|
-
if (shouldFilter(errorText)) {
|
|
111
|
-
// 静默处理,不输出
|
|
112
|
-
return true; // 阻止默认错误处理
|
|
81
|
+
// 2. 拦截 Fetch API (很多现代 SDK 混用 XHR 和 Fetch)
|
|
82
|
+
if (window.fetch && !window._qbsdkFetchBlocked) {
|
|
83
|
+
const originalFetch = window.fetch;
|
|
84
|
+
window.fetch = function(input, init) {
|
|
85
|
+
let url = '';
|
|
86
|
+
if (typeof input === 'string') {
|
|
87
|
+
url = input;
|
|
88
|
+
} else if (input instanceof Request) {
|
|
89
|
+
url = input.url;
|
|
113
90
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
91
|
+
|
|
92
|
+
url = url.toLowerCase();
|
|
93
|
+
|
|
94
|
+
if (url.includes('localhost.weixin.qq.com') || url.includes('wx_game_base')) {
|
|
95
|
+
// 返回一个永远 pending 或者 立即 reject 的 Promise
|
|
96
|
+
// 为了不报错,返回一个伪造的 Response
|
|
97
|
+
return Promise.resolve(new Response(JSON.stringify({ code: -1, msg: 'Blocked' }), {
|
|
98
|
+
status: 200,
|
|
99
|
+
statusText: 'OK',
|
|
100
|
+
headers: { 'Content-Type': 'application/json' }
|
|
101
|
+
}));
|
|
117
102
|
}
|
|
118
|
-
|
|
103
|
+
|
|
104
|
+
return originalFetch.apply(this, arguments);
|
|
119
105
|
};
|
|
106
|
+
window._qbsdkFetchBlocked = true;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// 3. 拦截 Console (保留原有的日志过滤逻辑作为第二道防线)
|
|
110
|
+
if (window.console && !window.console.error._qbsdkFiltered) {
|
|
111
|
+
const originalError = window.console.error;
|
|
112
|
+
const originalWarn = window.console.warn;
|
|
120
113
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
114
|
+
const shouldFilter = (args) => {
|
|
115
|
+
const text = args.map(String).join(' ').toLowerCase();
|
|
116
|
+
return (
|
|
117
|
+
text.includes('localhost.weixin.qq.com') ||
|
|
118
|
+
text.includes('wx_game_base') ||
|
|
119
|
+
text.includes('err_connection_refused') ||
|
|
120
|
+
text.includes('err_failed') ||
|
|
121
|
+
text.includes('cors policy') ||
|
|
122
|
+
text.includes('wxgamesdkframe')
|
|
123
|
+
);
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
window.console.error = function(...args) {
|
|
127
|
+
if (!shouldFilter(args)) originalError.apply(console, args);
|
|
128
|
+
};
|
|
136
129
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
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
|
-
}
|
|
130
|
+
window.console.warn = function(...args) {
|
|
131
|
+
if (!shouldFilter(args)) originalWarn.apply(console, args);
|
|
132
|
+
};
|
|
194
133
|
|
|
195
|
-
window.
|
|
134
|
+
window.console.error._qbsdkFiltered = true;
|
|
196
135
|
}
|
|
197
136
|
}
|
|
137
|
+
// ============================================================
|
|
138
|
+
// 拦截逻辑结束
|
|
139
|
+
// ============================================================
|
|
198
140
|
|
|
199
141
|
// 内部常量配置
|
|
200
142
|
const API_CONFIG = {
|
|
@@ -419,6 +361,11 @@
|
|
|
419
361
|
}
|
|
420
362
|
|
|
421
363
|
_handleError(error, message) {
|
|
364
|
+
// 内部错误也进行过滤,防止将过滤掉的错误再次打印
|
|
365
|
+
const errStr = String(error).toLowerCase();
|
|
366
|
+
if (errStr.includes('localhost.weixin.qq.com') || errStr.includes('blocked by adsdkwrapper')) {
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
422
369
|
console.error(`[AdSDK Error] ${message}:`, error);
|
|
423
370
|
if (this.config.onAdError) {
|
|
424
371
|
this.config.onAdError(error, message);
|
|
@@ -447,4 +394,4 @@
|
|
|
447
394
|
|
|
448
395
|
// 返回类供直接使用
|
|
449
396
|
return AdSDKWrapper;
|
|
450
|
-
})(typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : this);
|
|
397
|
+
})(typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : this);
|