|
| 1 | +export function setup() { |
| 2 | + hookXHR() |
| 3 | +} |
| 4 | +export function run() {} |
| 5 | + |
| 6 | +declare global { |
| 7 | + interface XMLHttpRequest { |
| 8 | + _requestURL: string |
| 9 | + _requestMethod: string |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +function hookXHR() { |
| 14 | + const originalOpen = XMLHttpRequest.prototype.open |
| 15 | + const originalSend = XMLHttpRequest.prototype.send |
| 16 | + |
| 17 | + // @ts-ignore |
| 18 | + XMLHttpRequest.prototype.open = function (method, url: string, ...rest) { |
| 19 | + this._requestURL = url |
| 20 | + this._requestMethod = method |
| 21 | + // @ts-ignore |
| 22 | + return originalOpen.call(this, method, url, ...rest) |
| 23 | + } |
| 24 | + |
| 25 | + XMLHttpRequest.prototype.send = function (body) { |
| 26 | + const xhr = this |
| 27 | + const url = this._requestURL |
| 28 | + const method = this._requestMethod |
| 29 | + if (url && url.includes('GetWebLivePermission')) { |
| 30 | + const originalOnReadyStateChange = xhr.onreadystatechange |
| 31 | + xhr.onreadystatechange = function () { |
| 32 | + if (xhr.readyState === 4) { |
| 33 | + try { |
| 34 | + if (xhr.status >= 200 && xhr.status < 300) { |
| 35 | + let responseData = JSON.parse(xhr.responseText) |
| 36 | + if (responseData.data) { |
| 37 | + responseData.data.allow_live = true |
| 38 | + } |
| 39 | + |
| 40 | + Object.defineProperty(xhr, 'responseText', { |
| 41 | + get: function () { |
| 42 | + return JSON.stringify(responseData) |
| 43 | + }, |
| 44 | + }) |
| 45 | + } |
| 46 | + } catch (e) { |
| 47 | + console.error('修改GetWebLivePermission返回值出错:', e) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if (originalOnReadyStateChange) { |
| 52 | + // @ts-ignore |
| 53 | + originalOnReadyStateChange.apply(this, arguments) |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if (url && url.includes('/room/v1/Room/startLive')) { |
| 59 | + const isString = typeof body === 'string' |
| 60 | + const params = isString |
| 61 | + ? new URLSearchParams(body) |
| 62 | + : (body as URLSearchParams) |
| 63 | + params.set('platform', 'mobile') |
| 64 | + arguments[0] = isString ? params.toString() : params |
| 65 | + } |
| 66 | + // @ts-ignore |
| 67 | + return originalSend.apply(this, arguments) |
| 68 | + } |
| 69 | +} |
0 commit comments