-
Notifications
You must be signed in to change notification settings - Fork 10
/
ajax_inspect.js
89 lines (81 loc) · 2.63 KB
/
ajax_inspect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
'use strict'
;(function () {
const match = RegExp('extension:\\/\\/([^\\/]*)').exec(document.currentScript.src)
const extensionID = match[1]
const XHR = XMLHttpRequest.prototype
// Remember references to original methods
const _open = XHR.open
const _send = XHR.send
// Collect data
XHR.open = function (method, url) {
this._method = method
this._url = url
return _open.apply(this, arguments)
}
// Implement "ajaxSuccess" functionality
XHR.send = function (postData) {
this.addEventListener('load', function () {
if (this._url.includes('asset')) {
// Ignore asset requests
return
}
let formattedPostData = postData
if (postData instanceof ArrayBuffer) {
formattedPostData = String.fromCharCode.apply(null, new Uint8Array(postData))
}
let response
switch (this.responseType) {
case null:
// no response data
break
case '':
case 'json':
case 'text':
response = this.responseText
break
case 'arraybuffer':
response = String.fromCharCode.apply(null, this.response)
break
default:
console.log('received unsupported response type', this.responseType)
}
let jsonRequest
if (formattedPostData) {
try {
jsonRequest = JSON.parse(formattedPostData)
} catch (_error) {
console.log('Cannot convert json:', _error)
console.log(formattedPostData)
}
}
let jsonResponse
if (response) {
try {
jsonResponse = JSON.parse(response)
} catch (_error) {
console.log('Cannot convert json:', _error)
console.log(response)
}
}
if (jsonRequest || jsonResponse) {
const payload = { jsonRequest: jsonRequest, jsonResponse: jsonResponse, metadata: {} }
if (this._url.includes('forgeofempires.com')) {
const urlObj = new URL(this._url)
const match = RegExp('^[^\\.]*').exec(urlObj.hostname)
payload.metadata.type = 'game'
payload.metadata.world = match[0]
} else if (this._url.includes('metadata')) {
const match = RegExp('https://foe([^\\.]*).innogamescdn.com/start/metadata\\?id=([^-]*)-(.*)').exec(this._url)
if (match !== null) {
payload.metadata.type = 'meta'
payload.metadata.lang = match[1]
payload.metadata.id = match[2]
payload.metadata.hash = match[3]
}
}
chrome.runtime.sendMessage(extensionID, payload)
}
})
return _send.apply(this, arguments)
}
})()