qb-pc-sdk 1.0.4 → 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 +107 -3
- package/package.json +1 -1
- package/src/ad-sdk-wrapper.js +107 -3
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
|
-
|
|
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
|
|
|
@@ -74,7 +86,8 @@ if (isBrowser && typeof window !== 'undefined' && window.console && !window.cons
|
|
|
74
86
|
// 拦截未捕获的 Promise rejection
|
|
75
87
|
const originalUnhandledRejection = window.onunhandledrejection;
|
|
76
88
|
window.addEventListener('unhandledrejection', function(event) {
|
|
77
|
-
const
|
|
89
|
+
const reason = event.reason || {};
|
|
90
|
+
const errorText = (reason.message || reason || '').toString().toLowerCase();
|
|
78
91
|
if (shouldFilter(errorText)) {
|
|
79
92
|
// 阻止默认行为,静默处理
|
|
80
93
|
event.preventDefault();
|
|
@@ -86,10 +99,101 @@ if (isBrowser && typeof window !== 'undefined' && window.console && !window.cons
|
|
|
86
99
|
}
|
|
87
100
|
});
|
|
88
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
|
+
|
|
89
160
|
window._qbsdkErrorHandlerAdded = true;
|
|
90
161
|
}
|
|
91
162
|
}
|
|
92
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
|
+
|
|
93
197
|
// 浏览器中预加载底层 SDK(仅 side-effect,挂载 window.GDTAdSDK)
|
|
94
198
|
if (isBrowser) {
|
|
95
199
|
try {
|
package/package.json
CHANGED
package/src/ad-sdk-wrapper.js
CHANGED
|
@@ -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
|
-
|
|
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
|
|
|
@@ -76,7 +121,8 @@
|
|
|
76
121
|
// 拦截未捕获的 Promise rejection
|
|
77
122
|
const originalUnhandledRejection = window.onunhandledrejection;
|
|
78
123
|
window.addEventListener('unhandledrejection', function(event) {
|
|
79
|
-
const
|
|
124
|
+
const reason = event.reason || {};
|
|
125
|
+
const errorText = (reason.message || reason || '').toString().toLowerCase();
|
|
80
126
|
if (shouldFilter(errorText)) {
|
|
81
127
|
// 阻止默认行为,静默处理
|
|
82
128
|
event.preventDefault();
|
|
@@ -88,6 +134,64 @@
|
|
|
88
134
|
}
|
|
89
135
|
});
|
|
90
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
|
+
|
|
91
195
|
window._qbsdkErrorHandlerAdded = true;
|
|
92
196
|
}
|
|
93
197
|
}
|