Skip to content

Commit

Permalink
feat: improve xhr hack
Browse files Browse the repository at this point in the history
  • Loading branch information
KochiyaOcean committed Oct 17, 2023
1 parent a206b52 commit 4dfc297
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 42 deletions.
114 changes: 72 additions & 42 deletions assets/js/xhr-hack.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,83 @@ window.hackXhr = (win = window) => {
if (win.xhrHacked) {
return false
}

const OriginalXMLHttpRequest = win.XMLHttpRequest
win.XMLHttpRequest = function () {
let method, reqUrl, reqBody
const req = new OriginalXMLHttpRequest()

// Hack open method
req.originalOpen = req.open
req.open = (...params) => {
method = params[0]
reqUrl = params[1]
return req.originalOpen(...params)

win.XMLHttpRequest = class XMLHttpRequest extends OriginalXMLHttpRequest {
constructor() {
super()

this.method = 'GET'
this.requestURL = ''
this.request = ''
this.responseHack = undefined
this.responseTextHack = undefined
this.responseXMLHack = undefined

this.addEventListener('load', () => {
const responseURL = this.responseURL || this.requestURL
gameAPIBroadcaster.sendRequest(
this.method,
[undefined, url.parse(responseURL).pathname, responseURL],
this.request,
)
})
this.addEventListener('loadend', () => {
if (!this.responseType || ['json', 'document', 'text'].includes(this.responseType)) {
gameAPIBroadcaster.sendResponse(
this.method,
[undefined, url.parse(this.responseURL).pathname, this.responseURL],
this.request,
this.response,
this.responseType,
this.status,
)
}
})
this.addEventListener('error', () => {
const responseURL = this.responseURL || this.requestURL
gameAPIBroadcaster.sendError(
[undefined, url.parse(responseURL).pathname, responseURL],
this.status,
)
})
}

// Hack send method
req.originalSend = req.send
req.send = (body) => {
reqBody = body
return req.originalSend(body)
open(method, requestURL, ...props) {
this.method = method
this.requestURL = requestURL
super.open(method, requestURL, ...props)
}

// Send event
req.addEventListener('load', () => {
const resUrl = req.responseURL || reqUrl
gameAPIBroadcaster.sendRequest(
method,
[undefined, url.parse(resUrl).pathname, resUrl],
reqBody,
)
})
req.addEventListener('loadend', () => {
if (!req.responseType || ['json', 'document', 'text'].includes(req.responseType)) {
gameAPIBroadcaster.sendResponse(
method,
[undefined, url.parse(req.responseURL).pathname, req.responseURL],
reqBody,
req.response,
req.responseType,
req.status,
)
}
})
req.addEventListener('error', () => {
const resUrl = req.responseURL || reqUrl
gameAPIBroadcaster.sendError([undefined, url.parse(resUrl).pathname, resUrl], req.status)
})

return req
send(body) {
this.request = body
super.send(body)
}

get response() {
return this.responseHack || super.response
}

set response(response) {
this.responseHack = response
}

get responseText() {
return this.responseTextHack || super.responseText
}

set responseText(responseText) {
this.responseTextHack = responseText
}

get responseXMLHack() {
return this.responseXMLHackHack || super.responseXMLHack
}

set responseXMLHack(responseXMLHack) {
this.responseXMLHackHack = responseXMLHack
}
}

win.xhrHacked = true
Expand Down
3 changes: 3 additions & 0 deletions lib/game-api-broadcaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class GameAPIBroadcaster extends EventEmitter {
}

private parseResponseBody = (rawResBody: unknown, resType: XMLHttpRequestResponseType) => {
if (rawResBody == null) {
return undefined
}
switch (resType) {
case 'arraybuffer':
case 'blob': {
Expand Down

0 comments on commit 4dfc297

Please sign in to comment.