qb-pc-sdk 1.2.8 → 1.3.0
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/README.md +254 -433
- package/core.min.js +1 -1
- package/index.js +279 -0
- package/inline-loader.js +1 -0
- package/package.json +6 -6
- package/src/ad-sdk-wrapper.js +846 -0
- package/src/example-simple.html +133 -0
- package/inline-loader-cdn.js +0 -1
- package/loader.min.js +0 -1
package/index.js
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
const isBrowser = typeof window !== 'undefined';
|
|
2
|
+
|
|
3
|
+
if (isBrowser && typeof window !== 'undefined' && window.console && !window.console.error._qbsdkFiltered) {
|
|
4
|
+
const originalError = window.console.error;
|
|
5
|
+
const originalWarn = window.console.warn;
|
|
6
|
+
|
|
7
|
+
const shouldFilter = function(...args) {
|
|
8
|
+
const errorText = args.join(' ').toLowerCase();
|
|
9
|
+
const hasWeixinLocalhost = /https?:\/\/localhost\.weixin\.qq\.com/i.test(errorText) ||
|
|
10
|
+
/localhost\.weixin\.qq\.com/i.test(errorText) ||
|
|
11
|
+
errorText.includes('localhost.weixin.qq.com');
|
|
12
|
+
|
|
13
|
+
const isGdtCorsError = (errorText.includes('sdk.e.qq.com') || errorText.includes('e.qq.com')) &&
|
|
14
|
+
(errorText.includes('cors') || errorText.includes('access-control-allow-origin')) &&
|
|
15
|
+
(errorText.includes('blocked') || errorText.includes('err_failed') || errorText.includes('500'));
|
|
16
|
+
|
|
17
|
+
return (
|
|
18
|
+
errorText.includes('微信') ||
|
|
19
|
+
errorText.includes('wechat') ||
|
|
20
|
+
errorText.includes('weixin') ||
|
|
21
|
+
errorText.includes('wxgame') ||
|
|
22
|
+
errorText.includes('微信游戏') ||
|
|
23
|
+
errorText.includes('微信小游戏') ||
|
|
24
|
+
errorText.includes('wxgamesdkframe') ||
|
|
25
|
+
hasWeixinLocalhost ||
|
|
26
|
+
errorText.includes('wx_game_base') ||
|
|
27
|
+
errorText.includes('getgamesession') ||
|
|
28
|
+
isGdtCorsError ||
|
|
29
|
+
errorText.includes('xmlhttprequest') ||
|
|
30
|
+
errorText.includes('err_failed') ||
|
|
31
|
+
errorText.includes('err_connection_refused') ||
|
|
32
|
+
errorText.includes('cors policy') ||
|
|
33
|
+
errorText.includes('secure context') ||
|
|
34
|
+
errorText.includes('loopback') ||
|
|
35
|
+
errorText.includes('more-private address space') ||
|
|
36
|
+
errorText.includes('net::') ||
|
|
37
|
+
errorText.includes('timeout') ||
|
|
38
|
+
errorText.includes('frame on invoke') ||
|
|
39
|
+
errorText.includes('/wx_game_base/api/business') ||
|
|
40
|
+
/localhost\.weixin\.qq\.com:\d+/.test(errorText) ||
|
|
41
|
+
/:\d{5}\/wx_game_base/.test(errorText) ||
|
|
42
|
+
/:\d{5}\/wx_game_base/.test(errorText)
|
|
43
|
+
);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const filteredError = function(...args) {
|
|
47
|
+
if (!shouldFilter(...args)) {
|
|
48
|
+
originalError.apply(window.console, args);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const filteredWarn = function(...args) {
|
|
53
|
+
if (!shouldFilter(...args)) {
|
|
54
|
+
originalWarn.apply(window.console, args);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
filteredError._qbsdkFiltered = true;
|
|
59
|
+
filteredWarn._qbsdkFiltered = true;
|
|
60
|
+
window.console.error = filteredError;
|
|
61
|
+
window.console.warn = filteredWarn;
|
|
62
|
+
|
|
63
|
+
if (!window._qbsdkErrorHandlerAdded) {
|
|
64
|
+
const originalOnError = window.onerror;
|
|
65
|
+
window.onerror = function(message, source, lineno, colno, error) {
|
|
66
|
+
const errorText = (message || '').toLowerCase() + (source || '').toLowerCase();
|
|
67
|
+
if (shouldFilter(errorText)) {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
if (originalOnError) {
|
|
71
|
+
return originalOnError.call(this, message, source, lineno, colno, error);
|
|
72
|
+
}
|
|
73
|
+
return false;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const originalUnhandledRejection = window.onunhandledrejection;
|
|
77
|
+
window.addEventListener('unhandledrejection', function(event) {
|
|
78
|
+
const reason = event.reason || {};
|
|
79
|
+
const errorText = (reason.message || reason || '').toString().toLowerCase();
|
|
80
|
+
if (shouldFilter(errorText)) {
|
|
81
|
+
event.preventDefault();
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (originalUnhandledRejection) {
|
|
85
|
+
originalUnhandledRejection.call(window, event);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
if (window.XMLHttpRequest) {
|
|
90
|
+
const OriginalXHR = window.XMLHttpRequest;
|
|
91
|
+
window.XMLHttpRequest = function() {
|
|
92
|
+
const xhr = new OriginalXHR();
|
|
93
|
+
const originalAddEventListener = xhr.addEventListener;
|
|
94
|
+
|
|
95
|
+
xhr.addEventListener = function(type, listener, options) {
|
|
96
|
+
if (type === 'error' || type === 'loadend' || type === 'abort') {
|
|
97
|
+
const wrappedListener = function(event) {
|
|
98
|
+
const url = (xhr.responseURL || xhr._requestURL || '').toLowerCase();
|
|
99
|
+
if (xhr._qbsdkFiltered || shouldFilter(url)) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
if (listener) {
|
|
103
|
+
listener.apply(this, arguments);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
return originalAddEventListener.call(this, type, wrappedListener, options);
|
|
107
|
+
}
|
|
108
|
+
return originalAddEventListener.apply(this, arguments);
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const originalOpen = xhr.open;
|
|
112
|
+
xhr.open = function(method, url, async, user, password) {
|
|
113
|
+
xhr._requestURL = url;
|
|
114
|
+
if (url && /localhost\.weixin\.qq\.com/i.test(url)) {
|
|
115
|
+
xhr._qbsdkFiltered = true;
|
|
116
|
+
}
|
|
117
|
+
return originalOpen.apply(this, arguments);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
Object.defineProperty(xhr, 'onerror', {
|
|
121
|
+
set: function(handler) {
|
|
122
|
+
const wrappedHandler = function(event) {
|
|
123
|
+
const url = (xhr.responseURL || xhr._requestURL || '').toLowerCase();
|
|
124
|
+
if (!xhr._qbsdkFiltered && !shouldFilter(url) && handler) {
|
|
125
|
+
handler.apply(this, arguments);
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
originalAddEventListener.call(this, 'error', wrappedHandler);
|
|
129
|
+
},
|
|
130
|
+
get: function() {
|
|
131
|
+
return this._onerror;
|
|
132
|
+
},
|
|
133
|
+
configurable: true
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
return xhr;
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
window._qbsdkErrorHandlerAdded = true;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (isBrowser && window.XMLHttpRequest && !window._qbsdkXHRBlocked) {
|
|
145
|
+
const OriginalXHR = window.XMLHttpRequest;
|
|
146
|
+
window.XMLHttpRequest = function() {
|
|
147
|
+
const xhr = new OriginalXHR();
|
|
148
|
+
const originalOpen = xhr.open;
|
|
149
|
+
const originalSend = xhr.send;
|
|
150
|
+
|
|
151
|
+
xhr.open = function(method, url, async, user, password) {
|
|
152
|
+
if (url && /localhost\.weixin\.qq\.com/i.test(url)) {
|
|
153
|
+
xhr._qbsdkBlocked = true;
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
return originalOpen.apply(this, arguments);
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
xhr.send = function(data) {
|
|
160
|
+
if (xhr._qbsdkBlocked) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
return originalSend.apply(this, arguments);
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
return xhr;
|
|
167
|
+
};
|
|
168
|
+
window._qbsdkXHRBlocked = true;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (isBrowser && !window._qbsdkIframeBlocked) {
|
|
172
|
+
const OriginalCreateElement = document.createElement;
|
|
173
|
+
document.createElement = function(tagName, options) {
|
|
174
|
+
const element = OriginalCreateElement.call(this, tagName, options);
|
|
175
|
+
|
|
176
|
+
if (tagName.toLowerCase() === 'iframe') {
|
|
177
|
+
const originalSetAttribute = element.setAttribute;
|
|
178
|
+
const originalSetProperty = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'src') ||
|
|
179
|
+
Object.getOwnPropertyDescriptor(HTMLIFrameElement.prototype, 'src');
|
|
180
|
+
|
|
181
|
+
element.setAttribute = function(name, value) {
|
|
182
|
+
if (name === 'src' && value && /wxgamesdkframe/i.test(value)) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
return originalSetAttribute.apply(this, arguments);
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
if (originalSetProperty && originalSetProperty.set) {
|
|
189
|
+
Object.defineProperty(element, 'src', {
|
|
190
|
+
set: function(value) {
|
|
191
|
+
if (value && /wxgamesdkframe/i.test(value)) {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
originalSetProperty.set.call(this, value);
|
|
195
|
+
},
|
|
196
|
+
get: function() {
|
|
197
|
+
return originalSetProperty.get ? originalSetProperty.get.call(this) : this.getAttribute('src');
|
|
198
|
+
},
|
|
199
|
+
configurable: true
|
|
200
|
+
});
|
|
201
|
+
} else {
|
|
202
|
+
let _src = '';
|
|
203
|
+
Object.defineProperty(element, 'src', {
|
|
204
|
+
set: function(value) {
|
|
205
|
+
if (value && /wxgamesdkframe/i.test(value)) {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
_src = value;
|
|
209
|
+
if (originalSetAttribute) {
|
|
210
|
+
originalSetAttribute.call(this, 'src', value);
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
get: function() {
|
|
214
|
+
return _src || this.getAttribute('src') || '';
|
|
215
|
+
},
|
|
216
|
+
configurable: true
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return element;
|
|
222
|
+
};
|
|
223
|
+
window._qbsdkIframeBlocked = true;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (isBrowser && window.document && window.MutationObserver && !window._qbsdkObserverAdded) {
|
|
227
|
+
const observer = new MutationObserver(function(mutations) {
|
|
228
|
+
mutations.forEach(function(mutation) {
|
|
229
|
+
mutation.addedNodes.forEach(function(node) {
|
|
230
|
+
if (node.nodeType === 1 && node.tagName && node.tagName.toLowerCase() === 'iframe') {
|
|
231
|
+
const src = node.src || node.getAttribute('src') || '';
|
|
232
|
+
if (src && /wxgamesdkframe/i.test(src)) {
|
|
233
|
+
node.src = '';
|
|
234
|
+
node.setAttribute('src', '');
|
|
235
|
+
try {
|
|
236
|
+
node.parentNode && node.parentNode.removeChild(node);
|
|
237
|
+
} catch (e) {
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
if (window.document.body) {
|
|
246
|
+
observer.observe(window.document.body, {
|
|
247
|
+
childList: true,
|
|
248
|
+
subtree: true
|
|
249
|
+
});
|
|
250
|
+
} else {
|
|
251
|
+
window.addEventListener('DOMContentLoaded', function() {
|
|
252
|
+
observer.observe(window.document.body, {
|
|
253
|
+
childList: true,
|
|
254
|
+
subtree: true
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
window._qbsdkObserverAdded = true;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (isBrowser) {
|
|
263
|
+
try {
|
|
264
|
+
const OriginSDK = require('qb-pc-ad-sdk-origin');
|
|
265
|
+
if (OriginSDK && !window.GDTAdSDK) {
|
|
266
|
+
window.GDTAdSDK = OriginSDK.default || OriginSDK.GDTAdSDK || OriginSDK;
|
|
267
|
+
}
|
|
268
|
+
} catch (err) {
|
|
269
|
+
// 底层 SDK 自动加载失败,静默处理(模块化环境需要手动引入)
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const AdSDKWrapper = require('./src/ad-sdk-wrapper.js');
|
|
274
|
+
|
|
275
|
+
module.exports = AdSDKWrapper;
|
|
276
|
+
module.exports.default = AdSDKWrapper;
|
|
277
|
+
module.exports.AdSDKWrapper = AdSDKWrapper;
|
|
278
|
+
module.exports.GDTAdSDK = isBrowser ? window.GDTAdSDK : undefined;
|
|
279
|
+
|
package/inline-loader.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(n){"use strict";var e=void 0!==n&&n.window?n.window:"undefined"!=typeof window?window:n,i=e.document;if(i&&!e.AdSDK){var d=e.navigator&&e.navigator.userAgent||"",o=e.navigator&&e.navigator.platform||"";if(!/Android|iPhone|iPad|iPod|Mac|webOS|BlackBerry|IEMobile|Opera Mini/i.test(d)&&/Win|Windows/i.test(d+o)){e._qbPcSdkLoading=!0;var t="https://unpkg.com/qb-pc-sdk@latest/core.min.js";if(i.querySelector('script[src="'+t+'"]'))a(null);else{var r=i.createElement("script");r.src=t,r.async=!0,r.onload=function(){e.GDTAdSDK&&e.AdSDK?a(null):setTimeout(function(){a(e.GDTAdSDK&&e.AdSDK?null:new Error("SDK\u521d\u59cb\u5316\u5931\u8d25"))},100)},r.onerror=function(){a(new Error("SDK\u52a0\u8f7d\u5931\u8d25"))},i.head.appendChild(r)}}}function a(n){e._qbPcSdkLoading=!1,e.dispatchEvent(new CustomEvent("qb-pc-sdk-ready",{detail:n?{error:n,AdSDK:null,GDTAdSDK:null}:{AdSDK:e.AdSDK,GDTAdSDK:e.GDTAdSDK}}))}}("undefined"!=typeof window?window:"undefined"!=typeof global?global:this);
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "qb-pc-sdk",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "pc广告 SDK 封装器 -
|
|
3
|
+
"version": "1.3.0",
|
|
4
|
+
"description": "pc广告 SDK 封装器 - 去除容灾",
|
|
5
5
|
"main": "core.min.js",
|
|
6
6
|
"files": [
|
|
7
7
|
"core.min.js",
|
|
8
|
-
"loader.
|
|
9
|
-
"
|
|
8
|
+
"inline-loader.js",
|
|
9
|
+
"index.js","src",
|
|
10
10
|
"README.md"
|
|
11
11
|
],
|
|
12
12
|
"keywords": [
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
32
|
"build:core": "node build-core.js",
|
|
33
|
-
"build:
|
|
34
|
-
"build": "npm run build:core && npm run build:
|
|
33
|
+
"build:snippet": "node build-snippet.js",
|
|
34
|
+
"build": "npm run build:core && npm run build:snippet",
|
|
35
35
|
"version:patch": "npm version patch",
|
|
36
36
|
"prepublishOnly": "npm run build"
|
|
37
37
|
},
|